🛠 Premier script Python : piloter la LED depuis Python
Objectives
Write your first Python script on the ODROID-N2: turn a LED on and off controlled from Python via libgpiod (gpiod module), reusing the circuit from the LED tutorial. The S922X (4× Cortex-A73 + 2× Cortex-A53) runs Python 3 effortlessly under Armbian.
Hardware
- ODROID-N2/N2+ running Armbian, connected via SSH or screen and keyboard (HDMI 2.0)
- LED + 330 Ω resistor on PIN_23, GND on PIN_25
sudo apt install python3-gpiod(orpip install gpiozerodepending on the image)
Complete code
#!/usr/bin/env python3
"""clignotant.py — LED sur PIN_23 (GPIOX_11) via libgpiod, ODROID-N2."""
import gpiod, time
def main:
with gpiod.Chip("gpiochip0") as chip:
line = chip.get_line(chip.line_offset_to_name("PIN_23"))
line.request(consumer="clignotant", type=gpiod.LINE_REQ_DIR_OUT,
default_val=0)
try:
for i in range(10):
line.set_value(1); time.sleep(0.5)
line.set_value(0); time.sleep(0.5)
print(f"cycle {i + 1}/10")
finally:
line.release
if __name__ == "__main__":
main
Expected verification
python3 clignotant.py prints cycle 1/10 to cycle 10/10 over 10 seconds while the LED blinks at 1 Hz, then the script terminates cleanly and the LED remains off. In case of AttributeError, check the libgpiod API version (python3 -c "import gpiod; print(gpiod.__version__)").
Common errors
ModuleNotFoundError: gpiod: installpython3-gpiod, notpip install gpiod(which is not the official binding).PermissionErroron gpiochip0: missinggpiogroup or need to run with sudo.line_offset_to_namefails: the device-tree of the image does not exposePIN_xxnames; list the lines withgpioinfo gpiochip0and use the numeric offset.- Forgetting
line.release: the line remains occupied and blocks subsequent scripts. - Confusing libgpiod v1 (
Chip.find_by_name,gpio_line_request_output) and v2 (get_line) APIs: adapt to the installed binding.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash