🛠 Faire clignoter une LED avec libgpiod
Objectives
Discover the board's GPIO (40-pin connector, 3.3 V logic) by blinking an LED with libgpiod, the modern method that replaces the old wiringPi. You will understand the difference between physical pin numbers and GPIO numbers (BCM), as well as the current limit per pin (16 mA max per pin, 50 mA for the entire connector).
Hardware
- Raspberry Pi 3 Model B+ type board with Raspberry Pi OS Lite installed
- 1 red LED + 1 330 Ω resistor
- 2 male-to-female Dupont wires
- 5.1 V / 2.5 A power supply recommended (the 3B+ consumes more than the 3B)
Complete Code
Wiring: pin 11 (GPIO17) → 330 Ω resistor → LED anode → LED cathode → pin 14 (GND).
# Installation de libgpiod (présent par défaut sur Raspberry Pi OS récent)
sudo apt update && sudo apt install -y gpiod
# Lister les puces GPIO : gpiochip0 est le connecteur 40 broches
gpiodetect
gpioinfo gpiochip0
# Clignotement : mode sortie, puis basculer l'état 10 fois
sudo gpioset gpiochip0 17=0 # état initial éteint
for i in $(seq 1 10); do
sudo gpioset --hold-period 500ms gpiochip0 17=1
sudo gpioset gpiochip0 17=0
sleep 0.5
done
Expected Verification
The LED blinks approximately once per second for 5 seconds. gpiodetect displays gpiochip0, and gpioinfo gpiochip0 shows line 17 as output while the command is running. No permission errors if you use sudo (or are in the gpio group).
Common Errors
- Confusing physical pin 11 and GPIO17: here they are the same pin, but for other pins the confusion arises quickly — always check against a diagram of the 40-pin connector.
- Forgetting the resistor: the LED burns out or draws too much current from the pin (16 mA limit).
- LED inserted backwards: the cathode (short leg) must go to GND.
gpiodetectreturns empty: kernel too old or exotic image without GPIO driver.- Powering the LED from 5 V instead of the GPIO: the GPIO line will not detect any change.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython