🛠 Premier script Python : bouton + LED avec python3-libgpiod
Objectives
Write your first Python script on the Orange Pi 3B: read a button and mirror its state to an LED using python3-libgpiod, leveraging interrupts (edge events) instead of polling in a loop.
Hardware
- Orange Pi 3B running Armbian, 5 V / 3 A
- 1 push button + 10 kΩ resistor, 1 LED + 330 Ω resistor
sudo apt install python3-libgpiod
Full code
#!/usr/bin/env python3
"""Bouton sur la broche d'entrée, LED en miroir (Orange Pi 3B / Armbian)."""
import gpiod
from gpiod.line import Direction, Edge, Value
CHIP = "/dev/gpiochip1"
BUTTON = 35 # gpio1_b3 en exemple — choisir une broche libre du plan officiel
LED = 36 # gpio1_b4 en exemple
with gpiod.Chip(CHIP) as chip:
request = gpiod.Request(
consumer="opi3b-demo",
lines={
BUTTON: gpiod.RequestLineConfig(direction=Direction.INPUT,
edge=Edge.BOTH),
LED: gpiod.RequestLineConfig(direction=Direction.OUTPUT,
output_value=Value.INACTIVE),
},
)
with chip.request_lines(config=request) as req:
print("Ctrl-C pour sortir")
while True:
req.wait_edge_events(timeout=1.0) # bloque sur l'interruption
for ev in req.process_edge_events:
if ev.line_offset == BUTTON:
req.set_value(LED, ev.value) # la LED suit le bouton
Expected verification
sudo python3 blink.py starts without errors; each button press turns the LED on, and releasing it turns the LED off. Ctrl-C cleanly terminates the script and releases the pins.
Common errors
- Older versions of python3-libgpiod use
LineSettings/request_linesinstead ofRequest: same pin numbers, different API — check the Armbian package version. - Reusing the same pin in two scripts without releasing the first one: "line is busy" error.
- Confusing the line number (offset within the gpiochip) with the physical pin number on the 40-pin connector: only the former matters to libgpiod.
- Forgetting to connect the common ground between the breadboard and the board: the LED never turns off completely.
Other tutorials for this board
- Faire clignoter une LED via le GPIO avec libgpiod
- Lire un bouton poussoir et afficher l'état dans la console
- Variation d'intensité d'une LED avec un signal PWM
- Interroger un capteur I2C sur les broches 3/5 du connecteur
- Configurer Ethernet gigabit et Wi-Fi puis se connecter en SSH
- Booter sur un SSD NVMe et récupérer une carte bloquée (MaskROM)
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash