🛠 Premier script Python : piloter les LED système
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
- Orange Pi PC running Armbian (Debian Trixie or Ubuntu Resolute)
- 5 V / 2 A power supply (DC jack only)
- Terminal access: SSH, HDMI screen, or serial 115200 8N1 on the 3-pin connector
- A text editor: nano or vim
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
- Running without write permissions on /sys/class/leds: use sudo or add your user to the gpio group via a udev rule.
- Writing to brightness while an active trigger other than none is set: the trigger overwrites your value, hence the trigger=none step.
- Typing orangepi:red:status incorrectly: the exact name comes from the device tree sun8i-h3-orangepi-pc.dts.
- Believing the micro-USB OTG port can power the board during tests: it cannot, the board remains off.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash