MicroPythonRaspberry Pi PicoExact chip

MicroPython: button and LED

A button on GP14 toggles an LED on GP15 through an interrupt.

MicroPython: button and LED in Argynix Lab

The code

main.pyPython
# A button on GP14 (to ground, the pull-up inside) toggles the LED on GP15, through an
# interrupt. Click the button while it runs.
from machine import Pin
import time

led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_UP)
presses = 0

def pressed(pin):
    global presses
    presses += 1
    led.toggle()

button.irq(trigger=Pin.IRQ_FALLING, handler=pressed)
while True:
    print("presses:", presses)
    time.sleep(1)