Built-in project · ESP32-DevKitC V4

MicroPython table lamp: a button toggles an LED, on an ESP32-DevKitC V4

MicroPython on an ESP32: each press of the button on GPIO 13 turns the LED on GPIO 2 on or off, with the contact bounce filtered out. The whole build, an ESP32-DevKitC V4 and 5 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.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • Toggling an output on each button press
  • Debouncing with a short wait
  • Waiting for the release of a button
  • Reading and flipping a pin's value
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 7

How it works

The button on GPIO 13 has a 10k resistor up to 3.3 V, so the pin reads 1 until you press, and 0 while pressed. The LED sits on GPIO 2 with a 220 Ω resistor to ground.

When the program sees a 0, it waits 20 ms and looks again. If it is still 0, that is a real press and not contact bounce. It flips the LED, prints whether the lamp is on or off, and waits for you to let go. That wait means one press can only flip the lamp once.

Parts list

7 parts, plus the jumper wires. They retail for about $10.35; on Mokxi they're free.

Kits that can build it

These 3 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
Resistor, 10k Ω (2) pin 1ESP32-DevKitC V4 pin 13
Resistor, 220 Ω pin 1ESP32-DevKitC V4 pin 2
LED, red pin AResistor, 220 Ω pin 2
LED, red pin CGround
Resistor, 10k Ω (1) pin 13.3 V
Pushbutton pin 1aResistor, 10k Ω (1) pin 2
Resistor, 10k Ω (2) pin 2Resistor, 10k Ω (1) pin 2
Pushbutton pin 2aGround

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.

main.py · ESP32-DevKitC V4
# A table lamp: each press of the button on GPIO 13 turns the LED on
# GPIO 2 on or off. A press is only counted once the contacts have
# settled for 20 ms, so one press never flips the lamp twice.
from machine import Pin
import time

led = Pin(2, Pin.OUT)
button = Pin(13, Pin.IN, Pin.PULL_UP)

while True:
    if button.value() == 0:
        time.sleep_ms(20)                 # let the contacts settle
        if button.value() == 0:
            led.value(not led.value())
            print("lamp", "on" if led.value() else "off")
            while button.value() == 0:    # wait for the release
                time.sleep_ms(10)
    time.sleep_ms(10)

Troubleshooting

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

  • The lamp flickers or flips twice on one press This project
    That is contact bounce. Keep the 20 ms wait, or make it a little longer.
  • The LED never lights This project
    Check the LED direction: anode (A) toward the resistor and GPIO 2, cathode (C) to ground.
  • 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 LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • 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.