Built-in project · Raspberry Pi Pico

MicroPython button on GP13 lights an LED on GP15, on a Raspberry Pi Pico

MicroPython on a Raspberry Pi Pico: a push button on GP13 with a 10k pull-up lights the LED on GP15 for as long as it is held. 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

  • Reading a pulled-up button in MicroPython
  • An outside pull-up resistor on the breadboard
  • A pressed button reads 0
  • Printing only when something changes
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico
  • Parts: 7

How it works

GP13 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. GP15 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.

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
# The LED on GP15 is lit while the button on GP13 is held.
# The button pulls GP13 to ground through 10k, so held reads 0.
from machine import Pin
import time

led = Pin(15, Pin.OUT)
button = Pin(13, Pin.IN)       # the 10k pull-up is on the breadboard
was = None

while True:
    held = button.value() == 0
    led.value(held)
    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
    Held means the pin reads 0, so keep the test as button.value() == 0.
  • The button reads random values. This project
    The code does not turn on a pull-up. The 10k pull-up resistor on the breadboard must connect to 3.3 V.
  • 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.