Learn to code

ARDUINO & CODES

Previous Post
Controlling a stepper motor with an H-Bridge and Arduino
Next Post
Measuring Temperature with TMP36 Sensor

Controlling DC motors with TB6612FNG breakout board for Arduino

in Arduino Motor Control

In the previous tutorials you have learned how to control a motor with an H-Bridge. However, the H-Bridge is small and if you want to solder it permanently in your project there can be troubling on your way. Therefore you can use a breakout board such as the TB6612FNG motor driver.

The TB6612FNG is an easy and affordable way to control motors. So, the motor driver breakout board is beautiful small but does need a bit more effort to get it working. This guide covers the TB6612FNG motor driver which has a supply range of 2.5V to 13.5V and is capable of 1.2A continuous current and 3.2A peak current (per channel), so it works pretty well with most of our DC motors. If the TB6612FNG does not fit your project’s specifications, check out our various other motor driver boards.

Without going into much detail about the board it is wise to know the basics of the boards and the pin layout.

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

TB6612FNG Board Overview

The TB6612FNG is an H-Bridge driver that can turn a connected motor in either direction and at a variable speed.  This allows you to build a very responsive  robot driven by 2 motors that can:

– move forward and 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

Pin Label FunctionPower/Input/Output Notes
VM Motor Voltage Power This is the power source for the motors ranging from 2.2V - 13.5V
VCC Logic Voltage Power This is the voltage to power the board (2.7V - 5.5V)
GND GroundPower Common Ground for motor voltage and logic voltage. Should all be connected
STBY StandbyInputAllows the H-bridge to work
AIN1/BIN1 Input 1 for channels A/B InputInput for the motor that determines the direction
AIN2/BIN2 Input 2 for channels A/B InputInput for the motor that determines the direction
PWMA/PWMB PWM input for channels A/B InputPWM input that controls the speed of the motors
A01/B01 Output 1 for channels A/B OutputOutput to connect to one motor
A02/B02
Output 2 for channels A/B OutputOutput to connect to one motor

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 18
N20 Gear Motor N20 Gear Motor × 2
TB6612FNG TB6612FNG × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

Setup of the BreadBoard

The first connection that you will make is to connect the power and ground to the BreadBoard. You will use the 5V output for this project. The board can handle between 2.7 and 5.5V for its logic supply.

 

Connecting the tb6612fng to Arduino

Now it is time to connect the board to the Arduino. Please take a good look at how the output pins of the board are connected to the Arduino Uno board. If you are unsure about it, look at the below code. An overview of the connections are there.

Connecting the tb6612fng board to the motors

After connecting the TB6612 board to the Arduino you can attach the motors to it. You can also try it with 1 motor instead of 2.

Safety Warning: make sure that the voltage which is going from the TB6612 breakout board to the motors is sufficient and that it does not exceed the limits of the motors that you are using.

The Code

// This is the library for the TB6612 that contains the class Motor and all the
// functions
#include <SparkFun_TB6612.h>

// Pins for all inputs, keep in mind the PWM defines must be on PWM pins
// the default pins listed are the ones used on the Redbot (ROB-12097) with
// the exception of STBY which the Redbot controls with a physical switch
#define AIN1 2
#define BIN1 7
#define AIN2 4
#define BIN2 8
#define PWMA 5
#define PWMB 6
#define STBY 9

// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

// Initializing motors.  The library will allow you to initialize as many
// motors as you have memory for.  If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

void setup()
{
 //Nothing here
}


void loop()
{
   //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor1.drive(255,1000);
   motor1.drive(-255,1000);
   motor1.brake();
   delay(1000);
   
   //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor2.drive(255,1000);
   motor2.drive(-255,1000);
   motor2.brake();
   delay(1000);
   
   //Use of the forward function, which takes as arguements two motors
   //and optionally a speed.  If a negative number is used for speed
   //it will go backwards
   forward(motor1, motor2, 150);
   delay(1000);
   
   //Use of the back function, which takes as arguments two motors 
   //and optionally a speed.  Either a positive number or a negative
   //number for speed will cause it to go backwards
   back(motor1, motor2, -150);
   delay(1000);
   
   //Use of the brake function which takes as arguments two motors.
   //Note that functions do not stop motors on their own.
   brake(motor1, motor2);
   delay(1000);
   
   //Use of the left and right functions which take as arguements two
   //motors and a speed.  This function turns both motors to move in 
   //the appropriate direction.  For turning a single motor use drive.
   left(motor1, motor2, 100);
   delay(1000);
   right(motor1, motor2, 100);
   delay(1000);
   
   //Use of brake again.
   brake(motor1, motor2);
   delay(1000);
   
}

 

Code Explanation

Download the Library

Final step is uploading the code. First we must download and install the library.

Download the library using to GITHUB GitHub repository.

#include <SparkFun_TB6612.h>

 

Define the inputs

Below are all the connections made from the TB6612 Board to Arduino. At the breadboard layout, you can see it in detail.

// Pins for all inputs, keep in mind the PWM defines must be on PWM pins
// the default pins listed are the ones used on the Redbot (ROB-12097) with
// the exception of STBY which the Redbot controls with a physical switch
#define AIN1 2
#define BIN1 7
#define AIN2 4
#define BIN2 8
#define PWMA 5
#define PWMB 6
#define STBY 9

 

Various functions for to drive the motor
To control the dc motors various commands are used. Below are the commands that you can use to control your motors.
 

       //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor1.drive(255,1000);
   motor1.drive(-255,1000);
   motor1.brake();
   delay(1000);
   
   //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor2.drive(255,1000);
   motor2.drive(-255,1000);
   motor2.brake();
   delay(1000);
   
   //Use of the forward function, which takes as arguements two motors
   //and optionally a speed.  If a negative number is used for speed
   //it will go backwards
   forward(motor1, motor2, 150);
   delay(1000);
   
   //Use of the back function, which takes as arguments two motors 
   //and optionally a speed.  Either a positive number or a negative
   //number for speed will cause it to go backwards
   back(motor1, motor2, -150);
   delay(1000);
   
   //Use of the brake function which takes as arguments two motors.
   //Note that functions do not stop motors on their own.
   brake(motor1, motor2);
   delay(1000);
   
   //Use of the left and right functions which take as arguements two
   //motors and a speed.  This function turns both motors to move in 
   //the appropriate direction.  For turning a single motor use drive.
   left(motor1, motor2, 100);
   delay(1000);
   right(motor1, motor2, 100);
   delay(1000);
   
   //Use of brake again.
   brake(motor1, motor2);
   delay(1000);

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu