button_led.py
A push button on GPIO27 drives an LED on GPIO17: gpiozero's when_pressed/when_released callbacks avoid polling. Bounce is filtered with bounce_time=0.05 s. A good starting point for a local interface, mindful of this board's 40-pin header.
Hardware
A Raspberry Pi 5 (quad-core BCM2712 at 2.4 GHz, RP1 I/O chip, 40-pin GPIO, recommended 5 V/5 A USB-C power, two CSI camera ports). One push button, one LED, a 330-ohm resistor, dupont wires.
Wiring
One button leg to GPIO27 (pin 13), the other to GND; the pull-up resistor is internal (pull_up=True). LED on GPIO17.
How it works
gpiozero enables the internal pull-up; pressing ties the pin to GND and fires the callback. pause() keeps the event loop alive.
Running it
Copy the script to the board (scp), then run it with python3. On Bookworm, gpiozero 2.x drives the pins through the RP1 chip; keep gpiozero and python3-rpi.gpio updated (sudo apt full-upgrade). A 5 V/5 A supply is advised.
Variants
Use btn.wait_for_press() for blocking reads; hold_time detects long presses. Check the pin on the official J8 schematic.
button_led.py
# button_led.py — allume une LED quand on appuie sur un bouton.
# Deps: sudo apt install python3-gpiozero
# Cablage : bouton entre GPIO27 et GND, pas de resistance externe (pull-up interne).
from gpiozero import LED, Button
from signal import pause
led = LED(17) # GPIO17, broche 11
btn = Button(27, pull_up=True, bounce_time=0.05) # GPIO27, broche 13
def on_press():
print("Bouton presse")
led.on()
def on_release():
led.off()
btn.when_pressed = on_press
btn.when_released = on_release
print("Appuyez sur le bouton, Ctrl+C pour sortir")
pause()