Example Code for Arduino-Dual DC Motor PWM Control
Last revision 2025/12/10
This article offers a detailed guide on using Arduino to control dual DC motors via PWM, covering hardware and software setup, wiring, and providing sample code for effective motor speed and direction management.
Hardware Preparation
- DFRobot RoMeo (Arduino Robot Control Board), SKU: DFR0004, Quantity: 1, Purchase Link: DFRobot
- DC Motors, Quantity: 2
- External Power Supply (7V~12V DC), Quantity: 1
- USB Cable, Quantity: 1
Software Preparation
- Arduino IDE 0022 and above, Download Link: Arduino.cc
- Select “Arduino UNO” as the hardware in Arduino IDE.
Wiring Diagram
| Pin | Function |
|---|---|
| Digital 4 | Motor 1 Direction control |
| Digital 5 | Motor 1 PWM control |
| Digital 6 | Motor 2 PWM control |
| Digital 7 | Motor 2 Direction control |
"PWM Mode"

Other Preparation Work
- Connect DC motors to the Motor Terminals on the Romeo.
- Apply power to the Motor Power Input terminal (GND and VIN) with an external power supply (ensure correct polarity).
- Connect the Romeo to your computer using a USB cable.
- Open Arduino IDE and select the correct board and port.
Sample Code
//Standard PWM DC control
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control
int M2 = 7; //M1 Direction Control
///For previous Romeo, please use these pins.
//int E1 = 6; //M1 Speed Control
//int E2 = 9; //M2 Speed Control
//int M1 = 7; //M1 Direction Control
//int M2 = 8; //M1 Direction Control
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void advance(char a,char b) //Move forward
{
analogWrite (E1,a); //PWM Speed Control
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void back_off (char a,char b) //Move backward
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void turn_L (char a,char b) //Turn Left
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void turn_R (char a,char b) //Turn Right
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void setup(void)
{
int i;
for(i=4;i<=7;i )
pinMode(i, OUTPUT);
Serial.begin(19200); //Set Baud Rate
Serial.println("Run keyboard control");
}
void loop(void)
{
if(Serial.available()){
char val = Serial.read();
if(val != -1)
{
switch(val)
{
case 'w'://Move Forward
advance (255,255); //move forward in max speed
break;
case 's'://Move Backward
back_off (255,255); //move back in max speed
break;
case 'a'://Turn Left
turn_L (100,100);
break;
case 'd'://Turn Right
turn_R (100,100);
break;
case 'z':
Serial.println("Hello");
break;
case 'x':
stop();
break;
}
}
else stop();
}
}
Result
Send serial commands ('w' for forward, 's' for backward, 'a' for left, 'd' for right, 'x' for stop) to the Romeo via the serial monitor (19200 baud rate). The motors will respond accordingly:
- 'w': Both motors run forward at max speed.
- 's': Both motors run backward at max speed.
- 'a': Motor 1 runs backward, Motor 2 runs forward (turn left).
- 'd': Motor 1 runs forward, Motor 2 runs backward (turn right).
- 'x': Both motors stop.
Additional Information
- PWM DC motor control uses Pin 4,7 for direction and Pin 5,6 for speed.
- For previous Romeo versions, the pins used to control the motor are Pin 7,8 (Direction) and Pin 6,9 (Speed). Check the Motor Control Pin Jumpers for details.
Was this article helpful?
