🛠 Bouton poussoir et messages dans la console

Raspberry Pi 4 Model B (2019) — Board Sheet · debutant · Schéma : IMG-model-4b-debutant-2-model4b-bouton-console.svg — Bouton entre GPIO27 (broche 13) et GND avec pull-up interne, LED sur GPIO17 (broche 11) avec résistance 330 ohms

Objectives

Wire a push button, understand the internal pull-up, and display a message in the console on each press. You will write two versions: polling and event-driven, the latter being the method recommended by the official gpiozero documentation.

Materials

Complete Code

#!/usr/bin/env python3
# bouton.py - GPIO17 (broche 11) -> 330R -> LED -> GND ; GPIO27 (broche 13) <- bouton <- GND
from gpiozero import LED, Button
from signal import pause

led = LED(17)                       # sortie active a l'etat haut
btn = Button(27, pull_up=True)      # pull-up interne, appui actif a l'etat bas

while True:
    if btn.is_pressed:
        print("Bouton appuye !")
        led.on
    else:
        led.off

# Version evenementielle recommandee :
# btn.when_pressed = led.on
# btn.when_released = led.off
# pause

Expected Verification

Run python3 bouton.py then press the button: the message "Bouton appuye !" is displayed and the LED lights up; it turns off when released. Ctrl+C terminates the script cleanly.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials