Built-in project · Arduino Uno R3

DC motor on an L293D (pins 5, 3 and 4), on an Arduino Uno

An L293D runs a DC motor: enable on pin 5 sets the speed with PWM, and pins 3 and 4 set the direction. Forward, backward, slow, fast and stop, each one named. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $35. On Mokxi they're free.

DC motor on an L293D (pins 5, 3 and 4) · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Why a motor needs a driver chip, not a direct pin
  • An H-bridge: direction from two inputs, speed from enable
  • PWM on the enable pin sets speed
  • Sharing ground between the Uno and the driver
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 4

How it works

An Uno pin can only give about 20 mA, and a motor wants far more. The L293D is a motor driver: the Uno tells it which way and how fast, and the chip switches the motor's supply.

Pins 3 and 4 go to IN1 and IN2 and set direction. IN1 high with IN2 low turns the motor one way, the reverse turns it the other. Pin 5 goes to the enable pin and uses PWM, so a lower value spins the motor slower.

The loop runs forward at full speed, stops, runs backward, then tries a slow and a fast forward.

Parts list

4 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Motor driver, L293D pin EN12Arduino Uno R3 pin 5
Motor driver, L293D pin IN2Arduino Uno R3 pin 4
Motor driver, L293D pin IN1Arduino Uno R3 pin 3
Motor driver, L293D pin VSArduino Uno R3 pin 5V
Motor driver, L293D pin VSSArduino Uno R3 pin 5V
Motor driver, L293D pin GND1Arduino Uno R3 pin GND
DC motor pin 1Motor driver, L293D pin OUT1
DC motor pin 2Motor driver, L293D pin OUT2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A DC motor on an L293D: speed on pin 5, direction on pins 3 and 4.
//
//   L293D EN1,2 (pin 1)  -> Uno pin 5 (PWM: the speed)
//   L293D IN1 (pin 2)    -> Uno pin 3
//   L293D IN2 (pin 7)    -> Uno pin 4
//   L293D OUT1 and OUT2  -> the motor's two leads
//   L293D VSS (pin 16)   -> 5V, the chip's logic supply
//   L293D VS (pin 8)     -> the motor supply (the breadboard power module)
//   L293D GND pins       -> GND, shared with the Uno
//
// An Uno pin gives about 20 mA; a small motor wants several hundred. The
// L293D is an H-bridge: the Uno only tells it which way and how fast, and it
// switches the motor's own supply. IN1 HIGH and IN2 LOW turn the motor one
// way, the reverse turns it the other, and both the same stop it. The enable
// pin gates both, so analogWrite() on it sets the speed.
//
// Never run a motor from an Uno pin directly: the current spike when it starts
// can reset the board or damage the pin.

const int ENABLE = 5;
const int DIRA = 3;
const int DIRB = 4;

void drive(bool forward, int speed, const char *what) {
  digitalWrite(DIRA, forward ? HIGH : LOW);
  digitalWrite(DIRB, forward ? LOW : HIGH);
  analogWrite(ENABLE, speed);
  Serial.println(what);
}

void stopMotor() {
  analogWrite(ENABLE, 0);
  Serial.println("stop");
}

void setup() {
  pinMode(ENABLE, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: DC motor on an L293D");
}

void loop() {
  drive(true, 255, "forward, full speed");
  delay(2000);
  stopMotor();
  delay(1000);
  drive(false, 255, "backward, full speed");
  delay(2000);
  stopMotor();
  delay(1000);
  drive(true, 140, "forward, slow");
  delay(2000);
  drive(true, 255, "forward, fast");
  delay(2000);
  stopMotor();
  delay(2000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

drive sketch.ino

Takes a direction, a speed and a message. It sets DIRA and DIRB to opposite levels, so the motor turns one way or the other. Then analogWrite on ENABLE sets the speed, and the message goes to serial.

void drive(bool forward, int speed, const char *what) {
  digitalWrite(DIRA, forward ? HIGH : LOW);
  digitalWrite(DIRB, forward ? LOW : HIGH);
  analogWrite(ENABLE, speed);
  Serial.println(what);
}

stopMotor sketch.ino

Writes 0 to ENABLE, which switches the driver's output off and lets the motor stop.

void stopMotor() {
  analogWrite(ENABLE, 0);
  Serial.println("stop");
}

loop sketch.ino

Runs a fixed routine: forward at 255 for two seconds, stop, backward at 255, stop, then forward at 140 and at 255, and a final stop.

void loop() {
  drive(true, 255, "forward, full speed");
  delay(2000);
  stopMotor();
  delay(1000);
  drive(false, 255, "backward, full speed");
  delay(2000);
  stopMotor();
  delay(1000);
  drive(true, 140, "forward, slow");
  delay(2000);
  drive(true, 255, "forward, fast");
  delay(2000);
  stopMotor();
  delay(2000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The motor never turns This project
    Check EN1,2 goes to pin 5, and that VS and VSS both have 5V and GND is shared.
  • The motor only spins one way This project
    Check IN1 goes to pin 3 and IN2 to pin 4. The two direction pins must change together.
  • You want to skip the driver This project
    Do not run a motor from an Uno pin. The starting current spike can reset the board or damage the pin.
  • The motor is weak or does not turn, and the chip gets hot. Motor driver
    The L293D gives about 600 mA per channel and drops about 1.4 V per side. Use a motor that fits, and for bigger motors pick another driver.
  • Nothing moves even though the inputs are right. Motor driver
    Power both supply pins: VCC1 (pin 16) at 5 V for logic and VCC2 (pin 8) for the motor. Tie the enable pin high, and join all grounds.
  • The motor does not spin, or the pin gets hot or the board resets when it is wired straight to a GPIO pin. DC motor
    A pin gives about 20 mA and a motor needs far more. Drive it through a transistor or motor driver, with its own supply and a shared ground.
  • The transistor or pin is damaged by spikes when the motor stops. DC motor
    Put a flyback diode across the motor, cathode to the positive side. A motor is a coil and kicks back voltage when switched off. Add a 0.1 uF capacitor across the motor terminals to cut noise.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.