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

Spinning zoetrope · Arduino Uno R3live0.000 s 0.00x
Hold the button
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
// 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);
}

Parts list

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

How it is wired

13 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.

  • Ground: Arduino Uno R3 pin GND; Motor driver, L293D pin GND1; Resistor, 10k Ω (1) pin 1; Resistor, 10k Ω (2) pin 1; Potentiometer pin 1; Battery, 9 V, AA or 18650 pin V-
  • Arduino Uno R3 pin 9; Motor driver, L293D pin EN12
  • Arduino Uno R3 pin 5; Pushbutton (1) pin 2a
  • Arduino Uno R3 pin 4; Pushbutton (2) pin 2a
  • Arduino Uno R3 pin 3; Motor driver, L293D pin IN1
  • Arduino Uno R3 pin 2; Motor driver, L293D pin IN2
  • Arduino Uno R3 pin 5V; Motor driver, L293D pin VSS; Pushbutton (1) pin 1a; Pushbutton (2) pin 1a; Potentiometer pin 3
  • Arduino Uno R3 pin A0; Potentiometer pin 2
  • Motor driver, L293D pin OUT1; DC motor pin 1
  • Motor driver, L293D pin OUT2; DC motor pin 2
  • Motor driver, L293D pin VS; Battery, 9 V, AA or 18650 pin V+
  • Pushbutton (1) pin 2b; Resistor, 10k Ω (1) pin 2
  • Pushbutton (2) pin 2b; Resistor, 10k Ω (2) 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.