The TB6612FNG is a dual motor driver IC from Toshiba. There are plenty of breakout boards out there for it and it's one of the most popular choices to drive small motors.
There are plenty of of online resources on getting started witht he TB6612FNG but I decided to write this anyway to better compile what I came across.
I'll be focusing on the control logic and also explain the Sparkfun TB6612FNG motor driver library in detail in this instructable.
Step 1: Parts Required
Here's what we'll be using today:
1) Micro Metal motors
2) TB6612FNG motor driver
3) An Arduino and USB cable
4) Power source for the motors
6) Jumper wires
Step 2: The TB6612FNG Breakout Board
As I mentioned before, there are plenty of breakout boards from different manufacturers for the TB6612FNG. All of them have more or less the same components on them and also similar pinout.
Capacitors are soldered on to the board for protection against noise from the motors, so you wont have to solder those ceramic capacitors to the motors.
The IC also comes with internal diodes to protect from back EMF from the motors. But having extra safety measures hurt no one. I didn't add them because my motors are not very big and I'm short on diodes :|
Step 3: Pin Outs
The TB6612FNG breakout board has a very convinient pinout. All the motor outputs, inputs and power connections are nicely grouped together for maximum ease of use.
I made an illustration of the pinouts and how to connect them, I hope it comes in handy when connecting all those wires :)
Step 4: Schematics
I'm fairly new to using Fritzing. I find the circuit schematics from Fritzing quite difficult to understand, but the breadboard view is convenient for Instructables. Feel free to ask any questions if any of the wire connections look confusing.
Step 5: Wiring It Up
Wire everything up according to schematics. There's alot of wires, make sure to double check after every connection.
I used the following Arduino Pins for the motor driver inputs:
Motor Driver -> Arduino Pin Number
1) PWMA -> 5
2) INA1 -> 2
3) INA2 -> 4
4) PWMB -> 6
5) INB1 -> 7
6) INB2 -> 8
Things that can go wrong in this step:
1) Do not reverse the polarity while connecting the Vm and the GND from the power source. You might fry your motor driver.
2) Make sure to connect the PWMA and PWMB to PWM pins on the arduino.
3) Remember to connect the Arduino GND and the GND from the motor driver if you're using a different power source for each.
Step 6: Downloading and Installing the Library
Download the library from Sparkfun's GitHub page.
Once you've downloaded the zip file, open up your Arduino IDE.
From Sketch > Include Library > Add .Zip Library, add the library you downloaded.
Once successfully installed, it should show up on File > Examples, as 'SparkFun TB6612FNG Motor Library'
If you're having difficulties downloading and installing an Arduino Library, check out step 5 of this instructable.
Step 7: Running the Example Code
Now that we have our library ready, we can upload the example code to test it out.
1) Open the 'MotorTestRun' example from the 'Sparkfun TB6612FNG Motor Driver Library' listed on your libraries.
Note: If you're not using the same pin numbers as mentioned on step 5, be sure to change the pin definitions to according to your setup.
2) Select your board from the board manager
3) Upload your code and the motors should start moving
Once you upload the the motors should start moving. If they're not, check your wiring again.
Step 8: The Library Explained
Now to explain how to use the library for your own piece of code.
First begin with importing the library and initializing the pins on arduino
#include <SparkFun_TB6612.h> #define AIN1 2 #define AIN2 4 #define PWMA 5 #define BIN1 7 #define BIN2 8 #define PWMB 6<br><br>#define STBY 9<br>
To initialize your motor objects, you need to set offsets for each motors. Imagine if you're doing a forward command on your motor, and it's spinning in reverse. You could manually rewire it, or you could just change the offset from here. Nifty little QoL hack added by SparkFun. The values of these offsets are either 1 or -1.
You then have to initialize each of the Motors with the following parameters;
Motor = Motor( Pin 1, Pin 2, PWM pin, offset, Standby pin)
const int offsetA = 1; const int offsetB = 1; Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
And with this, you're done initializing the library. No more steps in the setup() function, we just run the code in the loop() function.
The motor method has the following functions. Tinker around to check them all out.
1) <motor_name>.drive(value, time)
Motor_name = name of your motor object
value= 255 to -255; negative values will make the motor move in reverse
time= time in milliseconds
Brake function doesn't take any arguments, brakes the motors.
3) brake(<motor_name1>, <motor_name2 )
Brake function takes motor object names as arguments. Brakes the motors passed into the function.
4) forward(<motor_name1>, <motor_name2>, time)
forward(<motor_name1>, <motor_name2> , speed, time)
Function accepts two motor object's name, optionally PWM speed and time in milliseconds and runs the motor in forward direction for the passed amount of time. If the value of speed is in negative, the motor will go backwards. The default speed is set to 255.
5) back(<motor_name1>, <motor_name2> , time)
back(<motor_name1>, <motor_name2>, speed, time)
Function accepts two motor object's name, optionally PWM speed and time in milliseconds and runs the motor in forward direction for the passed amount of time. If the value of speed is in negative, the motor will go forward. The default speed is set to 255.
6) left(<motor_name1>, <motor_name2>, speed)
right(<motor_name1>, <motor_name2>, speed)
Function accepts two motor object names and speed. The order of the motor objects passed as parameters is important. To drive single motors, use <motor>.drive() instead.