Built-in project · Raspberry Pi Pico

CircuitPython button on a Pico

CircuitPython on a Raspberry Pi Pico: a button on GP14 with the pin’s pull-up lights the board’s LED while it is held and counts each press. The whole build, a Raspberry Pi Pico and 1 more part, 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 Runs in your browser. Free, and no account needed.

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

CircuitPython button on a Pico · Raspberry Pi Picolive0.000 s 0.00x
Press Run, then press the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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.

code.py · Raspberry Pi Pico
# CircuitPython on a Raspberry Pi Pico: a button lights the LED.
# The button joins GP14 to ground. The pin's own pull-up holds it True, so
# a press reads False.
import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.GP14)
button.switch_to_input(pull=digitalio.Pull.UP)

print("Press the button")
presses = 0
was_pressed = False
while True:
    pressed = not button.value
    led.value = pressed
    if pressed and not was_pressed:
        presses += 1
        print("pressed", presses)
    was_pressed = pressed
    time.sleep(0.01)

Parts list

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

How it is wired

2 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Raspberry Pi Pico pin GND; Pushbutton pin 2b
  • Raspberry Pi Pico pin GP14; Pushbutton pin 1a

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.