Friday 22 August 2014

Driving DC motors using L293D

DC Motor Basics:


                A DC motor has two terminal pins for supply. Direction of rotation of motor can be reversed by changing the polarities. This is depicted as follows,
 
Motors require high currents to operate. The current and voltage from a microcontroller’s output cannot drive a dc motor. Hence we require an external driving circuit.
A simple motor driving circuit is shown below.,


Diode 1 is used to prevent reverse current flow from the motor to the controller (Protection Diode). When the microcontroller’s output is logic HIGH or ‘1’, then the motor will start to rotate. However the circuit is for only unidirectional control of DC motor i.e, the motor rotates in only single direction and can’t be reversed. For this purpose we will be using a H – bridge.

H – bridge:




             When a logic HIGH or ‘1’ is applied (from microcontroller) at CW, then the transistors T1 and T4 will get shorted and current flows from Vcc - T1 – motor – T4 – Gnd, thus making the motor to rotate in clockwise direction.


On the other hand, when a logic HIGH or ‘1’ is applied (from microcontroller) at CCW, then the transistors T2 and T3 will get shorted and current flows from Vcc – T3 – motor – T2 – Gnd, thus making the motor to rotate in counter clockwise direction (reverse).


                When the input at both CW and CCW is logic ‘0’ or LOW, then no transistor gets shorted and hence no conduction of current from Vcc to Gnd. When the inputs at both CW and CCW is logic ‘1’ or HIGH, then all the transistors gets shorted and all the current goes from Vcc to Gnd through transistors without going through the motor. Hence motor will not rotate in neither directions.


CW
CCW
Output
0
0
Stop
1
0
CW rotation
0
1
CCW rotation
1
1
Brake / Stop


Interfacing with L293D:


                There are several custom made H bridge IC’s available in the market. Commonly used IC by electronic hobbyists is L293D.


Note: if you buy L293B, then you have to put protection diodes additionally, because there is no protection diode inside the IC. L293D has inbuilt protection diodes.

L293D pinout:




Here is the datasheet – L293D

L293D has the capacity to drive two DC motors bi-directionally (2 channels). You may consider IC’s left side pins (Enable 1, Input 1, Output 1, Output 2, Input 2) as channel 1 and right side pins (Enable 2, Input 3, Output 3, Output 4, Input 4).

  • Enable pin is to enable the corresponding channel (Normally 5V – max 7V).
  • Input pins receive logic HIGH or Low from the controller (Normally 5V – max 7V).
  • Output pins are connected to the motors.
  • Vss – logic supply voltage (Normally 5V – max 36V)
  • Vs – Motor supply voltage, as per motor specifications (Normally 5V – max 36V)
  • Gnd pins – All of these ground pins are connected internally and hence they need not be interconnected externally.

General Connection diagram:

 

 If you use a 5V DC motor, the pin 8 (Vs) should be connected to 5V and similarly for different motor ratings. Various colour wires have been used to differentiate between the connctions.

With GR-Sakura (Arduino Compatible):

 

Pins 5, 4, 7 and 6 of sakura board are used as L293D Input pins 1, 2, 3 and 4 respectively.


Pins 5 and 4 – Input 1 and 2 of channel 1 / CW and CCW of motor 1

Pins 7 and 6 – Input 3 and 4 of channel 2 / CW and CCW of motor 2

Since I have used two 5V DC motors, 5V from sakura is connected to Vs of L293D.

Code:


#include<rxduino.h>
main()
{
                pinMode(7,OUTPUT);
                pinMode(6,OUTPUT);
                pinMode(5,OUTPUT);
                pinMode(4,OUTPUT);
                while(1)                                   // repeating / infinite loop
     {
                                digitalWrite(5,1);     // rotates motor 1 Clockwise
                                digitalWrite(7,1);     // rotates motor 2 Clockwise
                                digitalWrite(4,0);   
                                digitalWrite(6,0);
                                delay(5000);             // After 5 seconds
                                digitalWrite(5,0);
                                digitalWrite(7,0);
                                digitalWrite(4,1);     // rotates motor 1 Counter Clockwise
                                digitalWrite(6,1);     // rotates motor 2 Counter Clockwise
}
}



Feel free to comment regarding doubts...............

1 comment:

  1. Where should I connect ir sensor .in arudino plz reply soon

    ReplyDelete