🛠 Allumer une LED via le GPIO avec libgpiod
Objectives
Turn a LED on and off from the GPIO of a Raspberry Pi 3 Model B using the libgpiod library (modern character device API, which replaces the deprecated wiringPi). Understand the difference between BCM numbering (SoC pin number) and the physical pin number on the 40-pin connector, and remember that the Pi 3B's GPIO logic is 3.3 V only — the GPIO pins are NOT 5 V tolerant.
Hardware
- Raspberry Pi 3 Model B running Raspberry Pi OS Lite
- Red LED + 330 Ω resistor (additional 220 Ω to ground is possible)
- Breadboard and Dupont cables
- 5 V micro-USB 2.5 A power supply recommended
libgpiod-utilspackage (orgpioddepending on the distribution version)
Full code
The SoC's GPIO controller exposes /dev/gpiochip0 on the Pi 3B. We configure GPIO17 as an output and then make it blink:
# Installation des outils
sudo apt update && sudo apt install -y libgpiod-utils
# Vérifier le nom du chip (gpiochip0 sur Pi 3B)
gpiodetect
# Éteindre proprement le GPIO17 (broche physique 11)
gpioset gpiochip0 17=0
# Clignoter 10 fois : 200 ms allumé, 300 ms éteint
for i in $(seq 1 10); do
gpioset -t 200ms gpiochip0 17=1
sleep 0.3
done
Wiring: physical pin 11 (GPIO17) → 330 Ω resistor → LED anode; LED cathode → 220 Ω resistor → physical pin 14 (GND).
Expected verification
gpiodetect displays gpiochip0 [brcm-gpios] (54 lines). During the loop, the LED blinks approximately twice per second. gpioinfo gpiochip0 | grep line\ 17 shows the line as output while gpioset is running. Upon Ctrl-C, releasing the line sets the pin back to input (LED off).
Common errors
- Confusing BCM17 with physical pin 17 (which is 3.3 V!): always cross-check with the pinout, BCM17 = physical pin 11.
- Forgetting the resistor: the LED burns out or the pin is overloaded (limit 3.3 V / a few mA per pin).
- Connecting the LED to a 5 V pin (pins 2 and 4): forbidden voltage on the Pi 3B's GPIO pins.
permission deniedon/dev/gpiochip0: be a member of thegpiogroup (sudo usermod -aG gpio $USER) or run withsudo.- Wrong chip name on another board (Banana Pi/Orange Pi of similar form factor): check with
gpiodetectbefore wiring. - Using an old wiringPi tutorial: the API is deprecated, use libgpiod or gpiozero.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython