Controlling DC Motors using AVR Microcontrollers

This tutorial provides step-by-step instructions on how to use an AVR microcontroller (in this example an ATTiny2313) to control two DC motors using L293 motor driver.

While the microcontroller can act like a brain through software and control signals from its input/output pins, it lacks muscles. Muscles to the “brains” are provided by motors, actuators and other electromechanical drives.

The YouTube video is here:

What are the basic circuit building blocks

While driving DC motors, we need to consider the following:

  1. Microcontroller can provide signals to start, stop, forward or backword movement, but cannot provide the power needed to run the DC motors.
  2. To run DC motors, we need an interfacing circuit, commonly called as “driver”. It sits between the microcontroller and the DC motors. Its prime job is to accept signals from the microcontroller and then translate those signals into a high power circuit (meaning, microcontroller signals could be 5V, 20 mA while the output from the driver could be at 12V 300mA)
  3. The power supply for microcontroller is different from the power supply for the motors. There exists a common ground between the two to have a common reference point.

The whole idea can be outlined as given in the picture below:

DC Motor Control Block Diagram

DC Motor Control Block Diagram

Each of the blocks now needs a circuit. In my case, the translation is given below:

  1. Microcontroller – I used the “bare-bones” development board using ATTiny2313. Details of the board can be found either at WordPress link
  2. The driver circuit is using L293D IC. This is a motor driver IC that uses an H-bridge (H-bridge on Wikipedia) You can driver 2 DC motors using a single IC if you want bi-directional control. You can look at the datasheet for this IC at ST-Microelectronics
  3. Two different power supplies. For the microcontroller, I used a simple regulated 5V power supply. You can check the details on this power supply at WordPress link
  4. The power supply for the DC motors can be another regulated power supply of higher voltage and current rating. This depends on the rating of your motor. I used two different kinds of power supplies. Before I connected the main motors, I used very small DC motors that can run on 5V and < 100mA current. After initial testing, I plan to power the motors using LiPo batteries. Normal alkaline cells are not good due to high internal resistance.

The details on generating motor control signals

L293 can drive two motors with bi-directional control or 4 motors in single direction. Most hobby robotics would require bi-directional control. So let’s focus on that. I’ll explain how to drive one motor forward and reverse. You can extend the concept to drive the two motors.

First, let’s understand the pins of L293D. Before that, try to get L293D which has clamping diodes built in. This protects the circuit from back-EMF. If you cannot get the D version, get a L293 and solder clamping diodes. Please note, these have to be switching diodes with rating higher than what the motor can handle and not normal rectifier diodes (e.g. 1N4001)

The pinout of L293D is shown on the right.

The pins are explained below:

L293D Pinout

L293D Pinout

1 – When signal is high on this one, the first motor control is enabled

2 – This is the control pin. A high on this and a low on pin 7 will spin the motor in one direction

3 – This is where you connect one end of the motor

4 – Ground (Connect heat sink too if you need)

5 – Ground (Connect heat sink too if you need)

6 – Connect other end of the motor with this pin

7 – If this pin is high and pin 2 is low, then the motor spins in opposite direction

8 – The power supply for the motors

16 – The power supply for TTL level (5V)

Other pins are similar and used for bi-directional control of second motor.

If you want both motors to be enabled at all times, then simply connect both the enable pins to Vcc (TTL)

If you want S/W to control enable/disable then connect these pins with the output ports of the microcontroller.

I used ATTiny2313 and connected PORTB pins 0-5 (total 6) to L293D. The scheme is given in the table below:

Microcontroller Pin L293D Pin
PortB – Pin0 Pin-1 (Enable Motor A)
PortB – Pin1 Pin-2 (Motor A Input, first direction)
PortB – Pin2 Pin-7 (Motor A input, reverse direction)
PortB – Pin3 Pin-9 (Enable Motor B)
PortB – Pin4 Pin-10 (Motor B Input, first direction)
PortB – Pin5 Pin-15(Motor B input, reverse direction)
DC Motor Control Circuit Using L293D

DC Motor Control Circuit Using L293D

The circuit board I built is simple and a picture for reference is on the left. The two terminal blocks on the left provide output to two DC motors. The single terminal block on center-top is to get power for the motor. The L293D is at the center. On the right is an FRC cable header to connect with the microcontroller board. The microcontroller board provides TTL power needed by L293D.

The Software

Given the above pin connections, the software is now very simple. To control just one motor (let’s say MotorA) the pseudo-code is:

  1. Define port pins as output
  2. Output 0b00000011 or 0x0B on port. This command says, enable motor and drive in one direction
  3. Pause/Wait
  4. Output 0b00000101 or 0x05 on port. This command says, enable motor and drive in another direction.
  5. Pause/Wait
  6. Loop

The actual code is given below:

#include <avr/io.h>
#define F_CPU 3868400UL
#include “delay_x.h” //I’m using a custom delay routine from Hans-Juergen Heinrichs
void main()
                          DDRB = 0xFF; //All pins as output
                          PORTB = 0b00000011; //Drive motor A in one direction
                          PORTB = 0b00000101; //Drive motor A in reverse direction


Disclaimer: Don’t blame me if for anything. You bear the full risk of doing this.


3 thoughts on “Controlling DC Motors using AVR Microcontrollers

  1. Pingback: How to build your first mobile robot platform « Vishnu's Blogs

  2. Hy!My circuit look exactly like yours,but still have a problem.I have a lm35 temperature sensor on atmega328p and when the fan is on(the dc motor), the temperature increase/decrease with -/+3 C.I don’t understand why.I have two different power supplies.

    • Without knowing more details, it is difficult to answer. But I’m making a guess. If you have the fan closer to the temperature sensor, you will notice considerable difference in temperature when it is on or off, as the case may be.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s