Example Code for Arduino-Motor Control

Last revision 2026/01/06

This article guides you through the process of controlling motors with Arduino, including hardware setup, software requirements, and detailed example code for motor direction and speed control using PWM.

Hardware Preparation

Software Preparation

Other Preparation Work

  1. Unscrew the screw and open the USB programming port.
  2. Turn on Vortex power switch, and plug in the Micro usb cable, it will install the driver automatically if you have installed Arduino IDE. If not, you can find it in the driver file in Arduino IDE folder-->Drivers folder.
  3. There is a switch close to the USB port, make sure the trigger is close to the USB side. This is MP3 switch, we'll teach you how to add new song in the following chapter.
  4. Open your Arduino IDE, select "Arduino UNO" and right "COM port" in Arduino IDE, now you can enjoy coding.

Sample Code

int E1 = 5;
int M1 = 9;
int E2 = 6;
int M2 = 10;
void setup()
{
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
}
void loop()
{
  int value;
  for(value = 0 ; value <= 255; value+=5)    //foreward
  {
    digitalWrite(M1,HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E1, value);   //PWM Speed control
    analogWrite(E2, value);   //PWM Speed Control
    delay(30);
  }
  for(value = 0 ; value <= 255; value+=5)   //backward
  {
    digitalWrite(M1, LOW);
    digitalWrite(M2, LOW);
    analogWrite(E1, value);   //PWM Speed control
    analogWrite(E2, value);   //PWM Speed Control
    delay(30);
  }
}

Was this article helpful?

TOP