Learn

An Arduino L293D motor driver, forward, reverse, brake and coast

  • 192parts on the bench
  • 25boards running now
  • 1.00xreal time, on every board
Arduino Uno: L293D H-bridgelive0.000 s 0.00x
Click to open it in the editor
The H bridge example on a simulated ATmega328P: forward, brake, reverse, coast, then half throttle.

A transistor can switch a DC motor on and off, but it cannot make it turn the other way, because it can only pull one end of the motor down to ground. To reverse a motor you need to put either end at the supply and the other at ground, and that takes four switches arranged in an H around the motor. The L293D is that H, twice over, in one 16-pin chip.

The circuit above is an Arduino Uno driving a small DC motor through an L293D. It runs forward, brakes, runs in reverse, coasts to a stop, and then goes around again at half throttle. It is running in your browser on the sketch shown below, and the serial monitor says what the motor is being told to do at each step.

What you need

  • An Arduino Uno
  • An L293D motor driver chip
  • A small DC gearbox motor (the yellow kind from robot car kits)
  • A breadboard and jumper wires
  • On a real bench, a separate battery pack for the motor is a good idea

Wiring

Part and pin
Goes to
Note
L293D EN1,2 (pin 1)
Pin 9
The PWM that sets the speed
L293D IN1 (pin 2)
Pin 7
Direction input
L293D IN2 (pin 7)
Pin 8
Direction input
L293D OUT1 and OUT2 (pins 3 and 6)
The motor’s two terminals
L293D VSS (pin 16)
5 V
Logic supply
L293D VS (pin 8)
5 V here; the motor battery on a real bench
Motor supply
L293D GND (pins 4, 5, 12, 13)
GND
Share the ground with the Uno

The truth table

Each side of the motor connects to one output, and each output follows its input: IN1 high puts OUT1 at the motor supply, IN1 low puts it at ground. So IN1 high and IN2 low drives current one way through the motor, forward. IN1 low and IN2 high drives it the other way, reverse.

Both inputs the same, both high or both low, puts both ends of the motor at the same voltage. That is braking: the motor’s own back voltage is shorted through the chip and it stops in a fraction of a second. Taking the enable pin low instead lets go of both ends entirely, and the motor coasts to a stop over a second or more. The sketch does both so you can watch the difference on the canvas.

Forward, brake, reverse and coast, from the H bridge example (trimmed)
void drive(const char *what, int a, int b, int duty) {
  digitalWrite(IN1, a);
  digitalWrite(IN2, b);
  analogWrite(ENABLE, duty);
  Serial.print("motor: ");
  Serial.println(what);
}

void loop() {
  drive("forward", HIGH, LOW, 255);  delay(2500);
  drive("brake",   LOW,  LOW, 255);  delay(800);
  drive("reverse", LOW,  HIGH, 255); delay(2500);
  drive("coast",   LOW,  HIGH, 0);   delay(1600);
}

Speed control with PWM on the enable pin

The usual way to set the speed is analogWrite on the enable pin. Pin 9 on an Uno is a PWM pin, so analogWrite(9, 128) switches the outputs on and off 490 times a second, on for half of each cycle. Between pulses the outputs are released, not braked, so the motor coasts through the off time and its inertia smooths the pulses into a speed.

Something the sketch’s own comments point out: with nothing on the shaft, an unloaded motor coasting between pulses has nothing to slow it down, so it can creep up to nearly the same top speed at half duty as at full, just more slowly. With a real load, a wheel on the ground or a gear train, duty behaves like the throttle it looks like. Mokxi’s motor has no mechanical load, so you will see exactly that effect in the last part of the loop.

Where the volts go

The L293D’s output stages are Darlington transistors, and each keeps about 1.2 volts for itself. There is one on the high side and one on the low side of the motor, so from a 5 volt supply the motor sees only about 2.6 volts and runs at roughly half the speed you might expect. That is not a flaw in the simulation; it is the figure on the datasheet, and it is why builds that need every volt use a MOSFET driver board instead.

The D in the name stands for the clamp diodes built into the chip. They catch the voltage spike a motor coil throws when its current is switched off, which is why this circuit needs none of the separate flyback diodes a transistor driver does.

Try it in the editor

Change the half throttle step from SPEED / 2 to SPEED / 4 and watch how long the motor takes to wind up.

Then add a potentiometer on A0 and replace the fixed speed with analogRead(A0) / 4, which turns the 0 to 1023 reading into a 0 to 255 duty. Add a button to flip the direction, and you have the controls of a robot car’s drive wheel.

For a second motor, use the other half of the chip: EN3,4 on another PWM pin such as 10, IN3 and IN4 on two more pins, and OUT3 and OUT4 to the second motor. That is the whole drive system of a two-wheeled robot.

Common mistakes

No shared ground. The Uno and the motor supply must share GND, or the inputs have no reference and the chip ignores them.

Running the motor from the Uno’s 5 V pin on a real bench. A stalled motor can draw far more current than a USB port or the board’s regulator should supply, and the Uno resets every time the motor starts. Put the motor on its own battery pack through VS.

Forgetting the enable pin. With EN low, nothing happens however the inputs are set. Tie it high, or drive it with analogWrite.

Expecting full speed. The chip keeps a couple of volts for itself, so plan the motor supply a little higher than the motor’s rating, within the chip’s limits.

Questions

How do I reverse a DC motor with an Arduino?

Use an H-bridge such as the L293D. Set IN1 high and IN2 low for one direction, IN1 low and IN2 high for the other, and drive the enable pin with analogWrite for speed.

What is the difference between braking and coasting?

Braking sets both inputs the same, which shorts the motor through the chip and stops it quickly. Coasting takes the enable pin low, which disconnects the motor and lets it spin down on its own.

L293D or L298N?

Both are Darlington H-bridges with a similar voltage loss. The L298N handles more current and usually comes on a module with screw terminals. The L293D fits a breadboard and has its clamp diodes built in. Mokxi has both.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.