Sub topic 10
Reversing Direction with an H-Bridge Driver
Assume you can upload Arduino sketches, use pinMode() and digitalWrite(), and understand PWM from analogWrite(). Also assume the motor has its own suitable power supply and that its ground is connected to Arduino GND.
An H-bridge is a motor driver circuit that can connect a motor’s two terminals to either power or ground. By changing those connections, it reverses the current through the motor and therefore reverses its direction. A driver board contains the H-bridge transistors and usually adds protection features.
Why use an H-bridge?
A single MOSFET can switch a DC motor on and off or control its speed. It cannot normally reverse the motor because it only switches one side of the circuit.
An H-bridge driver usually provides:
- Two direction inputs.
- One PWM input for speed.
- A motor power input.
- A logic power input.
- A ground connection.
- Sometimes a standby or enable input.
The Arduino sends low-current control signals. The external motor supply provides the motor current.
Wiring a driver board
This example uses one channel of a driver such as the TB6612FNG.
| Driver connection | Arduino or circuit connection |
|---|---|
| `PWMA` | Arduino PWM pin 5 |
| `AIN1` | Arduino digital pin 7 |
| `AIN2` | Arduino digital pin 8 |
| `STBY` | Arduino digital pin 4 |
| Motor A terminals | The two motor wires |
| `VM` | Positive terminal of the external motor supply |
| `VCC` | Logic supply required by the driver |
| `GND` | Arduino `GND` and external supply negative |
Check your board’s datasheet before wiring it. Pin names and logic levels differ between driver boards. Connect the external supply ground to Arduino ground so the control signals have a shared voltage reference.
Do not connect a motor directly between Arduino pins. Do not power a motor from an Arduino I/O pin or from the board’s 5 V pin unless the motor’s current is known to be within the supply’s limits.
Direction and speed control
For a common H-bridge channel, the two direction inputs select the polarity:
| `AIN1` | `AIN2` | Typical result |
|---|---|---|
| LOW | LOW | Coast or stop |
| HIGH | LOW | Forward |
| LOW | HIGH | Reverse |
| HIGH | HIGH | Brake or stop, depending on the driver |
The PWM duty cycle controls average motor power. A value of 0 produces no drive, while 255 produces full drive on an Arduino Uno. The motor may not start at very low values because friction and the motor’s starting current require a minimum voltage.
// One channel of a dual H-bridge driver such as the TB6612FNG.
const int PWMA = 5; // PWM speed input
const int AIN1 = 7; // direction input 1
const int AIN2 = 8; // direction input 2
const int STBY = 4; // HIGH enables the driver
void setup() {
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(STBY, OUTPUT);
digitalWrite(STBY, HIGH);
}
// speed ranges from -255 to 255.
// Positive values move forward; negative values move backward.
void drive(int speed) {
speed = constrain(speed, -255, 255);
if (speed > 0) {
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
} else if (speed < 0) {
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
} else {
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
}
analogWrite(PWMA, abs(speed));
}
void loop() {
drive(200);
delay(2000);
drive(0);
delay(500);
drive(-200);
delay(2000);
drive(0);
delay(500);
}The drive() function uses a signed speed value. Its sign selects direction, and its magnitude selects speed. Stopping briefly before reversing reduces mechanical stress and current spikes. For a robot, change direction only after reducing the speed to zero, or ramp the speed gradually.
Common failures
- The motor is silent: Check
STBY, the motor supply, and the common ground. - The Arduino resets: The motor supply may be pulling down the Arduino supply, or motor noise may be entering the wiring.
- The direction is opposite: Swap the motor wires or swap the two direction branches in
drive(). - The driver is hot: Check stall current, supply voltage, cooling, and whether the motor is mechanically blocked.
- The motor only hums: Verify both motor terminals and check that the driver can provide the motor’s starting current.
Exercise
What does drive(-150) change compared with drive(150)?
Show answer
It selects the opposite direction while using the same PWM magnitude, so the motor receives approximately the same speed command with reversed polarity.