Built-in project · ESP32-DevKitC V4

MicroPython button on GPIO 13 lights an LED, on an ESP32-DevKitC V4

MicroPython on an ESP32: a push button on GPIO 13 with a 10k pull-up lights the LED on GPIO 2 for as long as it is held. 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

  • Reading a pulled-up button in MicroPython
  • The machine.Pin class for inputs and outputs
  • A pressed button reads 0
  • Printing only when something changes
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 7

How it works

GPIO 13 is tied to 3.3 V through two 10k resistors and to ground through the button. Not pressed, the pin reads 1. Pressed, the button pulls it to ground and it reads 0. GPIO 2 drives the red LED through a 220 ohm resistor.

The program checks the button every 10 ms. While it is held, the LED is on. It prints held or released only when the state changes.

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
# Button and LED: the LED on GPIO 2 is lit while the button on GPIO 13
# is held. The button pulls GPIO 13 to ground, so held reads 0.
from machine import Pin
import time

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

while True:
    held = button.value() == 0
    led.value(1 if held else 0)
    if held != was:
        print("held" if held else "released")
        was = held
    time.sleep_ms(10)

Troubleshooting

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

  • The LED is on when the button is not pressed. This project
    The button pulls the pin to ground, so held means button.value() == 0. Keep that test.
  • The LED never lights. This project
    Check the LED direction. The anode (A) goes to the resistor from GPIO 2, and the cathode (C) goes 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.