This is part 3 of my “Building Robots using Arduino” tutorial series, explaining how you can create robots using Arduino.
In this article, I will introduce the H-bridge IC L293D and we will also see how we can use it to control the speed of the motor.
Last week, I introduced DC Motors and explained how DC motor works and how we can connect it to Arduino.
Towards the end we also found that controlling DC motors directly using Arduino digital pin is not that efficient, since we can only give Arduino’s 5V to the motor.
The basic problem in using Arduino’s digital pins to control the motor directly is that it is very difficult to reverse the voltage.
To overcome this, we can use a circuit called H-bridge, which enables a voltage to be applied across the motor in either direction. We can either build the circuit ourself or use a pre-built IC. L293D is one such IC which is commonly used as a H-bridge. There are also other IC’s like L298 etc, but in this tutorial, we will see how we can use L293D.
I will probably will cover the other IC’s some time later in a separate post.
If you look at the datasheet of L293D IC, you will find that we can control two motors simultaneously using the IC. Even though the IC operates at 5V, it can still give a higher voltage (up to 36V) to the motor.
The following is the pin diagram of the IC.
The following is the explanation for the different pins of the IC.
- Vcc1 (pin 16) takes in regulated 5V for operating the IC.
- Vcc2 (pin 8) takes the external voltage (up to 36V) which
- 1,2 EN (pin 1) and 3,4 EN (pin 9) are the enable pins for the two motors
- 1A (pin 2) and 2A (pin 7) are the control pins for Motor 1. These will be connected to Arduino.
- 1Y (pin 3) and 2Y (pin 6) are the output pins for Motor 1. These will be connected to the first motor.
- 3A (pin 10) and 4A (pin 15) are the control pins for Motor 2. These will be connected to Arduino.
- 3Y (pin 11) and 4Y (pin 14) are the output pins for Motor 2. These will be connected to the second motor.
- pins 4,5,12,13 are Gnd pins
Connecting H-bridge with Arduino
As I explained above, we will be connecting H-bridge to Arduino, using the following connection.
- 1,2 EN (pin 1) goes to Arduino digital pin 10
- 1A (pin 2) goes to Arduino digital pin 8
- 2A (pin 7) goes to Arduino digital pin 9
- 3,4 EN (pin 9) goes to Arduino digital pin 11
- 3A (pin 10) goes to Arduino digital pin 12
- 4A (pin 15) goes to Arduino digital pin 13
- 1Y (pin 3) goes to one terminal of motor 1
- 2Y (pin 6) goes to another terminal of motor 1
- 3Y (pin 11) goes to one terminal of motor 2
- 4Y (pin 14) goes to another terminal of motor 2
- Vcc1 (pin 16) goes to 5V pin in Arduino.
- Vcc2 (pin 8) should be connected to the +ve terminal of your battery.
- Connect the -ve terminal of your battery to one of the Gnd pins (4,5,12 or 13)
- Connect Gnd from Arduino to one of the Gnd pins (4,5,12 or 13)
Note: Apart from the two enable pins, you can connect other pins to any other digital pins in Arduino. Next week, I will explain why we should connect the enable pins to only pin 10 and 11. Also, we might need some small capacitors across the power lines to remove noise. I have not added them, to keep the circuit simple and easy to understand.
Controlling directions of DC Motors
As you can see from the above pin diagram, each motor has 3 pins (1 enable pin and 2 control pins) through which we can control the motor. The logic to control the direction of the motor is as follows.
- If the enable pin for a motor is HIGH, then the motor will run.
- If control pin 1 is HIGH and control pin 2 is LOW, then the motor will rotate in one direction.
- If control pin 1 is LOW and control pin 2 is HIGH, then the motor will rotate in the other direction.
- For all other cases, the motor will not run.
The logic can also be expressed in the following tabular form
|Enable Pin||Control Pin 1||Control Pin 2||Motor status|
|H||H||L||Rotates in one direction|
|H||L||H||Rotates in another direction|
|H||H||H||Does not rotates|
|L||Doesn’t matter||Doesn’t matter||Does not rotates|
I have written a small Arduino sketch to explain this. You can find the full sketch at github.
This will rotate both the motors in one direction for 10 seconds and then will change their direction for the next 10 seconds.
Here’s a video of it.
What’s the fun in just doing what I am doing? After trying out this sample code, expand it to do the following
- Run both motors in the same direction
- Reverse the direction of both the motors
- Run both motors in different direction
- Stop one motor and run the other one.
Once you have completed any or all the above, let me know by posting a comment below or in twitter and include @hardwarefun.
Next week we will see how we can control the speed of the motor. Also, I will talk about how we can abstract the code into a library so that we can reuse it every where.
Till then, happy roboting 😉