Control a DC motor with Arduino and L293D chip

arduino L293D DC motor control side

[ad#Google links]

This is a quick guide with a bit of extra info (pin configurations etc..) that I’ve learnt along the way on how to use the L293D with the Arduino, showing that we can:

A) Use a supplemental power source to power the DC motor
B) Use the L293D chip to drive the motor
C) Use a switch to change the direction of the motor

UPDATE: If you intend to use this for robotics then please check out this page here to get the most out of this chip – I actually found the SN754410 easier to work with that the L293D, its exactly the same apart from it can handle more current Arduino obstacle avoidance robot

L239D DC Motor Driver & Pin Configuration

Although I’ve only used 1 motor, it is possible to use 2 motors on a single L293D chip, of course you then have to compensate on the current accordingly to ensure enough juice for both motors under peak load. Remember that if you use 2 motors, the power source will be the same voltage but the current needed will be doubled – a good start is by altering how your batteries are connected in series or parallel.

“The L293D is a monolithic integrated, high voltage, high current, 4-channel driver.” Basically this means using this chip you can use DC motors and power supplies of up to 36 Volts, thats some pretty big motors and the chip can supply a maximum current of 600mA per channel, the L293D chip is also what’s known as a type of H-Bridge. The H-Bridge is typically an electrical circuit that enables a voltage to be applied across a load in either direction to an output, e.g. motor.

This means you can essentially reverse the direction of current and thus reverse the direction of the motor. It works by having 4 elements in the circuit commonly known as corners: high side left, high side right, low side right, and low side left. By using combinations of these you are able to start, stop and reverse the current. You could make this circuit out of relays but its easier to use an IC – The L293D chip is pretty much 2 H-Bridge circuits,  1 per side of the chip or 1 per motor.

The bit we really care about in all of this is the 2 input pins per motor that do this logic and these, more importantly for our needs, can be controlled from the Arduino board.

You also don’t have to worry about voltage regulation so much because it allows for 2 power sources – 1 direct source, upto 36V for the motors and the other, 5V, to control the IC which can be supplied from the Arduino power supply or since my motor power supply is only 6V I’m going to use this (if the motor supply was higher I would consider using a transistor or voltage regulator). The only thing to remember is that the grounding connection must be shared/ common for both supplies. Below you can see the pin layout for the chip and the truth table showing the output logic.

L293D Pin layout

Pin 1 Pin 2 Pin 7 Function
High Low High Turn clockwise
High High Low Turn anti-clockwise
High Low Low Stop
High High High Stop
Low Not applicable Not applicable Stop

Generally speaking most DC motors require a lot more current than the Arduino board can provide for instance the motor that I’m using needs around 5 to 6 Volts. Now I could use a 12 Volt power source for the Arduino, but then its going to drain quickly when it has to power everything, especially if I was to add in another motor and a couple of servos, so instead my Arduino runs off of my 9 Volt power supply I made. (here)

You’ll need a few capacitors in this circuit to smooth out the power load to the motors as much as possible to help avoid any spikes and stabalise the current. I’m using a 50 Volt 10 uF capacitor on the power supply – I suggest you do this as the bare minimum. You could also add in a capacitor for each motor that you use – something like a 220nF multilayer ceramic capacitor should be OK for the small motors.

Arduino L293D Circuit Components

10K Ohm resistor (Brown, Black, Orange, Gold)
50V 10uF Capacitor
6V DC motor
L293D motor controller/ driver chip (IC)
A switch (push, toggle etc..)
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
4x AA battery holder
4x AA batteries
Optional 220nF multilayer ceramic capacitor (Y5V)
Optional 9V DC power supply or use the USB power for the Arduino

Building the L293D motor driver circuit

First lets start with the 16 pins on the L293D chip and what we need to wire these to. You’ll see that its basically got 2 sides, 1 for each motor.


  1. Enables and disables the motor whether it is on or off (high or low) comes from the Arduino digital PWM pin 9
  2. Logic pin for the motor (input is either high or low) goes to Arduino digital pin 4
  3. Is for one of the motor terminals can be either +/-
  4. Ground
  5. Ground
  6. Is for the other motor terminal
  7. Logic pin for our motor (input is either high or low) goes to Arduino digital PWM pin 3
  8. Power supply for the motor, this should be given the rated voltage of your motor, so mine is from a 6V supply
  9. Enables and disables the 2nd motor on or off (high or low)
  10. Logic pin for the 2nd motor (input is either high or low)
  11. Is for one of the 2nd motor terminals can be either +/-
  12. Ground
  13. Ground
  14. Is for the 2nd motors other terminal
  15. Logic pin for the 2nd motor (input is either high or low)
  16. Connected to +5V, in this case the power from motor supply

You can see from my photos how I’ve placed the L293D and wired it according to the above pins. Next I have my switch on Arduino digital pin 2 and I have the GND pin from Arduino connected to the GND rail on my breadboard. I also add the capacitor in between the power supply – making sure that the negative and positive terminals are correctly aligned. Finally I complete the circuit by adding in wires to carry the current from one side of the breadboard to the other and I add in the motor and its power supply.


Arduino L293D code

So the final bit is to upload the sketch below to the board and give it a test 🙂
[ad#Google Ad in content]

int switchPin = 2;    // switch input
int motor1Pin1 = 3;    // pin 2 on L293D
int motor1Pin2 = 4;    // pin 7 on L293D
int enablePin = 9;    // pin 1 on L293D

void setup() {
  // set the switch as an input:
  pinMode(switchPin, INPUT); 

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enablePin, OUTPUT);

  // set enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH);

void loop() {
  // if the switch is high, motor will turn on one direction:
  if (digitalRead(switchPin) == HIGH) {
    digitalWrite(motor1Pin1, LOW);   // set pin 2 on L293D low
    digitalWrite(motor1Pin2, HIGH);  // set pin 7 on L293D high
  // if the switch is low, motor will turn in the opposite direction:
  else {
    digitalWrite(motor1Pin1, HIGH);  // set pin 2 on L293D high
    digitalWrite(motor1Pin2, LOW);   // set pin 7 on L293D low

[ad#Google Ad in content]

My initial thoughts are of expanding this layout to include an additional motor perhaps. But more interestingly I think changing the switch to start/stop the motor, controlling the enable pin 1 on the L293D and then using a potentiometer to make use of PWM and control the speed as well as the direction of the motor.


  • thank you very much, a very useful work.

    • You’re welcome 🙂

      • hey,
        i mtrying to compile this program in c but thisone is giving me some errors.

  • hi i’m new to electronics could u pls gimme the circuit diagram i didnt understand the above topic!

  • useful work!we’v successfully run our 1st pair o wheels!

  • Well… I couldn’t make it work.

    I think i’m not missing a thing… is there any common mistake? I might b doing something wrong without noticing.

    but thanks anyway!

  • Great, thanks
    Have you yet made the extended version? I mean using potentiometer to both control the speed and direction? Have would you do that? I’ve been trying to accomplis that but not very well. i can control the speed but not direction…

    • I will do this – thats the trouble with Arduino you get sidetracked forever building another circuit you’ve just thought of 🙂

      • First off you are amazing for putting these tutorials (and the code) online. For graphic artists like me, it’s invaluable.

        So here’s my question… Are there limits on the voltage for the motor? For example I have a baggier motor that I would like to use for more torque, but I’m not sure if I risk frying something?
        What would you recommend?

        Thanks again Larry!

        • Sorry bout the delay in getting back to you on this. there are limits to the voltage a motor can take, its normally specified with the motor and typically in DC, so the motors I use operate on 5 volts but you can use the Arduino and something like a TIP120 transistor to let the arduino control bigger voltage motors up to something like 36 volts using the L293D.

          However higher voltage than the L293D can handle will fry it – as well as high amperage motors, the L293D chip can only take 600 milliamps per motor which isn’t that much and anything above that makes the chip get super hot – even though the motors are 5-12volt. Much better off with the SN754410 – exactly the same but can take twice the amperage.

          Just remember that the Arduino board can only give out limited voltage so ideally run your motors from a separate power source. Which I think I show in this tutorial – if not lookat my robotics stuff.

          I’ve yet to fry anything! 🙂

  • […] with arduino through h-bridge. Theres nice tutorials on h-bridge at ITP’s site, and on lucky larrys ardunio site and also on Umass Amherts M5:s site. So I wont explain the basics of h-bridge here. From the scheme […]

  • […] Lucky Larry’s ARDUINO, MAINLY ARDUINO PROJECTS, TUTORIALS… AND A FEW OTHER THINGS blog – Control a DC motor with Arduino and L293D chip… but (sigh)… I think I’ll just buy a shield […]

  • Hi, do you have the processing changes for useing the ping sonar with the arduino to the radar?

    • I think the only thing that changes is the pin configuration on the arduino sketch for using the ping sonar, just do a temporary sketch and update mine accordingly.

  • Hi,

    I have a project in school. It is all about computer interfacing using visual basic or any kind of language. I dont have any idea what to interface. I am planning to drive dc motor but i dont know how. I am not particular in computer interfacing and control systems. Can you give me any idea? Thank you.

    • sorry, probably a bit too late to reply now. But the Arduino boards are ideal for interfacing with hardware and running code and keep things simple. Check eBay for a cheap board or also look at PIC.

  • Hey, so is the smoothing capacitor connecting the 5v and ground? Also, you talk about the SN754410, I thought it didn’t have flyback diodes, wouldn’t it be easier to work with the chip that doesn’t require external flyback diodes?

    • The smoothing capacitor does run between the 5v and ground from what I can remember as a way to remove any power spikes. The SN754410 doesn’t have flyback diodes, so yes to be safe you’d want a couple of 1n4004’s or something similar if running high power supplies – I’m using 9v battery so it wasn’t so much of an issue.

      The L293D is limited though despite having inbuilt flyback diodes and it can’t handle higher stall current motors, if you find something similar that can let me know.

  • Two questions: 1) Is the capacitor placed in between the ground and 6V?
    2) Why do you link the ground rail from the arduino into this circuit?

    • The capacitor is there to smooth any spikes from the power supply.

      I always keep a common ground between all circuits where possible.

  • thanks! this is the best tutorial on l293d on the internet! it has very clear instructions on motor direction control, your circuit is clear, and you coding is readable! awesome tutorial, you really should put it on instructables 🙂

  • How do you use PWM to control the speed in this?

    Would you apply it to the enable pins?

    • Yes, applying PWM to the enable pin will vary the output voltage to the motor.

      So, analogWrite(enablePin,128); // PWM pin number, value (0-255)

  • Hi, I’ve chosen your project as my final project for my Automatic Control class, the teacher wants us to use Arduino with the library PID. Is there any way I can include this library in this excercise?

  • hey, if i got a set point (using potentiometer) and i’m comparing it with a feed back from motor (another potentiometer connected to the motor). How do i implement the coding, i wanna use PWN on it so that when the difference between the two potentiometer is great the speed of motor: slow down and when the difference is small the speed of the motor: faster.

  • Hello i want to do the same circuit but i don’t know where i can by the L239D chips.
    Can you help me ?
    Thank you.

  • Can I just ask why did you put a resistor between the button and ground? is this needed?

  • Yes, that resistor is needed.

  • Hi, damn I think my ATmega 328p on my arduino Uno broke when trying this. I used a 12v power supply that went through the arduino board. So when I pushed the switch I guess there was too high current going in on pin 2. But I thought the resistor would help against that. Am I wrong? Also its hard to know whats damaged, at least I can’t upload any code.

  • Is it required that you must use an external 6V battery for the motor instead of using a common 9V battery for both arduino and the motor?

    I mean that you can divide the voltage for arduino and the IC

  • is it possible to hold a dc motor in a certain position eg. at 60 degrees or 75degrees

  • i hae everything running and it works fine.. the only thing is, when i unplug the 9 volt power source for my motor, the motor keeps spinning, the arduino seems to be powering it, i dont understand why this is happening tho… any advice will be GREATLY aprecciated.. Mario

    • Odd. I would check for any common power, for instance if it’s on a shared rail etc… and just double check the setup and everything is wired up properly.

  • how can i get 2 inputs from one ir sensor to drive motor using l293d ic circuit?
    please help me

    • so you have 1 IR sensor and you want to drive the motor based upon what it detects? Or is this more a line following robot?

  • When I use DC Motor ex my children toy, i can’t control the dc motor. DC Motor not work,just bzzzzz…

  • hello i would like to ask about the program if i would like to vary the speed based from a reading of a potentiometer? and also is the PWM signal also used as an input for pin 2 or pin 7 instead of just a digitalwrite which has constant 5V output? are those pins the ones that control the speed not the pin 1?

  • hi.. can you teach me the codes of input in arduino? like instead of using switch i will use the output of an ic..

  • Hello there, I like your project and decided to make one.

    I bought these

    AVX – 0.22uF 220nF 50V Y5V 1210 SMD Capacitors – 150pcs

    from ebay but I think these are the wrong capacitors or am I wrong?

    Can I use these or not?

    Please reply, because I might cancel the purchase if these are wrong

    • Hey,

      Not sure I can be of much use here, all capacitors work the same, what will be different is it’s effective temperature range and capacitance. But considering this is a small circuit I can’t imagine it’ll actually make much of a difference, if the capicitance is the same but a different construction.

  • very helpful.thanks a lot for this tutorial.

  • i am using L293d motor driver to run two dc motors.
    is it required to use a particular voltage for it.
    i am trying to run this but i think motor driver is out of order.
    how to make sure this

  • Thanks for this link. .really helpful. .almost done half my final yr project wid dis. .there is only 1 doubt that for two 12V motors will the circuit works. .it wont get fried!!

  • Larry, thanks for posting this information. It has been most helpfull. I have a few questions though. The stepper motors I have require 24 volts. Would this circuit work the same way if the 6V power supply was changed out for 24 volts?

    The other question is what is the advantage of driving the motors with this L293D vs. using a few transistors and controlling the steps with the Arduino?

    I have a circuit with an Arduino controlling some LEDs with how I want to drive the 4 leads of the stepper with full steps and half steps. Then I have a circuit with divided voltage with 4.5 volts going through some momentary switches into the base of some transistors to switch the full 24 volts going to the stepper motor. so if I push the buttons in the right order the motor starts stepping forward. But my questions are aimed at combining the two circuits without frying my Arduino.


  • how if i want to control 3 dc motor do i need 2 L293D? L293D support only two motor.if yes how about it connection?

  • How did you connect that 10microfarad capacitor??? I really appreciate your work…. thanks in advance

  • hi…its really such a nice tutorial…
    actually i am driving 2 dc motor with l293d and i am using arduino uno…my question is that when i am giving the power supply its automatically starts and its speed is very high…how to control the speed?????
    actually i was trying to move it with voice command

  • hi please help me .. i want to control 2 dc motor with l293d with arduino uno .how can i control the rotation of motors clockwise n anti-clockwise. please write a program.

  • Check out this guide for a variable speed motor controller called GOduino II. It seems the designer based his work on your project.

    GOduino II = Arduino + L293D Variable Speed Motor Controller

  • how can you control two motors???

  • […] Next was this LASER Projector (pattern generator to be precise). It involves two DC motors and L293D as a motor driver. […]

  • Hi Larry. Ive coded it with what you have done and have got it working with one motor, Ive also tried the following code with no luck.

    int motor1Pin1 = 8; // pin 8 on L293D
    int motor1Pin2 = 9; // pin 9 on L293D
    int enablePin1 = 7; // pin 7 on L293D
    int motor2Pin1 = 12; // pin 12 on L293D
    int motor2Pin2 = 13; // pin 13 on L293D
    int enablePin2 = 11; // pin 11 on L293D

    void setup() {
    // set all the other pins you’re using as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(enablePin1, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(enablePin2, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin1, HIGH);
    digitalWrite(enablePin2, HIGH);

    void loop() {
    // if the switch is high, motor will turn on one direction:
    digitalWrite(motor1Pin1, HIGH); // set pin 8 on L293D low
    digitalWrite(motor1Pin2, LOW); // set pin 9 on L293D high
    digitalWrite(motor2Pin1, HIGH); // set pin 12 on L293D low
    digitalWrite(motor2Pin2, LOW); // set pin 13 on L293D high


  • hi
    Iam new to robotic , and stuff I have an electric golf trundler -12 dc motor.Has a variable speed controller.
    U able to give tme ips how to design a ciruit to interface the control of this motor.Here is how I wanted to operate:
    1. the trundler will follow me whereever I go.
    2. distant betwen trundler and me is about 2-3 mteres.Any distance less than that it will not do anything, Put if dist is just outside the 2 m window it will follow
    3. I stop and it stops. I tee off it stops or I manually turn off power.

    Would appreciate any head up on tis

    Best regards


  • Thanks Larry… It was beautiful to see my motor running 🙂 Thanks for this wonderful tutorial.

  • I have plugged the same but did not work. Output voltage is so small that it can not drive the motor. even it can not light an LED full bright. I measured output voltage which is below 0.5V. External power supply is 9V. I even tried with 12V also but the result is same. Don’t understand where is the voltage is going? Any idea?

  • thanks for the good work buddy ….
    but will u help me out with the servos (like futaba) which uses a potentiometer along with an IC (not known to me yet )
    i would like to use L293D in place of the unknown ic..
    all i need is a connection with that i could vary currents

  • is it possible to run the motor for a certain time? i mean i want to run it for 2 seconds and after 2 seconds it will automatically stop. is that possible?

You must be logged in to post a comment.