Built-in project · Arduino Uno R3

Spinning zoetrope on an Arduino Uno

An L293D runs a DC motor from a 9 V battery: one button starts and stops it, one reverses it, and a potentiometer sets its speed. The whole build, an Arduino Uno and 8 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 35 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • Driving a DC motor with an H-bridge
  • Reversing a motor by swapping two inputs
  • Speed control with PWM on the enable pin
  • Detecting a button press, not just a held button
  • Keeping motor power separate from logic power
  • Difficulty: Intermediate
  • Time: About 35 minutes
  • Board: Arduino Uno R3
  • Parts: 10

How it works

An L293D chip lets the Uno control a motor that needs more power than a pin can give. The motor is wired to the chip's OUT1 and OUT2 and powered from the battery on VS. The Uno only sends signals: pins 3 and 2 choose the direction, and pin 9 turns the chip on with PWM to set the speed.

One button on pin 5 starts and stops the motor. Another on pin 4 flips its direction. Each acts once per press. A knob on A0 sets the speed from slow to full.

Kits that can build it

These 5 starter kits have every part in the circuit. Each kit's page lists what else it can build.

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 GND1Arduino Uno R3 pin GND
Resistor, 10k Ω (1) pin 1Arduino Uno R3 pin GND
Resistor, 10k Ω (2) pin 1Arduino Uno R3 pin GND
Potentiometer pin 1Arduino Uno R3 pin GND
Battery: AA, coin cell, LiPo pin V-Arduino Uno R3 pin GND
Motor driver, L293D pin EN12Arduino Uno R3 pin 9
Pushbutton (1) pin 2aArduino Uno R3 pin 5
Pushbutton (2) pin 2aArduino Uno R3 pin 4
Motor driver, L293D pin IN1Arduino Uno R3 pin 3
Motor driver, L293D pin IN2Arduino Uno R3 pin 2
Motor driver, L293D pin VSSArduino Uno R3 pin 5V
Pushbutton (1) pin 1aArduino Uno R3 pin 5V
Pushbutton (2) pin 1aArduino Uno R3 pin 5V
Potentiometer pin 3Arduino Uno R3 pin 5V
Potentiometer pin 2Arduino Uno R3 pin A0
DC motor pin 1Motor driver, L293D pin OUT1
DC motor pin 2Motor driver, L293D pin OUT2
Battery: AA, coin cell, LiPo pin V+Motor driver, L293D pin VS
Resistor, 10k Ω (1) pin 2Pushbutton (1) pin 2b
Resistor, 10k Ω (2) pin 2Pushbutton (2) pin 2b

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
// Spinning zoetrope: an L293D H-bridge runs a DC motor either way, at a speed
// set by a knob, with one button for on and off and one for direction.
//
//   L293D   EN12 -> pin 9 (speed, PWM), IN1 -> pin 3, IN2 -> pin 2
//           OUT1 and OUT2 -> the motor, VS -> the 9 V battery's +
//           VSS -> 5V, all four GND pins -> GND
//   pin 5   the on/off button, with a 10k pull-down
//   pin 4   the direction button, with a 10k pull-down
//   A0      the potentiometer's wiper (ends to 5V and GND)
//
// An H-bridge is four switches around the motor. IN1 high and IN2 low lets
// current through one way; swap them and it flows the other way, and the motor
// turns backward. EN12 switches the whole half-bridge on, so PWM on it sets
// the speed.
//
// Each button changes something on the press, not while it is held, so the
// sketch remembers the last reading and acts on the change from LOW to HIGH.

const int ENABLE = 9;
const int IN1 = 3;
const int IN2 = 2;
const int ON_OFF = 5;
const int DIRECTION = 4;

bool running = false;
bool forward = true;
int lastOnOff = LOW;
int lastDirection = LOW;

void report() {
  Serial.print(running ? "running" : "stopped");
  Serial.println(forward ? ", forward" : ", backward");
}

void setup() {
  pinMode(ENABLE, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ON_OFF, INPUT);
  pinMode(DIRECTION, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: spinning zoetrope");
  report();
}

void loop() {
  int onOff = digitalRead(ON_OFF);
  if (onOff == HIGH && lastOnOff == LOW) {
    running = !running;
    report();
  }
  lastOnOff = onOff;

  int direction = digitalRead(DIRECTION);
  if (direction == HIGH && lastDirection == LOW) {
    forward = !forward;
    report();
  }
  lastDirection = direction;

  int speed = analogRead(A0) / 4;
  digitalWrite(IN1, forward ? HIGH : LOW);
  digitalWrite(IN2, forward ? LOW : HIGH);
  analogWrite(ENABLE, running ? speed : 0);
  delay(10);
}

Code walkthrough

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

report sketch.ino

Prints the motor's state to serial: running or stopped, followed by forward or backward. It runs at startup and again after each button press.

void report() {
  Serial.print(running ? "running" : "stopped");
  Serial.println(forward ? ", forward" : ", backward");
}

setup sketch.ino

Sets ENABLE, IN1 and IN2 as outputs and the two buttons as inputs. The buttons use external 10k pull-down resistors, so they read LOW until pressed. Then it prints a greeting and the starting state.

void setup() {
  pinMode(ENABLE, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ON_OFF, INPUT);
  pinMode(DIRECTION, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: spinning zoetrope");
  report();
}

loop sketch.ino

Reads both buttons and compares each to its last reading, so a toggle happens only on the change from LOW to HIGH. Then it reads the knob, divides by 4 to fit 0 to 255, and sets IN1 and IN2 for the direction. analogWrite sends the speed to the enable pin, or 0 when stopped.

void loop() {
  int onOff = digitalRead(ON_OFF);
  if (onOff == HIGH && lastOnOff == LOW) {
    running = !running;
    report();
  }
  lastOnOff = onOff;

  int direction = digitalRead(DIRECTION);
  if (direction == HIGH && lastDirection == LOW) {
    forward = !forward;
    report();
  }
  lastDirection = direction;

  int speed = analogRead(A0) / 4;
  digitalWrite(IN1, forward ? HIGH : LOW);
  digitalWrite(IN2, forward ? LOW : HIGH);
  analogWrite(ENABLE, running ? speed : 0);
  delay(10);
}

Troubleshooting

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

  • The motor never moves, though the serial monitor says running. This project
    Check the L293D has both supplies: VSS to 5V for logic and VS to the battery's plus for the motor. All grounds must be joined.
  • A button toggles randomly or does nothing. This project
    Each button needs its 10k resistor to ground. Without it the pin floats when the button is open.
  • 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 input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.

Change it and keep it

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