button_led.py

Raspberry Pi Model B (Rev 1, 2012) — Board Sheet · File : button_led.py · Language : Python · Use case : Lire un bouton poussoir et réagir par événement (callback gpiozero)

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 26-pin header.

Hardware

A Raspberry Pi Model B revision 1 (BCM2835, 26-pin GPIO header, original through-hole ports, no standard 40-pin P1 connector). 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 or USB stick), then run it with python3 (or bash). These boards often run an old Raspbian: gpiozero is still apt-installable. GPIO pins follow the 26-pin header in the official schematic.

Variants

Use btn.wait_for_press() for blocking reads; hold_time detects long presses. Check the pin on the official P1 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()

← Back to the board