Built-in project · Arduino Uno R3

Three-speed fan on an L293D (pins 9 and 10), on an Arduino Uno

Each press of the button on pin 2 steps a DC motor on an L293D (inputs on pins 9 and 10) through off, slow, medium and fast; the LED on pin 13 shows each press. The whole build, an Arduino Uno and 4 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.

Three-speed fan on an L293D (pins 9 and 10) · Arduino Uno R3live0.000 s 0.00x
Press the button to change speed
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 desk fan with three speeds: each press of the button steps it up.
//
//   button: one side -> 5V, the other -> pin 2 and 10k to GND
//   L293D 1A -> pin 9   2A -> pin 10   1,2EN -> 5V
//   L293D 1Y and 2Y -> the motor   VCC1 and VCC2 -> 5V   GND -> GND
//   the LED is the Uno's own, on pin 13
//
// The L293D's enable pin is tied high, so the chip is always on and the two
// inputs decide everything: pin 9 held LOW and a PWM value on pin 10 turns the
// motor one way at that speed. The button cycles 0, 150, 200 and 250.
// A press is only believed once it has held for 50 ms (debouncing).

const int BUTTON = 2;
const int LED = 13;
const int IN1 = 9;
const int IN2 = 10;
const int SPEEDS[4] = {0, 150, 200, 250};
const char *NAMES[4] = {"off", "slow", "medium", "fast"};

int speed = 0;
int stable = LOW;
int last = LOW;
unsigned long changedAt = 0;

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: fan. Press the button on pin 2 to change speed.");
}

void loop() {
  int now = digitalRead(BUTTON);
  if (now != last) {
    last = now;
    changedAt = millis();
  }
  if (now != stable && millis() - changedAt > 50) {
    stable = now;
    digitalWrite(LED, stable);
    if (stable == HIGH) {
      speed = (speed + 1) % 4;
      Serial.println(NAMES[speed]);
    }
  }
  analogWrite(IN1, 0);
  analogWrite(IN2, SPEEDS[speed]);
}

Parts list

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

How it is wired

8 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 10; Motor driver, L293D pin IN2
  • Arduino Uno R3 pin 9; Motor driver, L293D pin IN1
  • Arduino Uno R3 pin 2; Pushbutton pin 2a
  • Arduino Uno R3 pin 5V; Motor driver, L293D pin EN12; Motor driver, L293D pin VS; Motor driver, L293D pin VSS; Pushbutton pin 1a
  • Ground: Arduino Uno R3 pin GND; Motor driver, L293D pin GND1; Resistor, 10k Ω pin 1
  • Motor driver, L293D pin OUT1; DC motor pin 1
  • Motor driver, L293D pin OUT2; DC motor pin 2
  • Pushbutton pin 2b; Resistor, 10k Ω 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.