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 Runs in your browser. Free, and no account needed.

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.

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

Parts list

4 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5; Motor driver, L293D pin EN12
  • Arduino Uno R3 pin 4; Motor driver, L293D pin IN2
  • Arduino Uno R3 pin 3; Motor driver, L293D pin IN1
  • Arduino Uno R3 pin 5V; Motor driver, L293D pin VS; Motor driver, L293D pin VSS
  • Ground: Arduino Uno R3 pin GND; Motor driver, L293D pin GND1
  • Motor driver, L293D pin OUT1; DC motor pin 1
  • Motor driver, L293D pin OUT2; DC motor pin 2

Change it and keep it

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