DC Motor Control using ATmega32 and L293D

 In this tutorial you will learn how to control DC motors using L293D half H-bridge integrated circuit as a motor driver which is controlled by ATmega32 microcontroller. L293D is a quadruple half-H bridge device designed used to drive DC motors and Stepper motors. It is designed to be used with a microcontroller like ATmega32 because microcontroller do not have sufficient output voltage and current to drive motors. The L293D and operating voltage of 4.5V to36V while it can output current of 600mA upto peak current of 1.2A. It can drive two DC motors and one stepper motor(or a two phase motor) at the same time and independently. So this motor driver IC can be very useful to learn for your electronics projects. 

Here we will be controlling just one DC motor to make it easy to understand. Once you have learned how to connect one DC motor with L293D and a microcontroller and how to write DC motor control program in C, you will able to understand how to connect and control 2nd DC motor using L293D.  

L293D IC

 About L293D Quadruple IC

Shown below is picture of L293D Integrated Circuit chip.

L293D Pin Diagram

l293d pin diagram

Internally, L293D consists of 4 op-amps that outputs higher current at the outputs if enabled. The 1A, 1Y are input and output for 1st op-amps and 2A, 2Y are input and output for 2nd op-amp. These two op-amps are enabled/disabled using the 1,2EN pin. Similarly, 3A and 3Y are input and output of the 3rd op-amp and 4A and 4Y are input and output of the 4th op-amp. These two (3 and 4) op-amps are enabled/disabled by the 3,4EN pin. The VCC1 is voltage supply pin which should be between 4.5V to 7V and VCC2 is voltage supply pin for motor which should be between VCC1 and 36V.

Interfacing ATmega32 with L293D and DC motor

To control one DC motor, we just need 1A and 2A inputs and 1Y and 2Y output. The inputs 1A and 2A should be connected to two pins on the ATmega32 microcontroller, which in our case is pin PD4 and PD5. The output 1Y and 2Y should be connected to the two terminals of the DC motor(in the schematic below these are OUT1 and OUT2). The enable 1,2EN pin should can be connected to the microcontroller or you can just connect to the supply voltage of the microcontroller(that is to +5V). If you want to enable/disable the motor from the microcontroller than you should connect this enable pin to micrcontroller which is what we will doing here. This enable pin in the schematic is shown as EN1 and is connected to PD2 pin of ATmega32. The VCC1 should be connect to the same power supply as the microcontroller. The VCC2 should be connected to the power supply(which can be different from MPU supply) of the motor. Here we have used +5V voltage supply for the motor too so we connecct the VCC2 also to +5V.

Atmega32, L293D DC motor schematic diagram is shown below.

atmega32 L293D DC motor schematic

Notice that we have used a 10KOhm potentiometer in order to control the speed of the DC motor. Otherwise it will be difficult to see the spinning rotation direction of the motor. Adding a potentiometer is not a mandatory requirement.

The following picture shows ATmega32, L293D and DC motor circuit wiring on a breadboard according to the schematic shown above.

Atmega32 L293D DC motor control

Video Demonstration of DC motor control using ATmega32 and L293D 

Programming & Coding

The following is C program code for DC motor control using ATmega32 and L293D.

#ifndef F_CPU
#define F_CPU 4000000UL

#include <avr/io.h>    
#include <util/delay.h>  

#define en12  (1<<PD2)
#define a1 (1<<PD4)
#define a2 (1<<PD5)
int main(void){
    DDRD |= en12 | a1 | a2;
    PORTD |= en12;

        //clockwise rotation
        PORTD &= ~a1;
        PORTD |= a2;

        //stop motor
        PORTD &= ~a1 | ~a2;    
        _delay_ms(3000);    //3 sec delay
        //anti-clockwise rotation
        PORTD |= a1;
        PORTD &= ~a2;
        PORTD &= ~a1 | ~a2;    //stop motor

Code explanation

In the program code above, we have first defined micontroller pin PD2 as Enable pin en12, pin PD4 and PD5 as a1 and a2 for easier programming and readable code. Then in the main function, we configured these 3 pins are outputs. Then we send a logic HIGH signal to the en12 pin to enable the L293D. In the while loop, we make input a1 low and input a2 high to rotate the motor in clockwise direction. Then to stop the DC motor for a while we make a1 and a2 both low. We wait for 3 second using delay function. Then to rotate the DC motor in reverse direction, that is anti-clockwise, we make the input a1 high and input a2 low. Then at the end we create a time delay of 3 seconds. And the sequence repeats again and again. This was illustrated in the video above.

Further Recommended Tutorials:

- Speed control of DC motor with PWM using Arduino

Post a Comment

Previous Post Next Post