🛠 Premier script Python : piloter les LED système

Orange Pi PC (Allwinner H3) · debutant · Schéma : IMG-orangepi-pc-debutant-3-opipc-premier-script-python.svg — Script Python traversant la couche leds-gpio du noyau Linux pour piloter les LED orangepi:red:status et orangepi:green:pwr de l'Orange Pi PC, sans bibliothèque propriétaire.

Objectives

Write your first Python script on the Orange Pi PC (Armbian, Python 3 preinstalled) without any exotic libraries: the script lists available LEDs, reads their kernel triggers, then takes manual control of the red PA15 LED. You will understand the mainline philosophy: everything is a file in /sys, no vendor driver required.

Hardware

Complete Code

Save as ~/leds.py then run with python3 leds.py:

#!/usr/bin/env python3
"""leds.py — découverte et contrôle des LED de l'Orange Pi PC (mainline)."""
from pathlib import Path
import time

LED = Path("/sys/class/leds/orangepi:red:status")   # PA15, du device tree

def main -> None:
    leds = sorted(p.name for p in Path("/sys/class/leds").iterdir)
    print("LEDs système :", leds)

    triggers = (LED / "available_triggers").read_text.split
    print("Triggers dispo :", triggers)

    # 1) Confier la LED à un trigger kernel (aucun bit-banging utilisateur)
    (LED / "trigger").write_text("heartbeat")
    print("Mode heartbeat pendant 5 s...")
    time.sleep(5)

    # 2) Reprendre la main et clignoter manuellement
    (LED / "trigger").write_text("none")
    for _ in range(10):
        (LED / "brightness").write_text("255")
        time.sleep(0.15)
        (LED / "brightness").write_text("0")
        time.sleep(0.15)
    # Rendre la LED au trigger par défaut (CPU)
    (LED / "trigger").write_text("cpu")
    print("Terminé, LED rendue au noyau.")

if __name__ == "__main__":
    main

Make it executable: chmod +x leds.py && ./leds.py

Expected Verification

The script displays the list of LEDs (orangepi:green:pwr and orangepi:red:status), the red LED pulses in a heartbeat rhythm for 5 seconds, blinks 10 times rapidly, then returns to being a CPU load indicator. No errors or permission denied messages.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials