Interfacing DC Motor with PIC Microcontroller using L293D

dc motor

This tutorial explains ” How to interface DC Motor with PIC Microcontroller ? “.

DC Motor and L293D

We can’t drive a DC Motor (depends) directly with a Microcontroller, as DC Motors requires high current and high voltage than a Microcontroller can handle. Microcontrollers usually operates at +5 or +3.3V supply and it I/O pin can provide only up to 25mA current. Commonly used DC Motors requires 12V supply and 300mA current, moreover interfacing DC Motors directly with Microcontrollers may affect the working of Microcontroller due to the Back EMF of the DC Motor. Thus it is clear that, it not a good idea to interface DC Motor directly with Microcontrollers.

The solution to above problems is to use H-bridge circuit.

Basic H-Bridge DC Motor Driving

Basic H-Bridge DC Motor Driving

It is a special circuit, by using the 4 switches we can control the direction of DC Motor. Depending upon our power requirements we can make our own H-bridge using Transistors/MOSFETs as switches. It is better to use ready made ICs, instead of making our own H-bridge.

L293D and L293 are two such ICs. These are dual H-bridge motor drivers, ie by using one IC we can control two DC Motors in both clock wise and counter clockwise directions. The L293D can provide bidirectional drive currents of up to 600-mA at voltages from 4.5 V to 36 V while L293 can provide up to 1A at same voltages. Both ICs are designed to drive inductive loads such as dc motors, bipolar stepping motors, relays and solenoids as well as other high-current or high-voltage loads in positive-supply applications. All inputs of these ICs are TTL compatible and output clamp diodes for inductive transient suppression are also provided internally. These diodes protect our circuit from the Back EMF of DC Motor.

PIN Diagram of L293D

PIN Diagram of L293D

In both ICs, drivers are enabled in pairs, with drivers 1 and 2 are enabled by a high input to 1,2EN and drivers 3 and 4 are enabled by a high input to 3,4EN. When drivers are enabled, their outputs will be active and in phase with their inputs. When drivers are disabled, their outputs will be off and will be in the high-impedance state.

Function Table of L293D

Function Table of L293D

Interfacing with PIC Microcontroller

Circuit Diagram

Interfacing DC Motor with PIC Microcontroller and L293D Circuit Diagram

Interfacing DC Motor with PIC Microcontroller and L293D Circuit Diagram

Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.

We can drive two DC Motors with one L293D, in this example we are using only the first pair of drivers to drive one DC Motor. First pair of drivers are enabled by connecting EN1 to Logic HIGH. IN1 and IN2 are connected to RB0 and RB1 of PIC Microcontroller respectively which are used to provide control signal to the DC Motor.  DC Motor is connected to OUT1 and OUT2 of the L293D.

MikroC Source Code

void main()
{
 TRISB = 0; // PORT B as output port
 PORTB = 1; // Set RB0 to high
 do
 {
   //To turn motor clockwise
   PORTB.F0 = 1;
   Delay_ms(2000);//2 seconds delay

   //To Stop motor
   PORTB = 0; // or PORTB = 3 
   Delay_ms(2000);//2 seconds delay

   //To turn motor anticlockwise direction
   PORTB.F1 = 1;
   Delay_ms(2000);//2 seconds delay

   //To Stop motor
   PORTB = 0; // or PORTB = 3 (3 = 0b00000011)
   Delay_ms(2000); // 2 seconds delay

 }while(1);
}

Control Signals and Motor Status

RB0/IN1 RB2/IN2 Motor Status
LOW LOW Stops
LOW HIGH Anti-Clockwise
HIGH LOW Clockwise
HIGH HIGH Stops

You can download MikroC Source Code, Proteus files etc here…

DC Motor Driving PIC Microcontroller and L293D



  • Cyril3142

    How can i controll the speed of the motor?

  • lijoppans

    You can use Pulse Width Modulation to control the speed of the DC Motor. Use Enable pins (EN1 and EN2) of L293D for this. Note: The switching frequency at enable pins of L293D must be below 5KHz

  • Mr_ranie

    if i want to use 1motor , 1 heather, can i use this circuit.

  • http://www.electrosome.com/ Ligo George

    What DC motor…??…….12V ……..??

    If you are dealing with higher voltages, it is better to use RELAY……….Connect your motor and heater through relay…..

  • Hd086

    hey , i have 2 DC motors and ned to control them by joystic , to control the motion and speed , how u can help 

  • http://www.electrosome.com/ Ligo George

    In simple way: Replace Joystick switches ……

    You can control the speed of DC Motor using Pulse Width Modulation…

  • Pmahao6

    how do i download MikroC source code

  • http://www.electrosome.com/ Ligo George

    You must be register and login to download the file…………
    you can login or register at http://www.electrosome.com/wp-login.php

    Download Link : http://www.electrosome.com/wp-content/plugins/download-monitor/download.php?id=9

  • Teronko

    how does pulse width modulation work and how is the c code controlling speed of dc motor?
     

  • http://www.electrosome.com/ Ligo George

    The speed of a DC Motor is proportional to the voltage supplied to it…………….

    By using PWM you can change the average voltage delivered to the DC Motor.

    You may use the inbuilt PWM generators in PIC Microcontroller……

  • peejay

    hi i have 3 dc motors to be controlled in a system separately ie. switch 1 for dc motor A, switch 2 for DC motor B and so on… how to interface them in a MCU based system? could you give me a hint sir please im willing to learn more thanks

  • http://www.electrosome.com/ Ligo George

    You can use two L293D’s for that………. with one L293D you can control 2 dc motors…

  • Ankit Sarkar

    How to control L293D IC if 2 motors are connected? and how the bot will go left and right?

  • http://www.electrosome.com/ Ligo George

    You… can connect two motors to a L293D……..

    You can use EN2, IN3, IN4, OUT3, OUT4 terminals for connecting second motor..

  • jaycee

    how can i daownload the proteusfiles sir?

  • http://www.electrosome.com/ Ligo George

    First login and click on the download link at the bottom of the article.

  • nish

    Wer I can find code for generating pwmn of varying duty cycle?

  • nish

    Sorry pwm i meant

  • http://www.electrosome.com/ Ligo George
  • sasan

    hi all..how can add sensors for this circuit ..for control garage door ?
    thanks

  • http://www.electrosome.com/ Ligo George

    Connect the sensor to any of the pins and configure it as input pin and read data through that pin..

  • polik

    does it requires different code if i’m using PIC 18F4550??

  • http://www.electrosome.com/ Ligo George

    We need to make some modifications… due to change in registers,,
    To set direction of each io pin use TRIS register, to read data from io pins use PORT register but for writing data to io pins use LAT register…

  • yap

    how to control the direction forward reverse of motor using switch button?

  • yap

    how to control the direction forward reverse of motor using switch button?

  • http://www.electrosome.com/ Ligo George

    Set a pin as input and connect a switch to it..
    http://www.electrosome.com/push-button-switch-pic-microcontroller/

    then use if statement to set the direction..

  • sasan

    Hi Ligo George

    I have one question…i want build one circuit with pic 16f84a with dc motor..but i want add two switch as sensor or finish and start motor …how can do ?

    regards

  • http://www.electrosome.com/ Ligo George

    Read the tutorial, of using Push Button Switch with PIC Microcontroller..
    If(switch 1 is on)
    {
    steps to on the motor
    }
    if(switch 2 is on)
    {
    steps to off the motor
    }

  • sasan

    I do

    include
    #use delay(clock = 4000000)
    #FUSES XT,NOWDT,NOPROTECT,NOPUT
    #BYTE port_b =0×06

    void main(void)
    {
    SET_TRIS_a(255);
    set_tris_b(0);
    output_b (0);
    while(True)
    {
    if(input(pin_A3)==1){
    output_high(pin_b0);
    output_low(pin_b1);
    output_high(pin_b4);
    //delay_ms(500);
    }

    if(input(pin_A1)==1){
    output_low(pin_b0);
    output_low(pin_b1);
    output_low(pin_b4);
    //delay_ms(500);
    }

    if(input(pin_A0)==1){
    output_low(pin_b0);
    output_high(pin_b1);
    output_high(pin_b4);
    //delay_ms(500);
    }

    if (input(pin_A2)==1){
    output_low(pin_b0);
    output_low(pin_b1);
    output_low(pin_b4);
    // delay_ms(500);
    }

    }
    }

    this one work in isis but on circuit don’t work

  • http://www.electrosome.com/ Ligo George

    Try after increasing the delay.. 500ms delay is too small for getting a response from DC Motor..

  • sairamya

    can u give us the embedded code for this

  • http://www.electrosome.com/ Ligo George

    You can download all the codes associated with this article from the above download link.

  • rishi

    When my Project board has a pic 18x with a darlington array what all changes do i have to make to the circuit to interface a dc motor 12 v with the project board knowing that the outputs of the project board are via the darlington array ?

  • http://www.electrosome.com/ Ligo George

    18F pic microcontrollers have 2 seperate registers for reading and writing data to and from the port… use LAT register to write data.. (output) and use PORT register to read data………..
    Use TRIS register to set direction as in above article…

  • shri

    in the source code PORTB.F0=1 what that F0 means?

  • http://www.electrosome.com/ Ligo George

    F0 means the zero th bit of that port. (PORTB).. F1 means first bit.. F2 means second bit and so on..

  • Dhanaraj

    give me the code to generate pmw signal with the values from adc of pic in mikro c

  • http://www.electrosome.com/ Ligo George
  • cliff rayner

    can u design this circuit where it is operate using only one switch to automatically move the motor clockwise and after 5 seconds the motor automatically rotate anticlockwise back to its original position..?

  • http://www.electrosome.com/ Ligo George

    Hello, the program is very simple……. combine the above program and push button switch program…. Try it yourself… It is very simple…

  • Bam293

    can i using other PIC ? to replace the L293D

  • http://www.electrosome.com/ Ligo George

    Nop, you should use a driver..

  • schumey

    if i add a switch, which port need to connect with? and need to set as input right?

  • http://www.electrosome.com/ Ligo George

    You can connect it to any port…. but it should be configured as input by writing to tris register…….. If you need to use PORTA, it should be configured as digital pin by writing to ADCON register..

  • Tom Soya

    How can i control stepper motor using transistors?

  • http://www.electrosome.com/ Ligo George

    Hello, the about article is for controlling dc motor.. not for stepper motor..

    stepper motor can easily drive using 4 transistors… wire transistor as a switch… .. similar to that using uln.

    http://www.electrosome.com/stepper-motor-pic-microcontroller/

  • biskot o’rio

    why it won’t work when i use proteus…i don’t understand how its work..can’t u explain in details pleasee..its not function at all after i enter the source code..

  • http://www.electrosome.com/ Ligo George

    use separate power supplies for motor and pic.. with common ground… use proper filters for the power supplies…try after increasing the delay of motor rotation..

  • biskot o’rio

    thank’s man..u are a great man..

  • ThatGthang

    is there a way to send out two different signals at the same time? using this code?

  • http://www.electrosome.com/ Ligo George

    Sorry, I don’t get your question.. .Can you please elaborate..??

  • SR

    How do I control two motors i.e quadruple half-H drivers using this IC ?
    Could you elaborate the output of two motors according to inputs (truth table) ?

  • Shruti

    I am trying to use l293 with 3.3 v controller.what changes do I are o do in the above mentioned circuit

    Also does what are the limits for the enable pin? Can I directly connect it to Vcc?Or should I attach a resistor to limit the current?

  • http://www.electrosome.com/ Ligo George

    You can connect the input pins IN3 and IN4 of L293D to PIC Microcontroller .. connect EN2 to VDD to enable second module of L293D…
    Connect the second motor to OUT3 and OUT4..

  • http://www.electrosome.com/ Ligo George

    Connect the Vss pin of L293D to 3.3V.. it is the logic voltage input of L293D….
    No other changes are required..

  • Nidhi

    i am working on a project -Automatic sliding doors, where 2 motors are used for the movement of the door. (MCU-pic16f877a) The doors open when the PIR sensor detects movement. How do i program my mcu such that the motors work when motion is detected and stop when no motion is detected?

  • http://www.electrosome.com/ Ligo George

    Hello,

    It is simple.. .as output of PIR sensor is digital…

    Connect it to an input pin of pic..(similar to using switch http://electrosome.com/push-button-switch-pic-microcontroller/ ).. .. switch on the motor if motion is detected as above example..

  • Abdo Hbrk

    can I connect L293d with (ccp pin) and control the motor speed by using (pwm) ?

  • http://www.electrosome.com/ Ligo George

    Yes, ofcourse… try connecting the pwm output to enable input of L293D..

  • nebloid

    Hi, this is a great tutorial. Could you please tell me the purpose of this line of code: CMCON = 0×07; // To turn off comparators. I have been successful in running the motor, both with and without this line of code but I am just beginning to learn C and therefore am curious about its purpose. Thanks.

  • http://www.electrosome.com/ Ligo George

    Yes you are right… I will work with and without that code… It is used to turn off the comparator module of PIC which uses pins RA4 and RA5……
    If you are using pins RA4 or RA5 as digital I/O, then you must disable it… otherwise it is not necessary..

  • cliff rayner

    can someone help to build a schematic diagram??
    i need to move back and forth three stepper motor in sequences.
    using only one switch,i need to move the first motor 180 degree clock wise and then move anticlockwise back to its original position. as soon as the first motor stop, the second motor will repeat the process and so does the third motor..and stopped..

  • http://www.electrosome.com/ Ligo George

    Read our stepper motor tutorials and try designing yourself..

  • fil

    can i control just one dc motor with the h-bridge?

  • NASYA

    CAN I KNOW, WHAT IS THE FUNCTION OF CRYSTAL IN THIS DIAGRAM?

  • Egor Rat

    This is well done! I was wondering if you had ever had the problem where your motor voltage (12v) gets dropped when connected through the L293? I have set up a circuit to control a 9V DC motor with an L293. So my motor voltage is 9V, and my chip voltage is 5v. However, my motor voltage drops to about 3v and therefore doesn’t supply enough power for the motor. I’ve read where the chip can drop the voltage by 2-3 volts (which I could live with), but this drop is more like 6-7 volts. Thanks

  • teketel

    i want to connect buck converter to the above ckt how i can

  • http://www.electrosome.com/ Ligo George

    Please elaborate your need..