🛠 Premier script Python : piloter la LED depuis Python

ODROID-N2 / ODROID-N2+ (Hardkernel) · debutant · Schéma : IMG-odroid-n2-debutant-3-odroidn2-premier-python.svg — ODROID-N2 avec LED sur PIN_23 pilotée par un script Python exécuté dans un terminal, mise en évidence du couple script et broche GPIO.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials