A TB6612FNG on an Arduino: two motors, both ways
Intermediate
Or see every lesson.
- 465parts on the bench
- 43boards running now
- 1.00xreal time, on every board
The TB6612FNG is a dual motor driver: two H bridges on one small board, enough for the two wheels of a robot. It is the driver in the SparkFun Inventor’s Kit, and it beats the old L298N and L293D at one thing that matters on batteries. Its MOSFET bridges keep a fraction of a volt for themselves, where the older chips lose two volts or more.
The circuit above is an Uno driving two gearbox motors through a TB6612FNG, off a pack of four AA cells. It runs a loop of moves: forward at full speed, forward at half speed, a spin on the spot, reverse, brake, and a coast in standby, and it prints each one. Press Run and watch the motors and the battery pack.
What you need
- An Arduino Uno (a Nano works the same way)
- A TB6612FNG breakout
- Two small DC gearbox motors
- A pack of four AA cells for the motors
- Jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| PWMA | Pin 5 | Left motor speed, a PWM pin |
| AIN1 | Pin 2 | Left motor direction |
| AIN2 | Pin 4 | Left motor direction |
| PWMB | Pin 6 | Right motor speed, a PWM pin |
| BIN1 | Pin 7 | Right motor direction |
| BIN2 | Pin 8 | Right motor direction |
| STBY | Pin 9 | Must be HIGH or the driver does nothing |
| VCC | 5 V | The logic supply |
| VM | Battery pack + | The motor supply, 2.5 to 13.5 V |
| GND | GND and battery pack - | One ground for both |
| AO1, AO2 | Left motor | |
| BO1, BO2 | Right motor |
The connection, pin by pin
Each motor needs three pins. Two direction pins pick the way it turns, and one PWM pin sets the speed. With two motors that is six pins, plus STBY, which wakes the whole chip. This build uses pins 2, 4 and 5 for the left motor, 7, 8 and 6 for the right one, and pin 9 for STBY, the same as SparkFun’s kit.
The PWM pins have to be pins that can do analogWrite(). On an Uno or a Nano those are 3, 5, 6, 9, 10 and 11. A Nano has the same chip and the same pin numbers as an Uno, so this wiring and sketch work on a Nano unchanged.
There are two supplies. VCC is the chip’s logic, from the Arduino’s 5 V. VM is the motor power, from the battery pack. The two grounds must be joined, or the driver cannot tell what the Arduino’s pins mean.
The SparkFun TB6612FNG library
SparkFun_TB6612.h, from the Library Manager as "SparkFun TB6612FNG", turns each motor into an object. You give it the two direction pins, the PWM pin, an offset of 1 or -1, and the STBY pin. drive() takes a speed from -255 to 255, and the sign is the direction. brake() stops a motor hard and standby() lets both coast.
forward(), back(), left() and brake() take both motors at once. left() turns one motor forward and the other backward, which spins the robot on the spot. The offset is for a motor wired the other way around: set it to -1 instead of swapping wires.
#include <SparkFun_TB6612.h>
const int AIN1 = 2;
const int AIN2 = 4;
const int PWMA = 5;
const int BIN1 = 7;
const int BIN2 = 8;
const int PWMB = 6;
const int STBY = 9;
const int offsetA = 1;
const int offsetB = 1;
Motor leftMotor = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor rightMotor = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
void loop() {
say("forward, full speed");
forward(leftMotor, rightMotor, 255);
delay(1500);
say("spin left on the spot");
left(leftMotor, rightMotor, 255);
delay(1200);
say("reverse");
back(leftMotor, rightMotor, 200);
delay(1500);
say("brake");
brake(leftMotor, rightMotor);
delay(1000);
say("standby: both motors coast");
leftMotor.standby();
delay(1500);
}What it does when you run it
The serial monitor prints each move as it starts: "drive: forward, full speed", then "drive: spin left on the spot", "drive: reverse", "drive: brake" and "drive: standby: both motors coast". At full speed both motors settle at about 200 rpm. In the spin, one turns forward and the other backward.
Watch the battery pack. With nothing running it shows 6.4 V, four fresh cells. With both motors at full speed it sags to about 6.16 V, because the cells have some resistance of their own. Real packs do the same, and a robot that resets when its motors start is usually a pack sagging too far.
The driver itself shows each channel’s mode on its face: off, CW, CCW or brake.
Brake or coast
Here the TB6612FNG differs from most drivers. When its PWM pin is low, it shorts the motor through the bridge, which is a brake, not a coast. So at half speed the motor is driven and braked in turn, and it slows down firmly instead of drifting.
With both direction pins low, the outputs turn off and the motor coasts to a stop. STBY low does the same for both motors at once. brake() is the hard stop, and standby() is the gentle one.
You do not need the library to use these modes. Set STBY HIGH once in setup(). Then, for one motor, AIN1 HIGH and AIN2 LOW turn it one way, AIN1 LOW and AIN2 HIGH turn it the other, and analogWrite() on PWMA sets the speed. Both direction pins HIGH is a brake, and both LOW is a coast. That is the whole truth table, and it is all the library does for you.
Common mistakes
STBY not wired. Every input has a pull-down inside, so an unwired STBY reads low and the chip does nothing at all. Pull the STBY wire in the circuit here and both motors sit at 0 rpm while the sketch prints its moves as if nothing were wrong.
Running the motors from the Arduino’s 5 V. Motors are noisy and draw more than the board can give. Give VM its own supply.
No shared ground between the Arduino and the motor supply.
PWMA or PWMB on a pin that cannot do PWM, so the motor only runs at full speed or not at all.
Expecting it to coast between PWM pulses. On this chip PWM low brakes.
Questions
How do I connect a TB6612FNG to an Arduino?
PWMA, AIN1 and AIN2 for one motor, PWMB, BIN1 and BIN2 for the other, STBY to a pin you set HIGH, VCC to 5 V, VM to the motor battery, and every GND together. This page uses pins 5, 2, 4, 6, 7, 8 and 9.
Which library works with the TB6612FNG?
SparkFun TB6612FNG (SparkFun_TB6612.h). You can also drive it with digitalWrite() and analogWrite() and no library at all.
Does the TB6612FNG work with an Arduino Nano?
Yes. A Nano has the same pin numbers and PWM pins as an Uno, so the same wiring and sketch work.
Why do my motors not turn with a TB6612FNG?
Check STBY first. It must be HIGH, and if it is not wired the chip stays off. Then check that VM has power and that the grounds are joined.
Keep going
Where the facts on this page come from
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.