🛠 Variation d'intensité d'une LED avec le PWM
Objectives
Vary the intensity of an external LED on the ODROID-C4 using the PWM output of the Amlogic S905X3 SoC (the 40-pin connector exposes up to 6 PWM channels). You will learn how to export a channel via /sys/class/pwm, reason in terms of period (ns) and duty cycle (duty_cycle), and write a breathing effect in bash.
Hardware
- ODROID-C4 running Armbian current
- 1 LED + 330 Ω resistor (the pins are 3.3 V: never use an LED without a resistor)
- Jumper wires
- >
Complete Code
#!/usr/bin/env bash
# respiration.sh - fade d'une LED sur pwmchip0 / channel 0
# Adapter le numéro de chip/canal selon le brochage confirmé.
CHIP=/sys/class/pwm/pwmchip0
sudo sh -c "echo 0 > $CHIP/export" 2>/dev/null || true
sleep 0.3
CH=$CHIP/pwm0
sudo sh -c "echo 20000 > $CH/period" # période 20 kHz (50 µs)
sudo sh -c "echo 1 > $CH/enable"
for step in $(seq 0 20 100) ; do # montée 0 -> 100 %
duty=$(( 20000 * step / 100 ))
sudo sh -c "echo $duty > $CH/duty_cycle"
sleep 0.05
done
for step in $(seq 100 -20 0) ; do # descente 100 -> 0 %
duty=$(( 20000 * step / 100 ))
sudo sh -c "echo $duty > $CH/duty_cycle"
sleep 0.05
done
sudo sh -c "echo 0 > $CHIP/unexport"
Expected Verification
The LED gradually increases in brightness and then decreases over approximately one second, without perceptible flickering (20 kHz). cat $CH/duty_cycle follows the written values. After the script, pwmchip0 no longer contains a pwm0 folder (channel released). If $CHIP does not exist, list ls /sys/class/pwm/ and verify that the PWM overlay is enabled in /boot/armbianEnv.txt (overlay_add=).
Common Errors
- Writing
duty_cyclebeforeperiod: the value is overwritten or rejected; always set the period before the duty cycle. - Confusing nanoseconds and microseconds: the period is in ns (20000 = 20 µs = 50 kHz).
- Forgetting the resistor: an LED-GND short circuit on a 3.3 V pin can damage the GPIO bank.
exportreturns "Device or resource busy": the channel is reserved by the kernel or an overlay; choose another channel.- >
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash