🛠 Bouton poussoir et retour console avec gpiozero

Raspberry Pi 3 Model A+ (2018) — Board Sheet · debutant · Schéma : IMG-model-3a-plus-debutant-2-model3a-bouton-console.svg — Raspberry Pi 3 A+ : bouton entre GPIO27 (broche 13) et GND (broche 20), LED + 330 Ω entre GPIO17 (broche 11) et GND (broche 14)

Objectives

Read a push button and control an LED from Python using gpiozero on the Raspberry Pi 3 A+: when_pressed / when_released events, internal pull-up, event loop. gpiozero numbers pins in Broadcom format, just like libgpiod.

Hardware

Complete Code

#!/usr/bin/env python3
# blink_btn.py — sudo apt install python3-gpiozero
from gpiozero import LED, Button   # numérotation Broadcom
from signal import pause

led = LED(17)                       # GPIO17 = broche physique 11
btn = Button(27, pull_up=True)      # GPIO27 = broche 13, repos à l'état haut

def on_press:
    print("Bouton pressé — la LED clignote")
    led.blink(on_time=0.2, off_time=0.2)

def on_release:
    print("Bouton relâché — LED éteinte")
    led.off

btn.when_pressed = on_press
btn.when_released = on_release

print("3A+ prêt (4x Cortex-A53 @ 1,4 GHz) — appuyez sur le bouton")
pause   # garde la boucle d'événements alive
sudo apt install -y python3-gpiozero
python3 blink_btn.py

Expected Verification

The console displays "3A+ ready"; each time the button is pressed, the LED starts blinking and a message is displayed; when released, the LED turns off. Ctrl+C exits cleanly.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials