출처: https://dronebotworkshop.com/tb6612fng-h-bridge/
Today we will look at another H-Bridge motor controller. This one uses MOSFETs and is a great replacement for the L298N.
Follow along and learn how to use the TB6612FNG H-Bridge with your Arduino projects.
Introduction
Controlling the speed and direction of DC motors is a very common application for both microcontrollers and microcomputers. You can probably think of many projects that you could build using these devices, from robots to cat toys.
But if you’ve ever tried connecting a DC motor directly to a microcontroller like an Arduino you’ll quickly find out that it isn’t feasible. Not only will it not work, as the Arduino cannot provide enough current for the motor, but you will also probably find yourself in the market for a new Arduino very soon!
DC motors require more current than any microcontroller or microcomputer can directly provide, and many of them operate on different voltages that your processor can’t deal with. So obviously some sort of interface is required.
The most common method of controlling a DC motor is to use a device called an H-Bridge. This type of controller allows you to control both the speed and direction of a DC motor, and a pair of H-Bridges can also be used to control a bipolar stepper motor.
We have used H-Bridges in many of our projects. They are relatively inexpensive and are pretty simple to work with. And the most common H-Bridge we’ve used thus far is the L298N.
The L298N is a very common component and it is used in hundreds, if not thousands, of Arduino designs. It’s inexpensive and, once you understand how it works, is quite simple to use. But it does have its flaws, which I’ll go into in a minute.
Today we will work with a better H-Bridge, one that can replace the L298N in many applications. It’s called a TB6612FNG and after using it I think you’ll agree that it is a much better choice than the old L298N.
H-Bridge Recap
Before we get started, it might be a good idea to recap how an H-Bridge works, just in case it’s still a mystery to you.
The following diagrams came from the article Controlling DC Motors with the L298N Dual H-Bridge and an Arduino that I wrote a few years ago. If you want a more in-depth explanation of DC motor and H-Bridge operation I would urge you to check it out.
You can visualize an H-Bridge as an arrangement of four switches. As you can see the switches are arranged in a pattern that looks a lot like the letter “H”. thus the name ”H-Bridge”.
The motor is placed in the middle of the “H” as illustrated in the diagrams. With the switches off the motor receives no current, as you would expect.
If we close a couple of switches, as illustrated above, you can see that the current is allowed to flow through the motor, causing it to rotate clockwise. Obviously you don’t want to close more than two switches at a time as doing so could cause a short circuit.
Closing the other two switches also applies current to the motor, but in the opposite polarity. This causes the motor to spin counterclockwise.
While you could construct an H-Bridge using physical switches it isn’t done very often. In most cases, the switches are replaced with semiconductors, like bipolar junction transistors (BJTs) and MOSFETs.
Replacing the switches with semiconductors has an additional advantage – it allows you to regulate the motor speed as well.
Pulse Width Modulation (PWM)
Pulse Width Modulation, or PWM, is essentially what it sounds like. A pulse is sent to the transistors controlling the H-Bridge, which turns the electronic “switch” off and on at a pretty rapid rate.
The width of the pulse can be changed to set the ratio of “on” versus “off”. This has the effect of controlling the speed of the motor.
PWM is a much better way of regulating the speed of a DC motor than just changing its supply voltage. With PWM the motor operates at full torque throughout the speed range, as opposed to the reduced torque you’d experience at lower speeds using voltage control.
TB66121FNG H-Bridge
Now let’s look at our “better” H-Bridge module.
The TB6612FNG H-Bridge is a small and inexpensive dual-channel H-Bridge controller that uses MOSFETs to achieve it’s great performance.
Made with MOSFETs
MOSFETs have an enormous advantage over bipolar junction transistors or BJTs.
A typical BJT has a voltage drop of approximately 0.7 volts when it is “on”, and since an H-Bridge requires the current to pass through two BJTs the total voltage drop is 1.4 volts. At low voltages this can really make a difference, if you supply the H-Bridge with 6-volts then the output to the motor will only be 4.6-volts. In this example, almost 25% of the voltage is lost in the H-Bridge.
The energy from that voltage needs to go somewhere, and it does – it is dissipated as heat. This is why most BJT-based H-Bridges require a big heatsink.
With a MOSFET you don’t have a fixed voltage drop. Instead, the MOSFET has a very small resistance when it is on, typically a fraction of an ohm. Even with two MOSFETs turned “on” in an H-Bridge design the total resistance is very low, in fact, if you’re using long thin-gauge wires for your power supply you may have more resistance in the wires than you do in the H-Bridge.
The result is an extremely low, almost negligible, voltage drop. This means that nearly all the voltage from the power supply is delivered to the motor. And, with very little voltage drop to deal with, there is very little heat to dissipate.
If you’d like to learn more about working with MOSFETs and Bipolar Junction Transistors please check out the article and video Arduino High-Current Interfacing – Transistors & MOSFETs.
TB6612FNG Specifications
The TB6612FNG has the following specifications (the specs are for each of the two H-Bridges in the package):
- Motor supply voltage of 2.5 to 13.5 volts DC.
- Logic supply voltage of 2.7 to 5.5 volts DC.
- Output current of 1.2 amperes continuous, 3.2 amperes peak.
- Built-in thermal shutdown.
- Standby power mode.
This device is a surface-mount chip that is available in many common modules, shields for the Arduino and HATs for the Raspberry Pi.
The pinouts of a typical TB6612FNG module are illustrated below:
One thing to note about the module layout is that all of the input pins are on one side and all of the output and power pins are on the other side. Notably absent from this dual H-Bridge module is a heatsink, although you can affix a small heatsink to the surface-mount chip if you desire.
Controlling the TB6612FNG
The TB6612FNG can be operated in four modes:
- Clockwise Rotation
- Counterclockwise Rotation
- Short Brake
- Stop
In order to control the H-bridge you use the AI1 (or on some models marked “AIN1”) and AI2 (AIN2) pins for the Motor A output. The B input control leads are used in a similar fashion for the Motor B output.
The following table shows how this works:
Input 1 (AI1 or BI1) | Input 2 (AI2 or BI2) | Mode |
HIGH | HIGH | Short Brake |
LOW | LOW | Stop |
HIGH | LOW | Clockwise |
LOW | HIGH | Counterclockwise |
The PWMA and PWMB inputs are used to control the motor speed using Pulse Width Modulation.
One thing to note is that for the TB6612FGH to operate you must hold the Standby input HIGH. You can tie it to VCC or use an output from your Arduino to control it.
A second noteworthy observation is that the Clockwise and Counterclockwise control signals are identical to the ones used in the popular L298N H-Bridge. This makes it pretty simple to use the TB6612FNG as a direct replacement in many situations.
TB6612 vs. L298N
The key phrase in the last paragraph is “In many situations”. There are some instances, such as higher voltage motors, where the TB6612FNG can’t be used. But for many designs, including ones using 6 and 12-volt motors, it is an ideal substitute that offers improved performance.
Take a look at the specifications of the TB6612FNG and the L298N viewed side-by-side.
If you have a motor that requires over 13.5 volts then that eliminates the TB6612FNG. However, if your motor is a low-voltage (i.e. 3-volt) device then the L298N is out of the picture.
Although the L298N can handle almost twice the continuous current that the TB6612FNG does, it is about even when it comes to peak current, in fact the TB6612FNG has a slight edge.
But after that the TB6612FNG is the clear winner.
- It is much smaller, in fact, most TB6612FNG modules are less than a quarter of the size of L298N modules.
- No heatsink, making it even smaller. However, it is a good idea to allow some airflow around the device.
- The efficiency is where the TB6612FNG really shines. There really is no contest here, something to strongly consider when building a battery-powered device.
- Virtually all of your motor supply voltage will get to the motor when you use the TB6612FNG. The L298N will drop 1.4-volts, as it uses BJTs.
- The TB6612FNG has a low-current standby mode that the L298N does not have.
I think that for my new designs I’ll be switching to the TB6612FNG. Even though I have a drawer full of L298N modules!
TB6612FNG with Arduino
Since the TB6612FNG uses a similar control arrangement to the L298N you can easily repurpose your old sketches to use the new controller. In fact, that’s exactly what I did for the first demo.
If you want to compare check out the “two motor with potentiometers” experiment in my article about the L298N. You should definitely see the similarities between the two.
TB6612FNG Hookup
For this experiment you will need the following components:
- An Arduino, I used an Arduino Uno but any Arduino will work.
- Two potentiometers to control speed. I used 10K linear pots, any linear pot with a value over 5K will suffice.
- A TB6612FNG dual H-Bridge motor controller.
- Two small DC motors. I used two 6-volt motors, the yellow ones you get with those small robot car kits.
- A power supply for the motors. As I used 6-volt motors my supply was 6-volts, consisting of 4 type AA batteries.
The hookup is as follows:
Use the table on the side of the hookup diagram to connect the TB6612FNG to the Arduino. And remember to tie the STBY (Standby) lead to +5-volts, or nothing will work.
TB6612FNG Arduino Sketch
After you hook up everything you’ll need to run a sketch to get it going. The following sketch should do the trick:
TB6612FNG H-Bridge Demo
/*
TB6612FNG H-Bridge Demo
TB6612-Demo.ino
Demonstrates use of TB6612FNG H-Bridge Motor Controller
Drives two DC Motors
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Motor A
int pwmA = 5;
int in1A = 3;
int in2A = 4;
// Motor B
int pwmB = 6;
int in1B = 7;
int in2B = 8;
// Speed control potentiometers
int SpeedControl1 = A0;
int SpeedControl2 = A1;
// Motor Speed Values - Start at zero
int MotorSpeed1 = 0;
int MotorSpeed2 = 0;
void setup()
{
// Set all the motor control pins to outputs
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(in1A, OUTPUT);
pinMode(in2A, OUTPUT);
pinMode(in1B, OUTPUT);
pinMode(in2B, OUTPUT);
}
void loop() {
// Set Motor A forward
digitalWrite(in1A, HIGH);
digitalWrite(in2A, LOW);
// Set Motor B forward
digitalWrite(in1B, HIGH);
digitalWrite(in2B, LOW);
// Read potentiometers and convert to range of 0-255
MotorSpeed1 = map(analogRead(SpeedControl1), 0, 1023, 0, 255);
MotorSpeed2 = map(analogRead(SpeedControl2), 0, 1023, 0, 255);
// Set the motor speeds
analogWrite(pwmA, MotorSpeed1);
analogWrite(pwmB, MotorSpeed2);
}
It’s a very simple sketch, and if you took a peek at the “2 potentiometers” sketch in the L298N article you’ll see it’s virtually identical.
We begin by defining variables that represent the Arduino pins we’ve connected our TB6612FNG motor controller to, the IN1 and IN2 pins as well as the PWM pins.
Next, we define variables to represent the analog connections to the potentiometers, as well as variables for the motor speed.
In the Setup, we simply define all of our pins as outputs, and then we move into the Loop.
We start the Loop by setting the direction of the two motors. If you wish you can use the table above to reverse the direction of one or both motors.
Next, we read the value from the two potentiometers, and as this value will range from 0 to 1023 we use the Map function to change the range to 0-255.
Finally, we use the analogWrite function, which produces PWM, to write out PWM pulses to the motor controllers, thus setting their speed.
<INSERT MOTOR CONTROL IMAGE>
Load the sketch up to your Arduino, connect the motor power supply and play with the two potentiometers. You should be able to control the motor speed with them.
TB6612FNG Robot Car Base
For our final experiment with the TB6612FNG, I’ll be using the TB6612FNG H-Bridge to drive the motors in a small robot car.
You could actually have used the last experiment to do exactly this, controlling the motors with the two potentiometers. But this time I want to do the programming a bit differently.
This time we will use a library.
Sparkfun Library for TN6612FNG
Sparkfun has released a library made specifically for the TN6612FNG, which you can download from GitHub.
This library simplifies using the motor controller. It provides functions to move the motor at any speed and in both directions, as well as functions to brake and stop.
And, as a bonus, the Sparkfun library comes with an example sketch that is perfect for our robot car. In fact, I only needed to modify one line to get it working.
Robot Car Hookup
I used one of those popular acrylic Robot Car kits, the ones you can easily get on eBay or Amazon for 10 – 20 dollars. These kits come with a base, a couple of motors, some matching wheels, a battery holder and all of the hardware you need to put it together.
The electrical hookup of our robot car is very similar to the hookup we just used. The connections between the Arduino and the TB6612FNG are identical, with the addition of a connection from the Arduino to the Standby pin. And I’ve removed the two potentiometers.
I’m still using the four AA cells to power the motors, and I used a 9-volt battery for the Arduino. Another great choice for powering the Arduino would be to use a USB power bank.
In my case I kept the wiring on the solderless breadboard and only used a single standoff to hold the Arduin. As a result I believe I created the world’s ugliest robot car!
You can do MUCH better by wiring everything permanently and properly mounting and balancing the Arduino and battery holders. In my case the unit was out of balance, so I attached a small C-clamp to add a bit of weight in the back!
Robot Car Sketch
Before you can run the robot car sketch you’ll need to install the Sparkfun Library. The easiest way to do that is to download the library as a zip file. Save it somewhere on your computer where you can find it later, your Downloads folder is an obvious place.
Now open your Arduino IDE and click on the Sketch menu item at the top of the page. Select Include Library, this will open up a sub-menu.
In the sub-menu the second item from the top will be Add ZIP Library. Select that and a dialog box will open. Use the dialog box to navigate to the location you saved your library ZIP file and select it.
The library will be installed, along with the sample sketch.
To run the sample sketch in your Arduino IDE you need to select the File menu from the top, and then Examples. This will bring up a sub-menu. Scroll down the sub-menu until you see Examples from Custom Libraries, near the bottom. Below that you will see an alphabetical list of examples. Look for the Sparkfun TB6612FNG Motor Library and select it.
There is only one example sketch included with the library called MotorTestRun. Open that sketch, it’s the one we will use for our robot car.
If you hooked up the Arduino and TB6612FNG as per the drawing above there is one change you will need to make to the sketch, this is because Sparkfun didn’t use the same Arduino pins as I did for the hookup.
Sparkfun used pin 2 for the AIN1 (AI1) connection whereas I used pin 3. So you can handle this two ways:
- Change line 28 of the sketch to read #define AIN1 3. This is the way I handled the difference.
- Keep the sketch as-is and just change the connection from pin 3 to pin 2.
Here is the sketch, modified to use pin 3.
TB6612FNG H-Bridge Motor Driver Example code
/******************************************************************************
TestRun.ino
TB6612FNG H-Bridge Motor Driver Example code
Michelle @ SparkFun Electronics
8/20/16
https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library
Uses 2 motors to show examples of the functions in the library. This causes
a robot to do a little 'jig'. Each movement has an equal and opposite movement
so assuming your motors are balanced the bot should end up at the same place it
started.
Resources:
TB6612 SparkFun Library
Development environment specifics:
Developed on Arduino 1.6.4
Developed with ROB-9457
******************************************************************************/
// 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 3
#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);
}
Using the Sparkfun library makes working with the TN6612FNG very simple. After defining all of the motor controller connections we setup two objects, one for each motor, named motor1 and motor2. Note how all the motor parameters are passed when creating the objects.
No need for anything in the Setup as the library takes care of defining the pins as outputs.
The Loop is essentially a demonstration of the functions the library provides. It is very well documented so you should have no problem adapting it to your needs. The functions include:
- Drive – This function sets the speed and direction of the motor (you apply it individually to each motor). You pass it the speed as either a positive or negative number, a negative number causes the motor to spin in the opposite direction. You may also specify the duration in milliseconds that you wish the motor to operate.
- Forward – This function allows you to drive both motors in a forward direction. It takes the speed as an optional parameter. Again a negative speed can be used, which will cause the motor to go backward.
- Back – As you might suspect this is the opposite of Forward.
- Brake – This stops the motor(s). If you didn’t specify a duration for the previous commands then you’ll need to use this to bring the robot to a halt.
- Left – This takes both motors as parameters and causes the robot to turn left. It also uses the speed as a parameter.
- Right – The opposite of Left of course!
The code in the Loop just exercises the robot car using a number of these functions. It actually is designed to have the car end up in the same place, but my ugly robot car is so unbalanced it never had a chance of doing that. Perhaps you’ll do better with your car!
You can experiment with the code and change or add more functions to control your car.
Conclusion
The TB6612FNG is an inexpensive dual H-Bridge motor controller that is easy to use and that can directly rep[lace the L298N in many applications. It’s small size and low heat dissipation makes it ideal for small robot cars and other battery-powered equipment.
You’ll be seeing a lot more of this little board in future projects. And if you know anyone who needs a drawer full of L298N’s please let me know!
Related
Control Large DC Gearmotors with PWM & ArduinoMarch 2, 2019
In "Arduino"
Controlling DC Motors with the L298N Dual H-Bridge and an ArduinoMarch 11, 2017
In "Arduino"
Build a Real Robot - Part 5 - Base ElectronicsApril 4, 2019
In "Build a REAL Robot"
'사물인터넷' 카테고리의 다른 글
Analog Feedback Servo Motor (0) | 2021.07.04 |
---|---|
Arduino High-Current Interfacing – Transistors & MOSFETs (0) | 2021.07.04 |
Color Sensing with Arduino (0) | 2021.07.04 |
Measuring Temperature with Arduino (0) | 2021.07.04 |
Multiple I2C Buses with an Arduino (0) | 2021.07.04 |