Introduction: Using the Sparkfun Motor Driver 1A Dual TB6612FNG - a Beginner's Guide

About: I’m not a rocket scientist. I don’t have a master’s degree in Electrical Engineering. I love automating, hacking, robotics, creating, building, understanding. A while back I discovered a way to turn my inter…

As a beginner myself, I struggled to find a one-stop set of instructions to get up and running with Sparkfun's Motor Driver 1A Dual TB6612FNG.  This motor driver breakout board is exceptional value, is beautifully small, but does need a little more effort to get it working. 

The TB6612FNG is an H-Bridge driver - I won't go into the technicalities of how it works, but the end result is that it can turn a connected motor in either direction and at a variable speed.  This allows you to build a very agile robot driven by 2 motors that can:
- move forward
- move backward
- turn in a very tight circle by driving one motor forward and one motor in reverse
- a variety of other moves using differential motor speeds and directions

To keep this instructable focused on the driver, I've built the bot on a simple Meccano body and haven't extended it to include any obstacle detection - the bot just drives a set pattern.

Before you start - the driver's specs:
- Logic supply voltage (VCC) of 2.7-5.5 VDC
- Motor supply voltage (VM) up tp 15VDC
- Current of 1.2A constant per channel (3.2A peak)
- Drives up to 2 motors

Images:
1. Finished Robot
2. The TB6612FNG breakout board
3. Full circuit diagram

Step 1: Get Your Stuff

First: The Ingredients
- 1 x Sparkfun Motor Driver 1A Dual TB6612FNG
- 1 x set of Header pins
- 1 x Breadboard plus connecting wires
- 8 x 1N4001 rectifier diodes (or similar)
- 1 x Arduino Uno R3
- 1 x 6V power supply (I used 4x 1.2V NiMH rechargeables in a holder)
- 1 x 9V battery with a 2.1mm barrel connector
- 2 x DC motors not exceeding the driver's current and voltage rating (I used Pololu's 250:1 micro metal gearmotor for their manageable RPM and high torque)
- Mounting brackets and wheels (I used Pololu's extended gearmotor brackets and 32x7 wheels - small wheels = manageable speed)
- Something to build the body (Meccano, Lego, thick corrugated card, etc)


Second: The Cooking Utensils
- Soldering Iron
- Resin core solder
- Wire strippers

Step 2: Solder Your Connections

You'll need to solder a few connections before you get started.  I found the fine solder work to be daunting, but randofo's How-To helped immensely - along with a good dose of practice on some old boards I found lying around.

The breakout board
The TB6612FNG breakout comes without pins - so you'll need to solder your own. 

Snap 2 sets of 8 header pins off the strip using pliers to get a clean break on the right pin.  Place the header pins into a breadboard, and then the breakout board on top of this - this will ensure the right spacing and a 90 degree angle between the board and the header pins.  I've also used play-doh to hold pins in place (play-doh won't melt, it simply goes crisp and can be picked off).  You'll see from my less-than-fine solder work that I'm still getting to grips with my soldering iron!

Battery connections - the Motor's battery
The motor will run off a separate 6V source.  Depending on the type of connector you have for your battery holder, you may need to solder header pins onto the leads - the power for the motor will connect into the breadboard. (The breakout board has capacitors on-board to isolate noise from the motor, so no need to add these yourself)

Battery connections - the Arduino battery
The Arduino will run off a 9V battery, so that it doesn't need to be tethered to the USB cable.  Depending on the connector/holder you have you may need to solder a 2.1mm barrel connector onto the leads (make sure the centre pin is soldered to the positive lead).  Alternatively buy a holder with a connector already attached.

Motor Terminals
Finally, solder wires to the motors' terminals. I used jumper wires, stripped the one end and soldered it onto the motor. That way, you have the pins to connect the motor to the breadboard.

Images:
1. Soldering the breakout board (can you tell I'm still learning!?!)
2. Battery connectors soldered
3. Motor terminals soldered and motors mounted

Step 3: Set Your Breadboard Up

Keeping it all together
I've used a rubber band to hold my Arduino onto my breadboard.  This prevents any accidental shorting of contacts on the underside of the Arduino, and keeps it attached to the breadboard - you can't accidentally de-wire your whole project if the breadboard wanders too far from the Arduino!

Position the Motor Driver
Place the TB6612FNG breakout board onto the breadboard.  It needs to straddle the break down the middle of the breadboard so as to keep the two rows of pins on separate circuits.  The one side of the driver board handles inputs, and the other outputs.

Power Rails
My breadboard has 2 power rails - I use one (the top one in the images) to provide the Arduino/TB6612FNG power, and the other (the bottom one) to provide the Motor's power.  You need to drive the motors from a separate power source as the current the motors draw is likely to be too much for the Arduino to handle.  You must connect the GND from the two sources to ensure a common ground.

Images:
1. Mounting the breakout board
2. Overall circuit diagram
3. Physical circuit, motors and motor batteries off-frame (note the jumper cable on the left connecting the ground from the two power sources)

Step 4: Connect the Arduino

Now wire the Breakout board to the Arduino - both the control pins and the power.

Connect the Power:
The power you're connecting here is the power needed to drive the TB6612FNG, not the power to drive the motors.
- Connect the Arduino 5V to the VCC
- Connect the Arduino GND to the GND

Connect the Control Pins:
Each motor (labelled A and B) has 3 control inputs: IN1 and IN2 to control the direction it turns, and a PWM input to control speed.  The STBY pin is used to bring the motors to life / put them to sleep
- Pin 3 ---> PWMA
- Pin 8 ---> AIN2
- Pin 9 ---> AIN1
- Pin 10 ---> STBY
- Pin 11 ---> BIN1
- Pin 12 ---> BIN2
- Pin 5 ---> PWMB

Images:
1. Connect the power
2. Connect the control pins
3. Circuit Diagram
4. Reverse side of the breakout board, showing connection points

Step 5: Connect the Motors

Next is to connect the motors to the breadboard.

Connect Motor Power
Connect the bottom power rail (6V) to the breakout board VM pin

Connect the Output Pins
Connect the A01 & A02 / B01 and B02 pins of the breakout board to the breadboard.  These pairs (01 and 02) will drive each motor - the TB6612FNG will switch the polarity and control the voltage of these to determine the direction and the speed that the motor spins in.

In the next step we'll connect the motors to the breadboard (with some back EMF protection)

Images:
1. Connect the power from the bottom rail
2. Connect the outputs to the breadboard, ready to accept the motor connections. Note motor battery connected in bottom left

Step 6: Sidetrack - Back EMF Protection

There is loads of info on Back EMF Protection, but I'm not really that technical.  So in my layman's terms...

EMF (Electromotive Force) is basically current that is generated (in this case) when inertia keeps a motor spinning after its power has been cut.  The motor effectively becomes a generator, and the current it generates "kicks back" into the circuit potentially causing damage to the components.

Luckily there is a way that we can protect against this...

Back EMF Protection is most easily achieved by connecting a diode in parallel to the motor. The diode (in simple terms) directs the voltage back into the motor so that it doesn't damage the rest of the circuit.  A common diode to use is the 1N4001.

In a simple circuit that drives a motor in one direction, this is quite easy - refer to the diagram above.  Connect the diode in parallel to the motor - the end with the white stripe connects to the positive supply, and the other to the ground.


However, we are controlling a motor that can spin in either direction, which means that this method won't work.  We need to be a little more creative and unfortunately use 4 times as many diodes.

Step 7: Add Back EMF Protection

In order to protect the driver and circuit against back EMF, we need to connect 2 rectifier diodes to each connection that each motor makes to the breadboard (I've called these connections  the "motor terminals" for simplicity).  This caters for the fact that each motor can spin in either direction. 

One diode connects the motor terminal to Positive, the other diode connects the motor terminal to GND.  It's important to get the polarity right, so connect as follows:
 - Motor Terminal ---> Positive: white stripe of diode to the positive power rail
 - Motor Terminal ---> Ground: white strip of diode to the motor terminal

Finally, connect the motor to the breadboard - in the image here, the white cable.

Repeat this for each of the 3 remaining motor outputs

Images:
1. Connections for a single motor terminal
2. Full board, showing all 4 motor terminal connections

Step 8: Final Assembly

We've now connected everything we need to from an electrical perspective:
- Arduino to the TB6612FNG breakout board
- Breakout board to motor power and diodes for back EMF protection
- Motors to the breadboard

We now need to put it all together onto a simple body:
- 2 wheels mounted to the motors
- a third caster wheel/ball that moves freely
- space to hold the breadboard, arduino and batteries

Here's what I came up with - you could work this with anything from lego to meccano to a simple plastic punnet / cardboard box.

Step 9: The Code

Finally, here is the code that I've used to control the motor driver.  Hope it's commented clearly enough to make sense, and apologies I couldn't work out how to provide a download link to it!

Enjoy.
/*
Sample Code to run the Sparkfun TB6612FNG 1A Dual Motor Driver using Arduino UNO R3

This code conducts a few simple manoeuvres to illustrate the functions:
  - motorDrive(motorNumber, motorDirection, motorSpeed)
  - motorBrake(motorNumber)
  - motorStop(motorNumber)
  - motorsStandby

Connections:
- Pin 3 ---> PWMA
- Pin 8 ---> AIN2
- Pin 9 ---> AIN1
- Pin 10 ---> STBY
- Pin 11 ---> BIN1
- Pin 12 ---> BIN2
- Pin 5 ---> PWMB

- Motor 1: A01 and A02
- Motor 2: B01 and B02

*/

//Define the Pins

//Motor 1
int pinAIN1 = 9; //Direction
int pinAIN2 = 8; //Direction
int pinPWMA = 3; //Speed

//Motor 2
int pinBIN1 = 11; //Direction
int pinBIN2 = 12; //Direction
int pinPWMB = 5; //Speed

//Standby
int pinSTBY = 10;

//Constants to help remember the parameters
static boolean turnCW = 0;  //for motorDrive function
static boolean turnCCW = 1; //for motorDrive function
static boolean motor1 = 0;  //for motorDrive, motorStop, motorBrake functions
static boolean motor2 = 1;  //for motorDrive, motorStop, motorBrake functions


void setup()
{
//Set the PIN Modes
  pinMode(pinPWMA, OUTPUT);
  pinMode(pinAIN1, OUTPUT);
  pinMode(pinAIN2, OUTPUT);

  pinMode(pinPWMB, OUTPUT);
  pinMode(pinBIN1, OUTPUT);
  pinMode(pinBIN2, OUTPUT);

  pinMode(pinSTBY, OUTPUT);

}

void loop()
{

  //Drive both motors CW, full speed
  motorDrive(motor1, turnCW, 255);
  motorDrive(motor2, turnCW, 255);

  //Keep driving for 2 secs
  delay(2000);

  //Turn towards motor1: Stop Motor1, slow Motor2
  motorStop(motor1);
  motorDrive(motor2, turnCW, 192);

  //Keep turning for 2 secs
  delay(2000);

  //Turn in opposite direction: Stop Motor2, slow Motor1
  motorDrive(motor1, turnCW, 192);
  delay(250);
  motorStop(motor2);

  //Keep turning for 2 secs
  delay(2000);

  //Straighten up
  motorDrive(motor2, turnCW, 192);
  delay(500);

  //Put motors into Standby
  motorsStandby();
  delay(1000);

  //Do a tight turn towards motor1: Motor2 forward, Motor1 reverse
  motorDrive(motor1, turnCCW, 192);
  motorDrive(motor2, turnCW, 192);

  //Keep turning for 2 secs
  delay(2000);


  //Apply Brakes, then into Standby
  motorBrake(motor1);
  motorBrake(motor2);
  motorsStandby();

  //Stand still for 5 secs, then we do it all over again...
  delay(5000);

}

void motorDrive(boolean motorNumber, boolean motorDirection, int motorSpeed)
{
  /*
  This Drives a specified motor, in a specific direction, at a specified speed:
    - motorNumber: motor1 or motor2 ---> Motor 1 or Motor 2
    - motorDirection: turnCW or turnCCW ---> clockwise or counter-clockwise
    - motorSpeed: 0 to 255 ---> 0 = stop / 255 = fast
  */

  boolean pinIn1;  //Relates to AIN1 or BIN1 (depending on the motor number specified)


//Specify the Direction to turn the motor
  //Clockwise: AIN1/BIN1 = HIGH and AIN2/BIN2 = LOW
  //Counter-Clockwise: AIN1/BIN1 = LOW and AIN2/BIN2 = HIGH
  if (motorDirection == turnCW)
    pinIn1 = HIGH;
  else
    pinIn1 = LOW;

//Select the motor to turn, and set the direction and the speed
  if(motorNumber == motor1)
  {
    digitalWrite(pinAIN1, pinIn1);
    digitalWrite(pinAIN2, !pinIn1);  //This is the opposite of the AIN1
    analogWrite(pinPWMA, motorSpeed);
  }
  else
  {
    digitalWrite(pinBIN1, pinIn1);
    digitalWrite(pinBIN2, !pinIn1);  //This is the opposite of the BIN1
    analogWrite(pinPWMB, motorSpeed);
  }



//Finally , make sure STBY is disabled - pull it HIGH
  digitalWrite(pinSTBY, HIGH);

}

void motorBrake(boolean motorNumber)
{
/*
This "Short Brake"s the specified motor, by setting speed to zero
*/

  if (motorNumber == motor1)
    analogWrite(pinPWMA, 0);
  else
    analogWrite(pinPWMB, 0);

}


void motorStop(boolean motorNumber)
{
  /*
  This stops the specified motor by setting both IN pins to LOW
  */
  if (motorNumber == motor1) {
    digitalWrite(pinAIN1, LOW);
    digitalWrite(pinAIN2, LOW);
  }
  else
  {
    digitalWrite(pinBIN1, LOW);
    digitalWrite(pinBIN2, LOW);
  } 
}


void motorsStandby()
{
  /*
  This puts the motors into Standby Mode
  */
  digitalWrite(pinSTBY, LOW);
}