A DC motor converts electrical power into mechanical rotation and is typically used as a left drive motor in mobile robots. It must be powered and controlled through a motor driver, connecting the motor terminals (often labeled M1 and M2) to the driver’s motor outputs, while the driver handles direction and speed control.
| Pin | Signal | Description |
|---|---|---|
M+ |
power | Positive motor terminal |
M- |
power | Negative motor terminal |
| From | To | Wire |
|---|---|---|
board:5V |
x1:VCC |
red |
board:GND |
x1:GND |
black |
board:D5 |
x1:IN1 |
green |
board:D6 |
x1:IN2 |
blue |
x1:OUT1 |
x2:M+ |
purple |
x1:OUT2 |
x2:- |
cyan |
This example was written and compile-checked for the arduino-uno. Codey Online adapts it to any other supported board for you.
const int motorIn1 = 5;
const int motorIn2 = 6;
void setup() {
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
Serial.begin(9600);
Serial.println("Left DC motor demo starting...");
}
void driveForward(int speedValue) {
analogWrite(motorIn1, speedValue);
digitalWrite(motorIn2, LOW);
}
void driveReverse(int speedValue) {
digitalWrite(motorIn1, LOW);
analogWrite(motorIn2, speedValue);
}
void motorStop() {
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
}
void loop() {
Serial.println("Forward");
driveForward(200);
delay(2000);
Serial.println("Stop");
motorStop();
delay(1000);
Serial.println("Reverse");
driveReverse(200);
delay(2000);
Serial.println("Stop");
motorStop();
delay(1000);
}
Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.
Start for free