DC Motor Control using AVR (l293d)

August 17, 2009 by admin
Filed under: Microcontrollers 

DC motor direction Control Using L293d and AVR


Hello all electro friends…

This is another tutorial for microcontroller interfacing series. This post is all about how to interface/control a simple DC motor using microcontrollers. Controlling a DC motor is nothing but controlling the direction and speed of a motor. It is very necessary to go through motor controlling concept, if you are designing an autonomous robot.

How DC Motor works ???

Lets start with how actually DC motor runs. Direction control of a DC motor is very simple, just reverse the polarity, means every DC motor has two terminals out. When we apply DC voltage with proper current to a motor, it rotates in a particular direction but when we reverse the connection of voltage between two terminals, motor rotates in another direction.

motor

Controlling using Micro Controllers !!!

I think you are now familiar how to change the direction of DC motor. Now let us consider how to control motor using Microcontroller provided:

1. Microcontroller provides us only digital logic (1 or a 0).

2. We cant provide polarity from microcontroller.

3. We cant connect motors to Controller as mostly motors runs on volatage higher that +5V, and motors demands high current (depends).

Now the solution to above limitations is use of a “H Bridge”.

basic-bridgeIt is a circuit which allows motor rotation in both directions. From four terminals of H bridge you can control a DC motor.

Using L293d Dual half H Bridge …

We can make our own H bridge using transistors but it will be better if we use a ready made IC named as L293d, its a dual half H brige IC.

ckt_mtr

Motors can be driven in clockwise direction or anti-clockwise direction according to our requirements. To do so we first need to connect motors and find out the commands we need to give to our microcontroller to perform the specific rotation.

Suppose we connect a DC motor to PORTC of AVR through L293d. First of all we need to connect L293D to PORTC. Lets use the bits C7 and C6 for connecting this motor, as shown in the diagram below.

l293d

Code WinAVR-GCC

Here is the code in WinAVR to drive this motor.

#include<avr/io.h>

#include<util/delay.h>

int main(void)

{DDRD=0x00;     // PORTD declared as input port

PORTD=0xFF;    // PORTD Pull Ups enabled initialized

DDRC=0XFF;       // PORTC declared as output port

PORTC=0x00;     // PORTC initialized

while(1)

{

if(!(PIND&0x01))            // PIN D0 is used as input

{

_delay_ms(500);    // Debounce time

while(!(PIND&0x01));

PORTC=0b00000010;       // motor rotates in one particular direction

}

else if(!(PIND&0x02))      // PIN D1 is used as another input

{

_delay_ms(500);  // debounce time

while(!(PIND&0x02));

PORTC=0b00000001;        // motor rotates in another direction

}

else

{

PORTC=0x00;           // Stop motor

}

}

}

we can drive a maximum of two DC motor and One stepper motor using  one L293d.

Decision Table will look like

IN1     IN2                  Motor1

0           1                      Rotates in one direction

1            0                    Rotates in other direction

Similar is true for another motor connected to Out3 and Out4 of L293d and can be controled through IN3 and IN4.

This is all about controlling direction of DC motor using L293d and AVR. Next we will try to explain Speed control using PWM.

For any kind of query or suggestion tell me :  sonal@electroons.com

Downloads-

L293d Datasheet

AVR code

Comments

4 Comments on DC Motor Control using AVR (l293d)

  1. chandan on Sat, 31st Oct 2009 4:39 pm
  2. well but how to control the speed of motors??have u explained it anywhere?the above one is the simplest one.plz illustrate the programming of controlling the speed of dc motors..

  3. prashant on Tue, 12th Jan 2010 7:00 am
  4. can u tell me anything about how to move it in more than two directions stated above. lyk can it move in left right directions as well? n to how much degree.. if not pl can u suggest something?

  5. Ebenezer on Tue, 19th Jan 2010 6:33 am
  6. i dont who u r , i hope u vil help me out !!!.. plz tell me hw to make a circuit for it…
    i’ve attached a file….. hw to make a line follower for it… plz help in this as soon as poosible..
    i’m very weak in electronics plz help me in this… i hope u vil help me out

    my mail eby.jery@gmail.com

  7. putri on Mon, 25th Jan 2010 10:03 pm
  8. hay.. im putri, do u have a basic compiler program for speed control dc motor using l293d? plzz help me..
    i’m a girl, so i’m not really good in electronic

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





Anti-Spam Protection by WP-SpamFree