Built-in project · Raspberry Pi Pico

MicroPython table lamp on a Pico: a button toggles an LED

MicroPython on a Raspberry Pi Pico: each press of the button on GP13 turns the LED on GP15 on or off, with the contact bounce filtered out. The whole build, a Raspberry Pi Pico 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 $4.35. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Toggling an LED on each press
  • Debouncing with a short wait
  • Waiting for a button release
  • Pull-up resistor on a button
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico
  • Parts: 7

How it works

The button on GP13 has a 10k resistor to 3.3 V, so the pin reads 1 until pressed and 0 while held. The LED is on GP15 with a 220 Ω resistor to ground.

When the program sees a 0, it waits 20 ms and looks again. If the pin is still 0, it is a real press and not bounce. The program toggles the LED, prints whether the lamp is on or off, and waits until you release the button. One press flips the lamp once.

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 1Raspberry Pi Pico pin GP13
Resistor, 220 Ω pin 1Raspberry Pi Pico pin GP15
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 · Raspberry Pi Pico
# A table lamp: each press of the button on GP13 turns the LED on GP15 on
# or off. A press counts once the contacts have settled for 20 ms, so a
# bouncing contact never flips the lamp twice.
from machine import Pin
import time

led = Pin(15, Pin.OUT)
button = Pin(13, Pin.IN)

while True:
    if button.value() == 0:
        time.sleep_ms(20)
        if button.value() == 0:
            led.toggle()
            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 flips twice on a press This project
    That is bounce. Keep the 20 ms wait, or make it a little longer.
  • The LED never lights This project
    Check its direction: anode (A) toward the resistor and GP15, 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.