Example Code for Arduino-DC Motor Control
Last revision 2026/01/08
Explore the setup and programming of DC motor control using Arduino, featuring detailed example code, wiring diagrams, and instructions for setting motor speed and direction.
Hardware Preparation
- DF_UNO 1
- Micro Metal Gearmotor 2
- Regulated Power 1
- The Shield 1
- Wires
Software Preparation
Wiring Diagram

Sample Code
- Open Arduino IDE
- Upload the code to UNO
/**set control port**/
const int E1Pin = 10;
const int M1Pin = 12;
const int E2Pin = 11;
const int M2Pin = 13;
/**inner definition**/
typedef struct {
byte enPin;
byte directionPin;
} MotorContrl;
const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;
const MotorContrl MotorPin[] = { {E1Pin, M1Pin}, {E2Pin, M2Pin} } ;
const int Forward = LOW;
const int Backward = HIGH;
/**program**/
void setup() {
initMotor();
}
void loop() {
int value;
/**test M1 **/
setMotorDirection( M1, Forward );
setMotorSpeed( M1, 100 );
delay(1000);
setMotorSpeed( M1, 0 );
delay(100);
setMotorDirection( M1, Backward );
setMotorSpeed( M1, 50 );
delay(1000);
setMotorSpeed( M1, 0 );
delay(100);
/**test M2**/
setMotorDirection( M2, Backward );
for (value = 0 ; value <= 100; value += 5) {
setMotorSpeed( M2, value );
delay(100);
}
setMotorSpeed( M2, 0 );
setMotorDirection( M2, Forward );
for (value = 0 ; value <= 100; value += 5) {
setMotorSpeed( M2, value );
delay(100);
}
setMotorSpeed( M2, 0 );
}
/**functions**/
void initMotor( ) {
int i;
for ( i = 0; i < MotorNum; i++ ) {
digitalWrite(MotorPin[i].enPin, LOW);
pinMode(MotorPin[i].enPin, OUTPUT);
pinMode(MotorPin[i].directionPin, OUTPUT);
}
}
/** motorNumber: M1, M2
direction: Forward, Backward **/
void setMotorDirection( int motorNumber, int direction ) {
digitalWrite( MotorPin[motorNumber].directionPin, direction);
}
/** speed: 0-100 * */
inline void setMotorSpeed( int motorNumber, int speed ) {
analogWrite(MotorPin[motorNumber].enPin, 255.0 * (speed / 100.0) ); //PWM
}
Result
M1 will forward at full speed, and then half speed inversion; M2 velocity from fast to slow, reverse first, and then forward.
Was this article helpful?
