🛠 Variation d'intensité d'une LED avec PWM
Objectives
Vary the intensity of an LED (breathing light effect) using the PWM output of the Amlogic S905 exposed on connector J2, utilizing the mainline kernel's sysfs PWM interface (the meson-gxbb-odroidc2 DTS labels the signals PWM D and PWM B on the header).
Hardware
- ODROID-C2 running Armbian/mainline (interface /sys/class/pwm)
- LED + resistor, optionally an NPN transistor (2N2222) if driving multiple LEDs
- J2 pin carrying the PWM D signal (the mainline DTS explicitly names it on header J2)
- >
Complete Code
#!/usr/bin/env bash
# pwm_respiration.sh — respiration LED via sysfs PWM
set -e
CHIP=$(ls /sys/class/pwm/ | grep -m1 pwmchip || true)
[ -z "$CHIP" ] && { echo "aucun pwmchip — activer le overlay PWM (armbian-config)"; exit 1; }
echo 0 > /sys/class/pwm/$CHIP/export
P=/sys/class/pwm/$CHIP/pwm0
echo 20000 > $P/period # 20 us de periode
while :; do
for pct in $(seq 0 5 100) $(seq 95 -5 0); do
echo $(( (20000 * pct) / 100 )) > $P/duty_cycle
echo 1 > $P/enable
sleep 0.03
done
done
Expected Verification
The LED gradually fades from off to full intensity and then back, without perceptible flickering. cat $P/duty_cycle follows the ramp. If /sys/class/pwm is empty, the PWM controller is not enabled: add the appropriate overlay/device-tree (armbian-config > overlays) and then reboot.
Common Errors
- Writing duty_cycle > period: undefined behavior, keep duty <= period.
- Forgetting enable=1: the output remains in the inactive state.
- Wrong channel: PWM D is not necessarily pwm0; test each exported channel.
- Driving the LED directly from the pin: stay below the GPIO current limit (the wiki limit is not reconfirmed), otherwise use a transistor.
- >
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash