🛠 Variation d'intensité d'une LED avec un signal PWM
Objectives
Vary the intensity of an LED (breathing light effect) using a PWM signal generated by the Orange Pi 3B. The official datasheet indicates PWM on the 40-pin connector; the exact PWM pin to use >
Hardware
- Orange Pi 3B running Armbian (recent kernel), 5 V / 3 A
- 1 power LED or standard LED + appropriate resistor, ideally an NPN transistor (2N2222) if using a power LED
- PWM pin on the 40-pin connector (see warning above), common ground
Complete Code
# 1. Repérer les contrôleurs PWM exposés
ls /sys/class/pwm/ # pwmchip0, pwmchip1...
# 2. Réserver un canal (exemple : canal 0 du pwmchip0)
echo 0 | sudo tee /sys/class/pwm/pwmchip0/export
# 3. Signal : période 20 kHz (en ns), puis rapport cyclique 0..period
sudo sh -c 'echo 50000 > /sys/class/pwm/pwmchip0/pwm0/period'
sudo sh -c 'echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable'
# 4. Respiration lumineuse (fade)
#!/bin/bash
P=/sys/class/pwm/pwmchip0/pwm0
for p in $(seq 0 500 50000); do echo $p | sudo tee $P/duty_cycle >/dev/null; sleep 0.03; done
for p in $(seq 50000 -500 0); do echo $p | sudo tee $P/duty_cycle >/dev/null; sleep 0.03; done
Expected Verification
The LED gradually brightens and then dims. cat $P/duty_cycle follows the written values; the complete cycle lasts approximately 6 seconds.
Common Errors
- Writing to
/sys/class/pwm/...withoutsudo(the files are owned by root) or without exporting the channel first: "Permission denied" / file not found. - Period too large: below ~100 Hz flickering is visible; stay at a few kHz.
- Exceeding the pin's allowable current (a few mA) with a power LED: use a transistor.
- Choosing a pin that is not actually multiplexed for PWM on the RK3566: check the official plan and available device tree overlays.
Other tutorials for this board
- Faire clignoter une LED via le GPIO avec libgpiod
- Lire un bouton poussoir et afficher l'état dans la console
- Premier script Python : bouton + LED avec python3-libgpiod
- Interroger un capteur I2C sur les broches 3/5 du connecteur
- Configurer Ethernet gigabit et Wi-Fi puis se connecter en SSH
- Booter sur un SSD NVMe et récupérer une carte bloquée (MaskROM)
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash