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 About 20 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.
What you'll learn
- Driving a motor through an L293D
- PWM to set a motor's speed
- Cycling through values with the % operator
- Debouncing a button with millis()
- A pull-down resistor on a button
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
An L293D chip drives the DC motor, because an Arduino pin cannot supply enough current. Its enable pin is tied to 5V, so it is always on. Pins 9 and 10 are the two inputs. The sketch keeps pin 9 at 0 and puts PWM on pin 10, so the motor spins one way at that speed.
Each press of the button on pin 2 steps through four speeds: off, slow, medium and fast, with PWM values 0, 150, 200 and 250. The Uno's own LED on pin 13 follows the button. A press only counts after it has been steady for 50 ms.
Parts list
6 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Motor driver, L293DIn Claw Machine Kit.
- 1 × DC motorIn Robot Car Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Kits that can build it
These 8 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 pin | Connects to |
|---|---|
| Motor driver, L293D pin IN2 | Arduino Uno R3 pin 10 |
| Motor driver, L293D pin IN1 | Arduino Uno R3 pin 9 |
| Pushbutton pin 2a | Arduino Uno R3 pin 2 |
| Motor driver, L293D pin EN12 | Arduino Uno R3 pin 5V |
| Motor driver, L293D pin VS | Arduino Uno R3 pin 5V |
| Motor driver, L293D pin VSS | Arduino Uno R3 pin 5V |
| Pushbutton pin 1a | Arduino Uno R3 pin 5V |
| Motor driver, L293D pin GND1 | Arduino Uno R3 pin GND |
| Resistor, 10k Ω pin 1 | Arduino Uno R3 pin GND |
| DC motor pin 1 | Motor driver, L293D pin OUT1 |
| DC motor pin 2 | Motor driver, L293D pin OUT2 |
| Resistor, 10k Ω pin 2 | Pushbutton pin 2b |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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]);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets the button as an input and the LED and both motor pins as outputs. Then it opens the serial port and prints how to use the fan. The motor starts off, because speed begins at 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.");
}loop sketch.ino
Reads the button and remembers when it last changed. Once the reading has held for over 50 ms it is accepted, and the LED follows it. A new press moves speed to the next of four steps and prints its name. At the end of every pass it writes 0 to IN1 and SPEEDS[speed] to IN2.
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]);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The motor does not turn This project
Check that the L293D enable pin and its supply pins are on 5V and the motor is across OUT1 and OUT2. - The button seems to skip or double-step This project
The 10k resistor must hold pin 2 at ground, with the other side of the button on 5V. - 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. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.