Sub topic 11
Wiring and Programming the Driver from an Arduino
Assume you know pinMode(), digitalWrite(), and analogWrite(), and can read a simple wiring diagram. This chapter uses a driver module with two direction inputs and one PWM enable input, such as an L298N or L293D module.
Connect the power and signals
An H-bridge driver keeps motor current away from the Arduino. The Arduino provides logic signals. A separate motor supply provides the current for the motor.
| Connection | Purpose |
|---|---|
| Motor supply positive to `VM` or `+12V` | Powers the motor output stage |
| Motor supply negative to `GND` | Return path for motor current |
| Arduino `GND` to driver `GND` | Gives both devices the same signal reference |
| Motor wires to `OUT1` and `OUT2` | Connects the motor to one H-bridge channel |
| Arduino pins 7 and 8 to `IN1` and `IN2` | Selects direction |
| Arduino pin 9 to `EN` | Controls speed with PWM |
Use the motor voltage required by the motor and driver. Do not power a motor from an Arduino I/O pin or from the Arduino 5 V pin. Place a bulk capacitor, such as 100–470 µF, across the driver motor-supply terminals. A 100 nF ceramic capacitor near the driver logic supply also helps reduce fast electrical noise.
Set direction and speed in code
The two direction inputs choose the polarity across the motor. The enable input receives a PWM signal. On common Arduino boards, analogWrite() accepts values from 0 to 255. A value of 128 produces approximately 50 percent duty cycle.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t EN = 9; // must be a PWM-capable pin
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN, OUTPUT);
}
// speed: -255 (full reverse) to 255 (full forward), 0 = stopped
void setMotor(int speed) {
speed = constrain(speed, -255, 255);
if (speed > 0) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} else if (speed < 0) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
} else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
analogWrite(EN, abs(speed));
}
void loop() {
setMotor(200);
delay(2000);
setMotor(0);
delay(500);
setMotor(-200);
delay(2000);
setMotor(0);
delay(500);
}The sign of speed selects direction. Its absolute value selects PWM duty cycle. Setting both inputs LOW usually selects coast, where the motor terminals are released. Driver modules differ, so check the data sheet before relying on a particular stop mode.
Avoid common wiring errors
- If the motor does nothing, check that the motor supply is connected and that Arduino ground and driver ground are shared.
- If speed control does nothing, verify that
ENis connected to a PWM-capable pin and that no module jumper permanently enables it. - If direction is reversed, swap the motor wires or exchange the two input signals.
- If the Arduino resets, separate motor power from logic power and improve decoupling. A motor starting or stalling can draw a large current pulse.
- Never connect two Arduino output pins together or connect an output directly to a motor terminal.
Exercise
The motor runs in one direction, but changing analogWrite(EN, 80) to analogWrite(EN, 200) has no effect. Name two likely causes.
Show answer
The enable input may not be connected to a PWM-capable Arduino pin, or an enable jumper may be forcing it permanently HIGH.