Control a DC motor with Arduino and L293D chip

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...

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.

arduino-L293D-DC-motor-control-circuit

  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-DC-motor-control

Arduino L293D code

So the final bit is to upload the sketch below to the board and give it a test :)

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
  }
}

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.

This content is published under the Attribution-Noncommercial-Share Alike 3.0 Unported license.

Technorati Tags: , , , , , , ,

  1. Arduino – Control a DC motor with TIP120, potentiometer and multiple power supplies
  2. Arduino: Controlling the Robot Arm
  3. Obstacle avoidance Arduino robot – build your own larryBot
  4. larryBot – Arduino robot versions 0.1 to 0.5 lessons learned
  5. Arduino – Modifying a Robot Arm
  6. Controlling a Servo with Arduino
  7. Arduino – Using a Sharp IR Sensor for Distance Calculation
  8. Arduino – Getting my LCD Screen to work.
  9. Arduino – IR remote/ intervalometer for Nikon D80 DSLR (that means timelapse photography yarrr!)
  10. Arduino – Redefining the TV Remote