MOTORS BASICS (Finally!): HOW TO USE AN H BRIDGE
by gearsngenes in Circuits > Arduino
59465 Views, 646 Favorites, 0 Comments
MOTORS BASICS (Finally!): HOW TO USE AN H BRIDGE
Hello, followers and makers alike. It's been a while and I haven't posted much. BUT...I now have something to discuss with all of you. Motors. They are very useful, but what would make them more effective is if we knew how to make them change direction without touching the wiring. How would we go about doing that? Hm....
PS: IF YOU DO NOT HAVE ANY EXPERIENCE FROM PREVIOUS LESSONS, PLEASE, PRETTY PLEASE SEE THEM NOW!!!!!!!!!!!!!!! Thank you. <3
MEET THE H-BRIDGE
This bizarre chip is called an h-bridge. H-bridges come in all sorts of sizes and capabilities. But for the time being, lets stick with this basic version called the TB66. This chip can drive two mediocre sized motors simultaneously, but all we will be focusing on at the time is how to control 1 motor.
As you can see, there are many pins you might not be familiar with. Do not worry. We will dissect and understand this chip so that way you can access all of its capabilities.
The Powers and Grounds
Like most projects we have worked with, the TB66 has powers and grounds. To begin your understanding of the infamous TB66, lets see how we use these terminals.
First, you need a ground and 5v power (top left). These will be used for logic power, or power for the actual chip controlling the circuit.
Then you need a second ground and a power terminal that hooks up to a battery (bottom left). These are what will be used to power your motors, when they are actually turning. For the battery you need to use, I have found that using one 9v battery or six AA batteries are the most preferable when using this H-bridge.
Then there is the manual switch (right, middle) and a ground. What the manual switch allows you to do is force your circuit to turn off, not allowing the motors to move unless the Manual switch gets 5V. Since the circuit that we will be focussing on does not require this, we will supply 5v to the man. switch.
Inputs and Outputs
The pins you see labeled above are what will be controlling how your motors (in this case, only one) move.
PWMA is the pin that, depending on how much signal you send to it, indirectly controls how fast your motors move. For instance, if one sends 3.3 v (2/3 of 5v) to PWMA, then the motors will move to the extent that fraction of the battery's voltage (9v), which would be around 6v.
Ain1 and Ain2 are the inputs, from the arduino**, control the direction of the motor. When using them in code, one of these inputs have to be written as LOW, and the other HIGH* when telling the motor to move. However, when telling the motor to stop, both inputs have to be LOW. These inputs don't actually send power to the motors (it wouldn't be enough) but rather represent which terminal on the motor is power and ground.
Ao1 and Ao2 are the pins that you will connect your motors terminals to. These are outputs that send the power from the battery. The way that the power is given to a motor depends on how the Ain1 and 2 are told to be. If Ain1 is LOW, then Ao1 is LOW and the same holds true for Ain2 and Ao2. Again, one output has to be LOW while the other is HIGH in order for the motor to move.
*BAD JOKE ALERT: High on ELECTRICITY!
**Arduino: the chip we will be using to control the TB66
Which Pins Correspond to Which?
Here is another way one can view the way that these input pins and output pins interact, inspired by bcraun:
Motor Pin-------------------OUTPUT------------------INPUT--------------------Arduino Pin
Motor Terminal+---------------AO1-----------------------AIN1--------------------------Pin 3
Motor Terminal - ---------------AO2-----------------------AIN2--------------------------Pin 4
OR
Motor Pin-------------------OUTPUT------------------INPUT--------------------Arduino Pin
Motor Terminal- ---------------AO1-----------------------AIN1--------------------------Pin 3
Motor Terminal + --------------AO2-----------------------AIN2--------------------------Pin 4
See bcraun's account @:
Corresponding Pins on the Arduino
Those familiar with my previous lessons know that the microcontroller we use is an arduino called pro Trinket (5v version).
This is the device that we will be programming and controlling the motors with. On the diagram above, I have labeled the pins on the arduino that correspond with the pins on the TB66.
Take your time in observing and noting. For our Inputs, we will be using pins 3, 4, 5.
It is now time to start constructing our circuit.
The Parts Needed
To start constructing your circuit, you will need the following:
-One fully charged 9v battery
-One TB66
-One Pro trinket (5v version!!!)
-Wires to connect your parts together with
Done? Yeesh, you're fast! Lets start working.
Connect the Powers
First things first, connect the essential grounds and powers. One thing to keep in mind, and this is very important:
CONNECT ALL THE GROUNDS TO A COMMON GROUND (as seen above), OR YOU WON'T HAVE A COMPLETE CIRCUIT!!
But don't connect powers of different voltages (ex. the 5v pin and Battery pin), or else you are going to be Mr. (or Ms.) Cranky.
Like seen in previous diagrams, connect the manual switch and 5v pins to the 5v pin to the arduino.
As mentioned earlier, connect all grounds to a common ground, including the ground terminal of the battery and the ground pin of the arduino.
Connect the battery's power to the associated pin. In real life, that pin will be labeled as Vmot.
Connect the Inputs and Outputs
Connect the arduino controls as followed:
Pins 3 and 4 on the arduino will act as our pins to connect to Ain1 and Ain2, respectively.
Pin 5 on the arduino will act as our PWMA. The speed you want your motors to be able to move at is completely your own decision.
And for the last step, connect your motor's terminals (ground and power) to Ao1 and Ao2, which will change relative to the inputs. The way you connect your motors terminals to the outputs does not matter, as long as those outputs correspond to the correct inputs.
And there you have it. You have successfully finished your own H-Bridge circuit! Welcome to the beginning of basic robotics!
Writing the Code
For my fellow viewers and followers, we all know that in order to get the circuit to actually do anything, we have to code the circuit to do something.
Those who do not know how to do basic coding, click on the following link to see how to code an LED to blink:
https://www.instructables.com/id/Lesson-4-Basic-Ard...
For those who are curious for the code now...it's down below. Please read the built in comments to help your understanding.
Beginining
void setup() {
pinMode(3, OUTPUT); //This is our AIN 1
pinMode(4, OUTPUT); //This is our AIN2
pinMode(5, OUTPUT); //This is our PWMA
}
void loop() {
digitalWrite(3, HIGH); // If you change this to LOW, then change the line below to HIGH
digitalWrite(4, LOW); //
analogWrite(5, 100); // *
}
END.
*When you say analogWrite in arduino, you are giving an integer of 0 to 255 after the coma. Divide this number by 255, and you get the fraction of the battery you are using to power your motors. In this case, 100/255 is roughly .39, or 39% of the battery's voltage will be sent to the motors. So if you are using a 9 V battery, 3.52 V will be used.
Feel free to experiment with the code and manipulate the speed of the motor.
For Other Tutorials
For those curious in continuing, I have made tutorials on building professional robots requiring the TB66 circuitry. Code for these codes can be found on my github account under Gears n' Genes. Please do not feel afraid to check them out and make your own code and creations.
Check it out here:
https://github.com/gearsngenes/IRDriven2WheelRobot
Don't forget to favorite this instructables and follow me for more lessons to inspire yourself to make creative projects.