🛠 Allumer une LED via le GPIO avec libgpiod
Objectives
Turn on and blink an LED from a GPIO pin on the Raspberry Pi Model B Rev 2 (Broadcom BCM2835 SoC, 26-pin P1 connector) using libgpiod, and understand the mapping between physical pin and GPIO number.
Hardware
- Raspberry Pi Model B Rev 2 (512 MB) powered via micro-USB 5 V
- Red LED + 330 Ω resistor, breadboard, jumper wires
- LED on GPIO17 (pin P1-11) via the resistor to GND. The logic level is 3.3 V only: no pin tolerates 5 V as input, and this board has no ADC.
Complete code
# Raspbian legacy 32 bits (ère BCM2835)
sudo apt update && sudo apt install -y gpiod libgpiod2
# >
gpiodetect # doit afficher: gpiochip0 (BCM2835 GPIOs)
gpioinfo gpiochip0 | head -25
# Clignotement: ligne 17 a l'etat 1 pendant 500 ms, 10 fois de suite
sudo gpioset -c gpiochip0 --time=500ms --repeat=10 17=1
Expected verification
gpiodetect displays gpiochip0. The LED blinks ten times at approximately one blink per second, then turns off. In gpioinfo, line 17 switches to output during the pulse.
Common errors
- Confusing GPIO17 (P1-11) with physical pin 17 (3.3 V): short circuit or LED always on.
- Forgetting the resistor: LED destroyed or SoC output overloaded.
- Permission denied on /dev/gpiochip0: run with sudo or add the user to the gpio group.
- Soldering the LED backwards: polarity (long leg = anode) is easy to reverse.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython