Arduino: H-Bridge IC
This post will cover how to use an H-Bridge integrated circuit (IC) to reverse the direction of the motors on my robot car. I will also take advantage of the new motor control to improve my obstacle avoidance algorithm. Here is a video showing you what you will have by the end of the tutorial:
Material
For this tutorial, you will need:
- 2WD Robot Car with Ultrasonic Distance Sensor, built in my last posts.
- Texas Instruments L293NE H-Bridge: The Arduino Starter Kit comes with one included.
- Arduino Time Library: Code library.
To understand a little about what an H-Bridge is, I read through this lab.
H-Bridge IC
Because my goal is to reverse the direction of the robot car, I need to reverse the direction of the current in both motors. This can be done by using the L293NE, which has two bridges (the left and right side of the chip). Each pin has a specific funtionality, which is described in this diagram taken from the NYU lab:
Given this diagram, 6 I/O pins are needed:
- 2 I/O pins for each motors’ enable pin
- 4 I/O pins for each motors’ 2 control pins
Because I/O pins 11
and 12
are reserved for the ultrasonic distance sensor, I chose I/O pins 5
through 10
to control the motors. Going through the IC’s pins one-by-one, here is how they are connected in my setup:
- Right motor’s enable pin, I/O pin
7
. - Right motor’s control pin 1, I/O pin
5
. - Right motor’s Terminal 1, hooked up to one side of the right motor.
- Not connected to anything.
- Not connected to anything.
- Right motor’s Terminal 2, hooked up to the other side of the right motor.
- Right motor’s control pin 2, I/O pin
6
. - Both motors’ power supply, hooked up to the 9V battery
+
column on the breadboard, e.g. not the Arduino’s power supply. - Left motor’s enable pin, I/O pin
8
. - Left motors control pin 1, I/O pin
10
. - Left motor’s Terminal 1, hooked up to one side of the left motor.
- Not connected to anything.
- Both motors’ ground connection, hooked up to the ground on the 9V battery
-
column on the breadboard, e.g. not the Arduino’s ground. - Left motor’s Terminal 2, hooked up to the other side of the right motor.
- Left motor’s control pin 2, I/O pin
9
. - IC power, connected to the
+
column that connects to the Arduino’s ground.
Here are two different views, from the side and from the back, that go with the description above:
Reversing Direction
Supplying voltage to one terminal of a motor will make the motor spin in one direction, while supplying voltage to the other terminal of the motor will make the motor spin in the opposite direction. If both terminals receive voltage, the motor will go forward. Therefore, you must find out which Terminal, when receiving voltage, makes your motor go backwards. After uploading the code below, if one of your motors is running backwards when it should be running forwards (or vice versa), it’s because the terminals were connected differently. If you experience this, just switch the Terminal 1 connection to the Terminal 2 connection and it will be fixed.
To reverse both motors, we must digitalWrite(LOW)
to the same Terminal for both motors. In my case, it is Terminal 1 (I/O pins 5
and 9
for the right and left motor respectively) while HIGH
and Terminal 2 (I/O pins 6
and 10
for the right and left motor respectively) while LOW
that cause both motors to go in reverse. Here is the code to do this:
At any point in time, if the enable pin of a motor is LOW
, the motor will turn off, going neither fowards nor backwards. This example stops the car:
Improved Obstacle-Avoidance Algorithm
Now that I have the ability to reverse the direction of the motors, I can improve upon the obstacle-avoidance algorithm in the last post.
Conclusion
After uploading the code to the robot, or watching the video above, we can see that it is definitely an improvement on the last algorithm we had. The ability to back up really gives some breathing room to the car in order for it to find empty space more easily. However, the random direction can cause problems if it is close to a corner. To solve this issue, I am going to mount the ultrasonic distance sensor on a Servo motor, allowing the car to look left and right in order to make a decision on which way to turn.