🛠 Variation d'intensité d'une LED avec PWM (sysfs pwm)
Objectives
Make an LED "breathe" by varying the duty cycle of a PWM signal via the sysfs interface /sys/class/pwm. The 40-pin connector multiplexes PWM outputs, and the board's green status LED is itself driven by PWM (pwm2 in mainline); the 5 V fan on the dedicated connector is also PWM-controlled (fan idle at 55 °C, full speed at 65 °C in the mainline device-tree).
Hardware
- Powered Orange Pi 5 Plus (5 V / 4 A, Type-C on the Ethernet side)
- LED + 330 Ω resistor on a PWM-multiplexed pin of the connector (consult the official "Pin Definition" diagram; the connector multiplexes PWM among UART/I2C/SPI/CAN/I2S)
- Terminal; no packages required (sysfs PWM is in the kernel)
Complete code
# 1. Trouver le contrôleur PWM exposé (pwmchipN)
cat /sys/class/pwm/pwmchip*/uevent 2>/dev/null
# Attacher le canal 0 du premier contrôleur (adapter le numéro à votre carte) :
echo 0 | sudo tee /sys/class/pwm/pwmchip0/export
P=/sys/class/pwm/pwmchip0/pwm0
echo 20000000 | sudo tee $P/period # période 20 ms -> 50 Hz
echo 0 | sudo tee $P/duty_cycle # 0 % : LED éteinte
echo 1 | sudo tee $P/enable # activer la sortie
# 2. Respiration : montée 0 -> 100 % puis descente, par pas de 1 %
while :; do
for d in $(seq 0 200000 20000000); do echo $d | sudo tee $P/duty_cycle >/dev/null; sleep 0.02; done
for d in $(seq 20000000 -200000 0); do echo $d | sudo tee $P/duty_cycle >/dev/null; sleep 0.02; done
done
# Stop : Ctrl-C, puis echo 0 | sudo tee $P/enable
# Nettoyage : echo 0 | sudo tee /sys/class/pwm/pwmchip0/unexport
Expected verification
The LED brightness continuously rises and falls (breathing cycle of approximately 1.6 s). cat $P/duty_cycle follows the written values; the frequency remains at 50 Hz (period 20,000,000 ns) regardless of brightness.
Common errors
- Writing to an unexported pwmchip: "Permission denied" on
/exportmeans the controller is not enabled in the device-tree; check the list of controllers and free channels withsudo cat /sys/kernel/debug/pwm. - Confusing
periodandduty_cycle:periodsets the frequency,duty_cyclesets the intensity;duty_cycle>periodis silently saturated. - Forgetting
enable=1: the output remains inactive, and the LED does not change. - >
Other tutorials for this board
- Allumer une LED via le GPIO 40 broches (libgpiod)
- Lire un bouton poussoir et afficher dans la console
- Premier script Python : fiche signalétique de la carte et LED
- Lire un capteur I2C (BMP280) depuis le bus du connecteur
- Exploiter le double Ethernet 2,5 GbE et sécuriser le SSH
- Booter sur NVMe (M.2 PCIe 3.0 x4) et récupérer une carte brique via MaskROM
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash