Sub topic 10
Reversing direction with an H-bridge driver
A single low-side MOSFET can switch a DC motor on and off and set its speed with PWM, but current can only flow one way through the motor. To reverse it you must reverse the polarity across its terminals. An H-bridge does this. This chapter covers how an H-bridge works, how to drive a TB6612FNG-style module from an Arduino, and the errors that most often go wrong.
This chapter assumes you have read the earlier chapters on external motor power with a common ground, PWM with analogWrite, and switching a motor with a MOSFET and flyback diode. It also assumes you can write functions in Arduino C++.
Why one switch is not enough
A DC motor spins in the direction set by the polarity of the voltage across it. Positive on terminal A and negative on terminal B turns it one way. Swapping the two turns it the other way.
A low-side MOSFET connects only one motor terminal, and only to ground. The other terminal stays wired to the supply. There is no way to put the supply on terminal B and ground on terminal A.
An H-bridge solves this with four switches arranged around the motor. The layout looks like the letter H, with the motor as the crossbar.
Each switch is a transistor inside the driver chip. The chip's logic keeps you from needing to manage them one by one.
The four useful states
The table lists what happens for each switch combination. Pairs are named by which diagonal is closed.
| Closed switches | Terminal A | Terminal B | Motor behavior |
|---|---|---|---|
| S1 and S4 | Supply | Ground | Spins forward |
| S2 and S3 | Ground | Supply | Spins in reverse |
| S3 and S4 | Ground | Ground | Brakes (terminals shorted) |
| None | Floating | Floating | Coasts (free spin) |
Braking and coasting differ. When the terminals are shorted, the motor's own back-EMF drives current through the short and resists rotation, so the motor stops quickly. When both are open, the motor spins down by friction alone.
Shoot-through
Closing S1 and S3 at the same time connects the supply straight to ground through two transistors. This is called shoot-through. If you built the bridge from discrete transistors, a software bug could destroy them in microseconds. Integrated drivers such as the TB6612FNG and L298N block these illegal combinations in hardware. Their input pins encode only the legal states, which is one reason to use a ready-made driver.
How the TB6612FNG is controlled
The TB6612FNG is a dual H-bridge, so one chip runs two motors. Each channel has three control pins.
| Pin | Role |
|---|---|
| `PWMA` | Speed. Takes a PWM signal. |
| `AIN1`, `AIN2` | Direction. Their combination selects the state. |
| `STBY` | Standby. Held HIGH to enable the whole chip. |
For channel A, the inputs map to behavior like this:
| AIN1 | AIN2 | PWMA | Result |
|---|---|---|---|
| HIGH | LOW | PWM | Forward at the PWM duty |
| LOW | HIGH | PWM | Reverse at the PWM duty |
| LOW | LOW | any | Stop (output high-impedance, so coasting) |
| HIGH | HIGH | any | Short brake |
Check the datasheet for your exact module. Pin labels and the stop/brake mapping differ between chips and between breakout boards.
Power connections
The TB6612FNG needs two supplies.
VCCis the logic supply, 2.7 to 5.5 V. Connect it to the Arduino's 5 V (or 3.3 V on a 3.3 V board).VMis the motor supply, up to about 13 V. Connect it to your external motor supply.
All grounds must be joined: the Arduino GND, the driver GND, and the motor supply ground. This is the same common-ground rule you applied for servos and MOSFETs. Without it, the logic signals have no shared reference and the driver behaves unpredictably.
The module's internal body diodes handle the inductive kickback from the motor, so you do not add a flyback diode across the motor as you did with the discrete MOSFET. A capacitor of 100 µF or more across VM and ground, placed close to the driver, is still good practice.
Wiring the example
The code below uses these connections.
| Driver pin | Arduino pin |
|---|---|
| `PWMA` | 5 (a PWM pin) |
| `AIN1` | 7 |
| `AIN2` | 8 |
| `STBY` | 4 |
Connect motor terminals to AO1 and AO2. Connect VM to the external supply, VCC to Arduino 5 V, and all grounds together.
A drive function with signed speed
Each direction needs two digital writes and one PWM write. Repeating that in every part of your sketch invites mistakes. A better approach is one function that accepts a signed number: positive for forward, negative for reverse, zero to stop.
// Dual H-bridge driver such as the TB6612FNG (channel A).
const int PWMA = 5; // speed (PWM pin)
const int AIN1 = 7; // direction input 1
const int AIN2 = 8; // direction input 2
const int STBY = 4; // standby: HIGH enables the driver
void setup() {
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(STBY, OUTPUT);
digitalWrite(STBY, HIGH);
}
// speed: -255 (full reverse) to 255 (full forward), 0 stops
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);
}Walk through how it works.
setup()makes all four control pins outputs and raisesSTBY. UntilSTBYis HIGH, the motor will not move at all.constrain()clamps the input, so a calculation that produces 300 cannot wrap around inanalogWrite.- The sign of
speedpicks theAIN1/AIN2combination. The magnitude, fromabs(speed), becomes the PWM duty. - The sketch pauses at zero for 500 ms between reversals. This gives the motor time to slow before it is driven the other way.
Why pause before reversing
A spinning motor acts as a generator. If you flip straight from full forward to full reverse, the supply must first stop the rotor and then accelerate it backward. The result is a large current spike, which can trip the driver's thermal or overcurrent protection, sag the supply, and reset the Arduino. Ramping the speed through zero, or inserting a short stop, reduces this stress. It also reduces wear on the gearbox.
Direction pins during PWM
You can apply PWM to either the speed pin or the direction pins. With the TB6612FNG, keep AIN1/AIN2 steady and put PWM on PWMA, as above. The default analogWrite frequency on pins 5 and 6 of an Uno is about 980 Hz, and on most other PWM pins about 490 Hz. That is audible as a whine in some motors. Many drivers accept PWM up to 100 kHz, but changing the frequency means reconfiguring the board's timers, which is beyond this chapter.
Choosing a driver
The H-bridge family has several common members. The right choice depends on current and voltage.
| Driver | Typical limits | Notes |
|---|---|---|
| L293D | 600 mA per channel, 4.5 to 36 V | Older, easy to find, drops about 1.4 V or more across its bipolar transistors. |
| L298N | 2 A per channel, 5 to 46 V | Common red module. Wastes noticeable voltage as heat, so a 6 V motor may see only about 4 V. |
| TB6612FNG | 1.2 A per channel, up to 13.5 V | MOSFET-based, low voltage drop, runs cooler. Good default for small hobby motors. |
| DRV8833 | 1.5 A per channel, 2.7 to 10.8 V | Low voltage, good for battery-powered robots. |
Bipolar drivers like the L298N lose voltage in their transistors. At a 6 V supply the motor receives noticeably less than 6 V. MOSFET drivers lose far less, so a small battery lasts longer and the chip stays cooler.
Common errors
These problems appear repeatedly with H-bridge circuits.
- Motor never moves. Check that
STBYis HIGH and thatVMis powered. The logic supply alone will not run the motor. - Motor runs only one way. One direction pin is miswired, or both read the same level. Print or measure the voltages on
AIN1andAIN2to confirm they differ. - Direction is backwards. Swap the two motor wires, or invert the sign in your code. Either is fine. Do not rewire the control pins to fix it.
- Arduino resets when the motor starts. The motor supply is shared with the Arduino and sags, or the grounds are wired through a long path. Add a bulk capacitor near
VMand give the Arduino its own supply path. - Driver gets very hot. The motor draws more than the driver's rating, often because it is stalled or the load is heavy. Measure current, or move to a larger driver.
- Motor creeps at low speed values. A motor needs a minimum duty to overcome friction. Values below roughly 60 to 80 out of 255 may only hum. Treat that range as a dead zone.
Adding a second motor
The second channel of the TB6612FNG works the same way with its own pins: PWMB, BIN1, BIN2, and outputs BO1 and BO2. STBY is shared. Wrap the code in a function that takes pin numbers, or in a small struct, so one drive routine serves both channels. This is the basis of a two-wheeled robot, where steering comes from giving the two channels different speeds.
Exercise
A sketch calls drive(-300) on the TB6612FNG code above. What happens to AIN1, AIN2, and the PWM duty, and why does the function behave this way?
Show answer
constrain() first limits the value to -255. Because the speed is negative, AIN1 is set LOW and AIN2 HIGH, which selects reverse. analogWrite then receives abs(-255), which is 255, so the motor runs in reverse at full duty. The clamp prevents an out-of-range value from wrapping around in analogWrite.
Exercise
Your motor runs fine in both directions at low speed, but the Arduino resets whenever you call drive(255) right after drive(-255). Give two causes and two fixes.
Show answer
Causes: reversing from full speed forces a large current spike because the motor's back-EMF and the new drive voltage add together, and that spike sags a shared supply; and the Arduino's ground return may share a thin wire with the motor current. Fixes: ramp the speed through zero or insert a short stop (for example 300 to 500 ms) before reversing, and add a bulk capacitor near VM while powering the Arduino from a separate or well-decoupled path.