Pages Menu
Categories Menu

Posted by on Jun 15, 2011 in Atmel AVR, Microcontrollers | 89 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
    // 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.


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 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


  1. hello sir i want to make a robot for automatically control by ir sensor plz give me code for that how i move that through ir sensor and that could sense the object and find the path

    • Its your project. Don’t you think it is your responsibility to write code for it?

  2. plz sir tell me how 2 or more input signals are multiplexed through programming
    want to like this type of promming….
    if two i/p’s are high then how i multiplexed them

    • I don’t think I understood your question. Could you please be more clear and provide some more details. Thanks.

Leave a Reply

%d bloggers like this: