Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Jun 15, 2011 in Atmel AVR, Microcontrollers | 72 comments

DC Motor Control using AVR

DC Motor Control using AVR

AVR SeriesHello guys. In the field of robotics, we use different types of motors – DC motors (mostly geared), servo motors, stepper motors, etc. In this post we will discuss how to control DC Motors (geared or gearless) using a MCU.

DC Geared Motor

DC Geared Motor

Most DC motors are normally very easy to reverse. By simply changing the polarity of the DC input, the direction of the drive shaft reverses. This property makes DC motors very popular among enthusiast people involved in robotics. In most cases, DC geared motors are used.

The changeover process (reverse in direction due to reverse in polarity) can be achieved via a simple changeover switch (DPDT switch) or for a remote or electronic control, via a suitable relay. For more information on changeover process, view this page. However, when we use MCU in our circuit, we don’t need a relay. The necessary control signals will be generated by the MCU. This signal will be passed to a Motor Driver IC, which in turn drives the motors. The following block diagram shows this process.

Why use a Motor Driver?

Block Diagram - Motor Control

Block Diagram – Motor Control

Block Diagram Explained

In the above block diagram, we can see that there is a microcontroller (MCU). Now, this MCU may/may not take in inputs (inputs as in from sensors, other digital inputs, etc). Next, as per our programming, the MCU will generate control signals. Please note that the MCU will generate signals in form of HIGH (Vcc = 5v) or LOW (zero). But this voltage is insufficient to drive a motor. That’s why we need to use a Motor Driver.

A motor driver always has a battery input Vs (which depends upon the rating of the motor). In simple terms, what a motor driver does is that it directs the Vs voltage to the motors connected (or in fact, the output pins) to it. Thus, the motors behave as per the control signals generated using the MCU with the excitation from the external battery voltage.

L293D DIP16 Package

L293D DIP16 Package

The most commonly used motor driver is the L293D. I have also found some people who look for its replacement. For them, SN754410 should do. Both are pin-to-pin similar to each other, quad Half-H-Bridge Motor Drivers, capable of driving high voltage motors using TTL 5V logic levels. They can drive 4.5V up to 36V at 1A continuous output current!

L293D Connections

L293D is a 16 pin IC which comes in a DIP Package. Its pin configuration is shown below.

L293D Pin Configuration

L293D Pin Configuration

Now let’s have a look at its connections for bidirectional motor control.

L293D Based Motor Driver

L293D Based Motor Driver

In this way, we can have bidirectional control over two motor. Let’s have a summary of connections:

  • There are two enable (EN) pins, pin 1 and pin 9. Pin 1 EN enables the motor M1 whereas pin 9 EN enables motor M2.
  • Connect motor M1 across OUTPUT1 and OUTPUT2 i.e. across pins 3 and 6.
  • Connect motor M2 across OUTPUT3 and OUTPUT4 i.e. across pins 11 and 14.
  • The inputs for motor M1 is given across INPUT1 and INPUT2 i.e. across pins 2 and 7.
  • The inputs for motor M2 is given across INPUT3 and INPUT4 i.e. across pins 10 and 15.
  • Connect GROUND pins 4, 5, 12 and 13 to ground.
  • Connect pin 16 to Vcc (=5V) and pin 8 to Vs (battery, 4.5V~36V).

As per the diagram, the inputs of motor M1 are M1-A and M1-B, whereas inputs of motor M2 are M2-A and M2-B.

Now consider the following cases for motor M1:

  • M1-A = 1 and M1-B = 0 → M1 moves clockwise (say). Then
  • M1-A = 0 and M1-B = 1 → M1 moves counter-clockwise.
  • M1-A = 0 and M1-B = 0 → M1 stops.
  • M1-A = 1 and M1-B = 1 → M1 stops.

Similar cases can arise for motor M2:

  • M2-A = 1 and M2-B = 0 → M2 moves clockwise (say). Then
  • M2-A = 0 and M2-B = 1 → M2 moves counter-clockwise.
  • M2-A = 0 and M2-B = 0 → M2 stops.
  • M2-A = 1 and M2-B = 1 → M2 stops.

Suppose if you need to control only one motor at a time, you need to enable that particular EN pin. Enabling both pins at the same time will drain your battery unnecessarily.

Motor Control Using AVR

Now let’s generate control signals from the AVR MCU and feed them to the inputs of L293D. Assuming ATMEGA32, let us connect L293D across PORTC pins (PC0…PC3) as shown in the diagram below.

AVR - L293D Connection

AVR – L293D Connection

Now open up AVR Studio 5, type the following code and build it. If you are new to AVR Studio 5, you can read this post to get started with it. The code can also be found on pastebin and in the code gallery.

#include <avr/io.h>
#include <util/delay.h>      // for _delay_ms()
 
int main(void)
{
    DDRC = 0x0F;            // initialize port C
                 // motors connected across PC0...Pc3
    while(1)
    {
    // clockwise rotation
    PORTC = 0b00000101;     // PC0 = High = Vcc
                    // PC1 = Low = 0
                    // PC2 = High = Vcc
                    // PC3 = Low = 0
 
    _delay_ms(500);         // wait 0.5s
 
    // counter-clockwise rotation
    PORTC = 0b00001010;     // PC0 = Low = 0
                    // PC1 = High = Vcc
                    // PC2 = Low = 0
                    // PC3 = High = Vcc
 
    _delay_ms(500);         // wait 0.5s
    }
}

After burning the code into your MCU, you will find that the motors rotate in clockwise direction for 0.5s and counter-clockwise direction for 0.5s. This goes on continuously.

Video

This is a simple demonstration of controlling two DC motors using a single L293D IC and a microcontroller. The video is made by Lavin Khandelwal for maxEmbedded. He has used the low cost 28 pin AVR Development Board and the USBasp AVR Programmer by eXtreme Electronics. He used the eXtreme Burner for burning the code.

In this way, you can control DC motors using AVR. For any kind of queries, clarifications or suggestions, you can use the comment box below!

Max

Max is the founder and admin of maxEmbedded. He describes himself as an 'embedded electronics freak' and an Apple/Linux fan. He likes to experiment, learn and share new things in this field. Furthermore, he likes to write stuffs on his website for techie newbies and loves to teach others. In his spare time, you will find him with volunteering somewhere!

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle Plus

72 Comments

  1. hey Mayank,
    I am working on stepper motor. I’ve purchased one. It is hybrid. But, it has 10 wires (mostly, I heard about 4,6,8 wires). Actually, I don’t know how to use them (no wire specification). Can you help me on this?? How can we check it is unipolar or not?

    Thanks in advance!

    • Hi Amit,
      Well, I haven’t worked with such stepper motors before, so I may not be able to help you out here. The point is, stepper motors can have more number of pins as well. More number of pins implies that it has more number of magnetic cores, which means better resolution. I would suggest you to look out for datasheets for similar motors online. I googled and found quite a few, so you may want to check it and see which one matches your motor.

  2. i am just start to learn about m.cu. kindly guide me which book i reffer.is there is any default program in it.

    • I wouldn’t suggest any book for learning microcontrollers. Look out for help online. Like maxEmbedded, you can find several other resources online to help you learn about microcontrollers.

  3. is it possible to control a single motor using l293D.

    • Oh yes, it is, Yeshwanth! Go ahead, try it out! :)

  4. i want the code of the stop the motor no delay

    • Hello muatez,
      Sorry, but we are no genie who can fulfill everybody’s wish. We don’t write codes for anyone. We teach the concept and empower you to write code yourself. If you could explain us in more detail what your requirements are, we could tell you a possible method. Thanks.

  5. sir,
    do you have a tutorial for stepper motor ? or will it publish soon on your web site.
    thanks

  6. Hey mayank,your post is awesome…I just want to ask you that if we can change the direction by just reversing the polarity,then why we need ATMega16 microcontroller??

    • How would you reverse the polarity then?

  7. hello sir
    after connecting all terminals my motor is stop working after 20-30 sec
    and also current is not coming properly

    i have connected l293d to portd
    my connection are
    i/p 1 = pd4
    i/p 2 = pd2
    i/p 3 = pd6
    i/p 4 = pd5
    enable 1 = pd3
    enable 2 = pd7

    my programming is http://pastebin.com/JbVkjWM6
    pls help sir

    • Divyam,
      I am surprised that the motors are even running for 20-30 seconds! You haven’t specified anywhere in your code that PORTD should be an output port. Change DDRB to DDRD in your code and try again.

      • sir sir programming is wrong
        i give output to the 0xff to port b but still not running properly

        some time it run somtime not
        pls help

        • Okay, so this is a good time to take out your multimeter and start debugging! Check whether the voltage across all the pins of PORTB are as you expect. Also, try changing to some other port to see if it works. Were you successfully able to blink an LED before?

  8. sir i interfaced lcd with lm35 and its working good now i want if temparature t>35 then moter on how i write code plz help….

    • Hasham,
      This is simple C programming. I hope you’re familiar with writing C based logic programs. If not, please get familiar with that first. You should not proceed with Embedded C unless you’re comfortable with programming in C.

  9. please help me how I can obtain L293D from lab center electronics

    • Go and buy it from the market!

  10. Hey!
    Awesome work!

    Its possible to control the voltage for a DC Motor with the L293D from 0 to 6 volts via PWM? Or will the PWM starts at 4,5 volts?

    Greets!

    • Hello Thomas,
      I have two questions for you:
      1. What makes you think it will start at 4.5 volts?
      2. What makes you think it should go up to 6 volts?

      And make sure you read my posts on PWM. Thanks!

  11. sir can i control/use dc motor for precise rotation?i mean can i have rotation upto 90degree only .is it possible ? i don’t want to use servo motors.

Trackbacks/Pingbacks

  1. AVR Timers – PWM Mode – Part I « maxEmbedded - [...] the motor will run at only 150 rpm. For more details regarding controlling DC motor using AVR, view this. Now …
  2. RF Module Interfacing without Microcontrollers « maxEmbedded - [...] If you are using it control a robot, the output data can be directly given to a motor driver. …
  3. Making an RF Car | maxEmbedded - [...] 4 input pins and 4 output pins. Though not required here, but in case you wish to learn how …

Post a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>