🛠 Allumer une LED via le GPIO 40 broches
Objectives
Discover the 40-pin GPIO connector (J8) on the Raspberry Pi 2 Model B and turn on an LED from the command line: first with the legacy sysfs interface, then with libgpiod, the recommended interface on recent systems. By the end, you will know how to export a pin, configure it as an output, and control it without writing a program.
Hardware
- Raspberry Pi 2 Model B running 32-bit Raspberry Pi OS (Bullseye is the practical limit for this ARMv7 model)
- Breadboard and 2 jumper wires
- 1 red LED + 1 330 Ω resistor
- Pins used: GPIO17 (pin 11 on J8) and GND (pin 6)
Full code
# Méthode 1 : sysfs (ancienne interface, dépréciée mais pédagogique)
echo 17 | sudo tee /sys/class/gpio/export
echo out | sudo tee /sys/class/gpio/gpio17/direction
echo 1 | sudo tee /sys/class/gpio/gpio17/value # LED allumée
sleep 2
echo 0 | sudo tee /sys/class/gpio/gpio17/value # LED éteinte
echo 17 | sudo tee /sys/class/gpio/unexport
# Méthode 2 : libgpiod (recommandée)
sudo apt install gpiod
sudo gpioset --time 2000 gpiochip0 17=1 # impulsion de 2 s
Expected verification
The LED turns on when the value is 1 and turns off at 0. With gpioset, the --time 2000 option produces a two-second pulse. The controller name may be gpiochip0 or gpiochip1 depending on the kernel: check with gpioinfo before copying the commands.
Common errors
- Forgetting the resistor: the LED burns out or the pin is stressed beyond its capacity (8 mA max per pin).
- Confusing 3.3 V and 5 V: GPIO logic is 3.3 V, never 5 V.
- Permission denied on /sys/class/gpio: use sudo or add the user to the gpio group.
- sysfs missing or unresponsive: the interface is deprecated, switch to libgpiod.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython