button_led.py

Raspberry Pi 3 Model B+ (2018) — 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 40-pin header.

Hardware

A Raspberry Pi 3 Model B+ (quad-core BCM2837B0 at 1.4 GHz, 5 GHz Wi-Fi, Bluetooth 4.2, shielded metal case SoC, 40-pin GPIO). 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 (or bash). Raspberry Pi OS ships gpiozero; otherwise sudo apt install python3-gpiozero. The 40-pin pinout is documented on the official Raspberry Pi site.

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()

← Back to the board