MicroPython relay switches a motor: button on 15, on an ESP32-DevKitC V4
MicroPython on an ESP32: each press of the button on GPIO 15 flips a relay driven through an S8050 from GPIO 13, and the relay runs a DC motor. The whole build, an ESP32-DevKitC V4 and 10 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 40 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13.05. On Mokxi they're free.
What you'll learn
- Driving a relay through a transistor
- A flyback diode protects against coil spikes
- Toggling an output on each button press
- Debouncing with a short delay
- Separating the control side from the motor side
- Difficulty: Intermediate
- Time: About 40 minutes
- Board: ESP32-DevKitC V4
- Parts: 16
How it works
Each press of the button on GPIO 15 flips a relay, and the relay switches a motor. The button uses the chip's internal pull up, so the pin reads 0 while pressed.
GPIO 13 cannot power a relay coil. It sends a small current through a 1k resistor into a transistor's base, and the transistor then carries the coil current. A diode across the coil absorbs the spike when the relay turns off.
The relay contact connects the motor to 5 V. A green LED on the coil circuit lights when it is on. The program waits 20 ms after the first press to skip bounce, flips the pin, prints "motor on" or "motor off", and waits for release.
Parts list
16 parts, plus the jumper wires. They retail for about $13.05; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 2 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Transistor (NPN or PNP)In LED Cube Kit.
- 1 × DPDT relay
- 1 × DiodeIn Pinball Table Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × DC motor
- 1 × Power, 3.3 V
- 4 × Power, 5 V
Kits that can build it
This starter kit has 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 |
|---|---|
| Resistor, 1k Ω pin 1 | ESP32-DevKitC V4 pin 13 |
| Resistor, 10k Ω (2) pin 1 | ESP32-DevKitC V4 pin 15 |
| Resistor, 10k Ω (1) pin 1 | 3.3 V |
| Pushbutton pin 1a | Resistor, 10k Ω (1) pin 2 |
| Resistor, 10k Ω (2) pin 2 | Resistor, 10k Ω (1) pin 2 |
| Pushbutton pin 2a | Ground |
| Transistor (NPN or PNP) pin B | Resistor, 1k Ω pin 2 |
| DPDT relay pin COIL2 | Transistor (NPN or PNP) pin C |
| Diode pin A | Transistor (NPN or PNP) pin C |
| LED, green pin C | Transistor (NPN or PNP) pin C |
| Transistor (NPN or PNP) pin E | Ground |
| DPDT relay pin COIL1 | 5 V |
| DPDT relay pin COM1 | 5 V |
| DC motor pin 1 | DPDT relay pin NO1 |
| DPDT relay pin NO2 | DPDT relay pin NC1 |
| DPDT relay pin NC2 | DPDT relay pin COM2 |
| Diode pin C | 5 V |
| Resistor, 220 Ω pin 1 | 5 V |
| LED, green pin A | Resistor, 220 Ω pin 2 |
| DC motor pin 2 | Ground |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Relay and motor: each press of the button on GPIO 15 flips the relay.
# GPIO 13 drives the relay's coil through an S8050, and the relay's
# contact runs the motor from 5 V.
from machine import Pin
import time
relay = Pin(13, Pin.OUT, value=0)
button = Pin(15, Pin.IN, Pin.PULL_UP)
while True:
if button.value() == 0:
time.sleep_ms(20)
if button.value() == 0:
relay.value(not relay.value())
print("motor", "on" if relay.value() else "off")
while button.value() == 0:
time.sleep_ms(10)
time.sleep_ms(10)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The relay clicks but the motor stays off This project
Check that the motor is wired to the relay NO contact and that its other side goes to ground. - The relay never clicks This project
Make sure the transistor emitter is grounded and GPIO 13 goes through the 1k resistor to its base. - 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. - 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 load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong. - The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.