DC Motor Control using AVR MCUs

Motor gives power to your MCU. Ya power to do physical works, for example move your robot. So it is essential to know how to control a DC motor effectively with a MCU. We can control a DC motor easily with microcontrollers. We can start it, stop it or make it go either in clockwise or anti clock wise direction. We can also control its speed but it will be covered in latter tutorials.

A Geared DC Motor.

DC Motor

A DC motor is electromechanical device that converts electrical energy into mechanical
energy that can be used to do many useful works. It can produce mechanical movement
like moving the tray of CD/DVD drive in and out (you may like to try it out Go
to My Computer, right click the drive icon and click “Eject”). This shows how
software controls a motor. DC motors comes in various ratings like 6V and 12V.
It has two wires or pins. When connected with power supply the shaft rotates.
You can reverse the direction of rotation by reversing the polarity of input.

Control with AVR MCUs

As the MCUs PORT are not powerful enough to drive DC motors directly so we need
some kind of drivers. A very easy and safe is to use popular L293D chips. It is
a 16 PIN chip. The pin configuration is as follows.

L293D Dual DC Motor Controller.

This chip is designed to control 2 DC motors. There are 2 INPUT and 2 OUTPUT PINs for each motors. The connections is as follows.

Fig: Motor Controller Using L293d chip.

The behavior of motor for various input conditions are as follows

A

B

Stop

Low

Low

Clockwise

Low

High

Anticlockwise

High

Low

Stop

High

High

So you saw that you just need to set appropriate levels at two PINs of the microcontroller
to control the motor. Since this chip controls two DC motors there are two more
output pins (output3 and output4) and two more input pins(input3 and input4).
The INPUT3 and INPUT4 controls second motor in the same way as listed above for
input A and B. There are also two ENABLE pins they must be high(+5v) for operation,
if they are pulled low(GND) motors will stop.The following program starts the
motor runs it one direction for some time and then reverses the direction.

Downloads

Speed Control

The speed of DC motor can also be controlled with MCU. PWM or pulse width modulation technique is used to digitally control speed of DC motors. I will show you how that is done in later tutorials after I introduce you with internal TIMERS of AVR because PWM is generated using timers.

Hello Avinash Sir,
I want to ask one simple question…
Why cann’t we use ULN2803 in place of L293D to drive DC motor??…ULN 2803 has also current specification of 600mApms…Is anything special in L293D?
Plz do reply.
Thanks

ULN2803 is darlinton array while L293 is a push pull driver. ULN can only sink current cannot source it, so it cannot be used in bi-directional control of motor. By the way this site is for engineers not KG students

Hey Avinash , Great tutorial I must say , but I wanted to ask that what would the program ( C ) look like if I had to control 2 motors using the L293D using the same logic.? ( And I mean controlling the motors simultaneously) What ports should I use for the second motor?

thank you avinash sir,this is a great website for beginners,hope you keep coming up with such helpful and self contained tutorials .god bless you sir.sir, can you please suggest online website to become perfect in c programming.i will be greatful to you

Great site, really motivated me to get back into programming. i would like to get the code for
the SMS project, but i dont have Facebook (sorry, im too old). any chance i might be able to get the code?