Arduino Motor Controller Using an L293D Chip

Custom robot shield

Controlling DC motors is at the heart of many robotic projects. Servo motors are sexy but DC motors are cheap and a lot more useful to control wheel based robots. In this article I’ll display a “robot shield” circuit that allows you to use cheap motors to drive your robot.

Wiring

This shield has been built on top of the Adafruit Prototype Shield. Using the prototype shield makes it much simpler to develop a custom Arduino shield. You can get one from Adafruit here.

The assembly is powered by a 6V battery connected to the Vin pin of the Arduino and to the voltage regulator input pin. The voltage regulator output is connected to pin 9 (Vcc2) of the chip to power the motors. The Arduino 5V pin is connected to pin 16 of the controller to power the internal logic. Make sure to use a proper heat sink on the voltage regulator and on the chip (pins 4,5,12,13) if you plan to power the robot with a higher voltage source.

The L293 current driver chip amplifies the input signal received on pins 2, 7, 10 and 15 to outputs pins 3, 6, 11 and 14. This allows us to take a low power signal from our circuit and transform it into a higher power signal for the motors. Using these 4 input/output the L293 can drive two motors forward and backward.

Motor #1 is connected to pins 3 and 6 and motor #2 is connected to pins 11 and 14. To drive motor #1 you apply 5V on pin #2 and 0V on pin #7. To drive the same motor in opposite directions you apply 5V on pin #7 and 0V on pin #2. You can use the same technique to drive motor #2 with pins 10 and 15.

Speed can be controlled by sending a logical pulse to the L293 “enable input” pins. Pin #1 drives the speed of motor #1 and pin #9 of motor #2. The best way to do this is by using the Arduino PWM outputs on pins 9, 10 and 11. In this example, the Arduino pin #10 is connected to the L293 pin #1 and the Arduino pin #11 is connected to the L293 pin #9.

Speed And Direction

There are two methods to control the direction of a wheeled robot. One is to build a direction mechanism similar to cars where the wheels are oriented in the direction you want the robot to go. This method involves complex mechanisms and requires more space for the robot to maneuver.

The alternate method is to drive left and right wheels in opposite directions allowing the robot to make a sharp 360 degrees turn. This method requires no additional mechanism and is much simpler to implement by using a micro-controller. However, this method assumes that both motors are running at the same speed, which is often not the case with cheap motors.

This code shows how to control direction using the L293 chip with an Arduino board.

...

void backward()
{
  digitalWrite(MOTOR_1A, LOW);
  digitalWrite(MOTOR_1B, HIGH);
  digitalWrite(MOTOR_2A, LOW);
  digitalWrite(MOTOR_2B, HIGH);
  checkDistance = 1;
}

void forward()
{
  digitalWrite(MOTOR_1A, HIGH);
  digitalWrite(MOTOR_1B, LOW);
  digitalWrite(MOTOR_2A, HIGH);
  digitalWrite(MOTOR_2B, LOW);
  checkDistance = 0;
}

void right()
{
  digitalWrite(MOTOR_1A, LOW);
  digitalWrite(MOTOR_1B, HIGH);
  digitalWrite(MOTOR_2A, HIGH);
  digitalWrite(MOTOR_2B, LOW);
  checkDistance = 0;
}

void left()
{
  digitalWrite(MOTOR_1A, HIGH);
  digitalWrite(MOTOR_1B, LOW);
  digitalWrite(MOTOR_2A, LOW);
  digitalWrite(MOTOR_2B, HIGH);
  checkDistance = 0;
}

void stop()
{
  digitalWrite(MOTOR_1A, LOW);
  digitalWrite(MOTOR_1B, LOW);
  digitalWrite(MOTOR_2A, LOW);
  digitalWrite(MOTOR_2B, LOW);
  checkDistance = 0;
}

...

For example, the Tamiya double gearbox includes two low cost DC motors. The gearbox is amazing for small robotic projects but heading straight with these motors can be much more difficult than expected.

This is where the L293 chip comes to the rescue. This chip can control both the direction and the speed of the motors. To control the speed of the motors you connect the two PWM digital outputs of the Arduino micro-controller to the enable input pins 1 and 9 of the chip. As explained in the datasheet: “When an enable input is high, the associated drivers are enabled and their outputs are active and in phase with their inputs”.

You are now able to adjust the motor speeds by changing the PWM output value of the enable input of the faster motor. PWM outputs range from 0 to 255 so, to slow down a motor by a ratio of 10% you should set the associated PWM output to 230. Trial an error is the only way to get that done and make your robot head in a straight line.

void setup()
{
...

  pinMode(10, OUTPUT); // To L293 pin #1
  pinMode(11, OUTPUT); // To L293 pin #9
  analogWrite(10, 185); // Faster motor at 72% of its full speed
  analogWrite(11, 255); // Slower motor at 100% of its full speed

...
}

What’s Next

In a future article we will see how to use the shield to drive a simple Robot controlled by an infrared remote control. This project will pack together many tutorials published on this blog. If you have worked with the L293 chip to control motors, let us know in the comment section below.

15 Comments

  1. vaibhav
    January 23, 2012

    nice work man…!!!

    Reply
  2. pchretien
    January 23, 2012

    Thank you! I’ll eventually post more info on the small robot I made using this shield.

    Reply
  3. iwestbury
    May 4, 2012

    Hi Phillipe. Nice clear article.

    I am going to use this for pwm control with a PIC. If you have tried this (note, I will only use it to go in one direction) does it matter whether you apply the pwm to the enable or to the inputs? Looking at the data sheet they appear to apply it to the inputs. I’m not sure :(

    Reply
    1. pchretien
      May 4, 2012

      Hi, Thank you for your message!

      One advantage of the enable pin is to spare a PWM output on your microcontroller. It may vary from one microcontroller to an other but on the Arduino, the Netduino and the Beaglebone there are a maximum of 3 PWM outputs.

      An other advantage of using the enable pin for PWM is the ability to maintain synchronicity between the two inputs. To drive a motor in one direction you will need to apply 5v on one input and Grnd on the other. To pulse both inputs you will need toggle both inputs one after the other … which will result in a short period of time where both inputs will be at the same value.

      That’s all I can see. I suggest you try both and let us know how it works! :)

      Good luck,

      Phil

      Reply
  4. iwestbury
    May 5, 2012

    Hi Phil, thanks for the quick response. I think the datasheet is misleading because if both A & B are Hi or Lo the motor will stop so the pwm MUST be applied to the enable pin. Thanks for the help!!

    Reply
  5. muffin123
    May 5, 2012

    please help me!!

    I am trying to build your “robot shield” but I don’t have a clue as to how. Could you take an birds-eye view picture of the shield? This would really help me!

    Rowan

    Reply
    1. pchretien
      May 6, 2012

      That’s pretty much what the actual picture is … It would be much more useful that you send me a picture of what you have done so far so I can try to help you.

      Reply
  6. muffin123
    May 8, 2012

    Hi

    I can’t seem to get a picture to copy into the comments box. But I can try to describe what I have so far. I have the 0 volts pins (on the L293D) conected to the negative of the motors power supply. the positive of the motors power supply is conected to +Vmotor on the L293D. enable 1 is conected to pin 10 (on the arduino) and enable 2 is conected to pin 9 (on the arduino). input 1 is conected to pin 5 (on the arduino), input 2 is conected to pin 6 (on the arduino), input 3 is conected to pin 4 (on the arduino) and input 4 is conected to pon3 (on the arduino). Motor 1 is conected to output 1 and 2, motor 2 is conected to outputs 3 and 4.

    Rowan

    Reply
    1. pchretien
      May 8, 2012

      Hi,

      Are you powering the Arduino? Make sure you have all grounds plugged together. You should have two distinct circuits, one for the Arduino and the L293 logic part and one for the motors and the L293 power part.

      P.

      Reply
  7. muffin123
    May 9, 2012

    Hi

    thank you for the ground tip!
    it really helped!

    Rowan

    Reply
  8. muffin123
    May 10, 2012

    When I turn on the motors all they do is make a faint buzzing noise. I don’t know what is happening.

    Rowan

    Reply
    1. pchretien
      May 10, 2012

      What value have you written on the enable pins in the setup() function? Try to hard wire both enable pins to the Arduino 5V.

      Reply
  9. muffin123
    May 10, 2012

    I hardwired the enable pins to the arduino 5V pin. Then I uploaded this code(note: all I am trying to do is spin 1 motor in 1 direction). It didn’t do anything. I might have wired it incorectly. This is my code:

    void setup() {

    pinMode(6, OUTPUT)
    pinMode(5, OUTPUT)

    }

    void setup() {

    digitalWrite(6, HIGH)
    digitalWrite(5, LOW)

    }

    Reply
    1. pchretien
      May 11, 2012

      Hi Rowan,

      I propose you use the code of my twitterbot to test your circuit. You can find it at https://github.com/pchretien/tweetbot. Once running you can open the serial terminal of the Arduino IDE and send commands to your robot; 1=forward, 2=backward … etc.

      P.

      Reply
  10. muffin123
    May 11, 2012

    sorry,

    it seems that my code did not copy corectly into the into the comments box: there are 2 void setups!! it was not like that before.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top