🛠 GPIO en Python : LED et bouton avec gpiozero
Goal
Drive the Zero's 40-pin GPIO from Python: an LED and a button with gpiozero. The Zero's header is electrically identical to the Model A+/B+/2B — 3.3 V logic, Broadcom numbering — but it ships unsoldered, so grab a soldering iron first.
Wiring
- Solder a 40-pin header (or use a pHAT / clamp connector)
- LED + 330 Ohm resistor: physical pin 11 (GPIO17) -> resistor -> LED anode, cathode to GND (e.g. pin 6)
- Push button: physical pin 13 (GPIO27) to GND, using the internal pull-up
- Current budget: 16 mA per pin, 50 mA TOTAL across all pins
Code
#!/usr/bin/env python3
# sudo apt install python3-gpiozero
from gpiozero import LED, Button
import signal
led = LED(17) # BCM numbering: GPIO17 = physical pin 11
btn = Button(27) # GPIO27 = physical pin 13, button wired to GND
btn.when_pressed = led.on # event-driven, no polling loop
btn.when_released = led.off
def bye(s, f):
led.off
raise SystemExit
signal.signal(signal.SIGINT, bye)
signal.pause # block forever; the Zero idles at ~0.1 A while waiting
Run with python3 bouton.py, stop with Ctrl-C.
Check
The LED lights while the button is held and goes off on release; after Ctrl-C the LED is cleanly off.
Pitfalls
- Physical pin 17 is a 3.3 V supply pin, not GPIO17 — BCM numbers are not pin numbers.
- Feeding 5 V sensor outputs into the header: the logic is 3.3 V and NOT 5 V tolerant.
- No ADC/DAC on this board: analog sensors need an external chip (e.g. MCP3008 over SPI).
- Exceeding 16 mA per pin or 50 mA total damages the SoC — drive relays/motors via a transistor.
Other tutorials for this board
- Premier démarrage de la Zero avec écran (mini-HDMI)
- Installation headless : la Zero en carte réseau USB (g_ether)
- Alimentation et consommation : le point faible de la Zero
- Console série sur USB : la Zero en port série (CDC-ACM)
- Camera CSI sur Zero v1.3 : le nappe 22 broches
- Sauver une Zero qui ne boot plus : le flux legacy sans EEPROM
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash