How to control a DC motor with an arduino and L293D H-bridge
Hi you all, :-)
Thank you for checking this new tutorial.
We will make a serie of tutorials on how DC motors with
You want to create a wheeled robot controled by an arduino uno board, control a DC motor with a variable speed and change the motor's direction. How do we usually do that? You can do it by changing the voltage between the motor pins but the torque will also change. And of course we want to keep our motor's torque to have effectiveness when moving our robot.
To do so, we'll use use our arduino board PWM "Pulse width modulation" (They are located on the pins: 3, 5, 6, 9 and 10 on the arduino Uno) and associate it with a H bridge (A L293 or a L293D). What is a H bridge? It's an association of 4 power transistors (NPN and PNP) and diode to protect the transistor. They may be MOSFETs or FETs (Those are popular for driven a lot of current: just what we need).
The speed variation is made by changing the duty cycle of our signal. What makes the average signal change ( It is called ''PWM'': Pulse width modulation). To make it easier, Texas instrument ''TI'' develloped a integrated circuit ''L293 and L293D" with all the components inside one chip (diodes, power transistors). We can even control 2 motors or more with that chip. A very good combination to make a robot from scratch.
It also allows us to change the rotation of our motor by activating 2 transistors inside the L293 at the same time (PNP and NPN transistors). So we can go clockwise or counter clockwise and also change our DC motor's speed since the arduino uno board got PWM outputs. The transistors act as a switch.
On the L293 chip, there are 16 pins. Among those are:
1,2 En: This pin on the L293 chip active the motor or not. If at VSS it's activated and if at GND, it's off. The PWM coming from the arduino uno is connected for the motor speed.
on this pin
GND: Need to be plugged to the power supply on GND
VCC: Need to be plugged to the power supply on VCC
1A, 2A: Commands to control the way our motor turns (clockwise or counter-clockwise)
1Y, 2Y: Those pins are where the motor is plugged
Materiels:
So what do we need in this tutorial on how to control a DC motor with an arduino and a L293.
To do so, you need to gather the following hardware:
3V battery or more depending of your DC motor voltage
Wires
1 breadboard (prototyping board)
1 L293D or L293
1 potentiometer
1 heatsink for the L293
A part of those components can be find on our online store ''makersgeneration online store'' or on sparkfun.
Schematics:
To start working with this project, you need to wire all the differents parts I have just enumerated before. To help you with that task, you can follow the next schematic.
Because of the power and the amperage consumed by the DC motor, you need an external power supply (a battery : 3V, 6 or 9V) for the dc motor. The L293D can furnish until 600mA per DC motor and is supplied with a voltage between 4.5V to 36 volts depending of your motor voltage.
Programming:
To program your arduino board. You need to open your Arduino IDE after downloading it from their website (arduino.cc). A tutorial can be find on their website explaining how to upload a program on their IDE.
When open, copy and paste the following code:
Program:
int a; // Variable declarion to increase and decrease the speed of our DC motor.
int Enable1 = 5; //PWM
int I1 = 12; // Motor's direction pin number 1
int I1_bis = 13; // Motor's direction pin number
void setup()
{
pinMode(Enable1, OUTPUT); // Pin to validate motor number 1 with the arduino PWM
pinMode(I1, OUTPUT); // pin to validate the motor's direction
pinMode(I1_bis, OUTPUT); // pin to validate the motor's direction
}
void loop() {
for(int a = 0; a<255; a++)
{
digitalWrite(I1, 1 );
digitalWrite(I1_bis, 0 );
analogWrite(Enable1, a); // We make our speed evolve from 0 to it's max speed. Our
PWM coming the pin numer 5 on our arduino
delay(50); // Wait 5s because increasing the speed in our loop
analogWrite(Enable1, 0 );
}
for(int a = 0; a<255; a++)
{
digitalWrite(I1, 0 );
digitalWrite(I1_bis, 1 );
analogWrite(Enable1, a); // We make our speed evolve from max speed to 0. Our
// PWM coming the pin number 5 on our arduino
delay(5000); // Wait 5s because increasing the speed in our loop
analogWrite(Enable1, 0); // stop the motor
}
}
Explanation:
This program allows us to increase and decrease automatically the DC motor speed with our PWM which in our case is ''analogWrite()". AnalogWrite() is one of he function which is in the Arduino native library.
AnalogWrite has 2 parameters like this one: "AnalogWrite(a,b)''. "a" being the pin on the arduino uno for the PWM and "b" being the speed we want our DC motor to revolve. We add a variable, "a" in our case that can go from 0 to 255. When It's at zero, our motor is stopped and at 255, it's the full speed.
Let me give you a little bit more understanding. When it's the half of 255 (~130), it's the half speed of the motor and so on if it's the quarter of 255 (~65), 1/8, 1/5....
When our program is uploaded to the arduino uno board, the motor starts from zero to full speed then decrease the speed until zero. Then it's start again from zero to the max speed but counter clockwise and then decrease the speed until zero to restart the cycle. You have now all the sketch of the program.
There are different combinationsto control the rotation of the DC motor with the L293 pins:
Enable1 Enable2 Motor's reaction
0 0 // Motor is stopped
0 1 // Motor turns clockwise
1 0 // Motor turns counter-clockwise
1 1 // Motor is stopped
Next steps:
You can modify the schematic or the program to fulfill your need. An example could be to add a potentiometer (variable resistor) to control the speed with the functions "analogWrite()" and analogWrite(). And even more.
If you do so , you can share them with us and the community your projects, your questions leave comments below and even leave a thumbs up if you like the tutorial and want more of them.
A video is waiting for you at the end of the tutorial.
For all of you that are more connected, you can subscribe to our newsletter on our website (makersgeneration.net) a the bottom or our social media (Facebook, Twitter, Google+, Instagram, Youtube).
Good tinkering and see you soon for another tutorial. ;-)