🛠 GPIO en Python : LED et bouton avec gpiozero
Objectives
Control the 40-pin HAT-compatible connector on the Zero 2 W: 3.3 V logic (careful, not 5 V!), Broadcom numbering (GPIO17 = physical pin 11), inputs with internal pull-up, and the event-driven model of gpiozero — preinstalled on Raspberry Pi OS — which avoids blocking polling. Current constraints to remember: 16 mA per pin, 50 mA TOTAL across all pins.
Hardware
- 1 powered Raspberry Pi Zero 2 W (2 A)
- 1 LED + 1 330 Ω resistor: anode → physical pin 11 (GPIO17), cathode → GND (pin 6, 9, 14, 20, 25 or 30)
- 1 push button: physical pin 13 (GPIO27) → button → GND (internal pull-up used)
- Optionally a solder-on header: the standard Zero 2 W comes with the connector NOT crimped (the "with headers" version is pre-soldered)
Complete code
#!/usr/bin/env python3
"""LED sur GPIO17, compteur d'appuis sur bouton GPIO27 (gpiozero).
Rappel : 16 mA max par broche, 50 mA au total sur toutes les broches."""
from gpiozero import LED, Button
from signal import signal, SIGINT, default_int_handler
led = LED(17) # numérotation BCM, pas numéro de broche !
button = Button(27, pull_up=True) # au repos = HIGH ; appuyé = relié à GND
presses = 0
def on_press(_):
global presses
presses += 1
print(f"Bouton appuyé {presses} fois")
button.when_pressed = on_press # événementiel, pas de boucle de polling
signal(SIGINT, default_int_handler) # Ctrl-C propre au lieu de rester bloqué
try:
led.blink(on_time=0.5, off_time=0.5) # non bloquant : thread en arrière-plan
while True:
pass
finally:
led.close # remet la broche à 0 à la sortie
Execution: python3 bouton.py, stop with Ctrl-C. To start it at boot: a simple systemd service with ExecStart=/usr/bin/python3 /home/pi/bouton.py.
Expected verification
The LED blinks at 1 Hz upon launch; each press displays a counter in the terminal. After Ctrl-C, the LED turns off (the pin is properly released by led.close). If nothing blinks, first check the physical pin / GPIO number inversion.
Common errors
- Confusing physical pin 17 and GPIO17: gpiozero speaks Broadcom; physical pin 17 is a 3.3 V pin!
- Powering an LED at 3.3 V without a resistor or drawing more than 16 mA: the pin heats up, beyond 50 mA cumulative the SoC risks damage.
- Connecting a 5 V Arduino sensor directly: the Pi's logic is 3.3 V, the pins are not 5 V tolerant (unless the module indicates otherwise).
- gpiozero vs RPi.GPIO vs libgpiod: three worlds; do not mix libraries on the same pin without releasing the line.
- Running the script as root "to make it work": unnecessary, the pi user is in the gpio group; look for permissions elsewhere.
Other tutorials for this board
- Premier démarrage de la Zero 2 W sans écran (headless)
- Se connecter au Wi-Fi (et comprendre les limites 2,4 GHz / WPA2)
- Surveiller la température et la fréquence de la Zero 2 W
- Le tour de passe-passe « clé USB Linux » : Ethernet sur USB (g_ether)
- Serveur de blocage de pub réseau avec Pi-hole
- Remettre une Zero 2 W en état de boot : usbboot et boot files
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash