🛠 Variation d'intensité d'une LED avec le PWM
Objectives
Vary the intensity of an LED (breathing effect) using the PWM output of the ODROID-N2. The 40-pin connector exposes 6 PWM channels (CNX specifications); here we target the PWM line associated with GPIOX_4 (physical pin 13). Under the mainline kernel, the Amlogic PWM controller (AO/EE) appears in /sys/class/pwm.
Hardware
- ODROID-N2/N2+ running Armbian (mainline)
- 1 LED + 330 Ω resistor between PIN_13 and GND (PIN_25)
pwmconfigpackage or sysfs access; a transistor (2N2222 + 1 kΩ resistor) if the LED draws more current- >
Complete code
#!/usr/bin/env bash
# respiration.sh — fondu PWM sur pwmchip0 canal 0 (vérifier le canal !)
set -e
CHIP=/sys/class/pwm/pwmchip0
[ -e $CHIP/pwm0 ] || echo 0 | sudo tee $CHIP/export
sleep 0.2
echo 500000 | sudo tee $CHIP/pwm0/period # 500 µs = 2 kHz
echo 1 | sudo tee $CHIP/pwm0/enable
while true; do
for d in $(seq 0 25000 50000); do # montée
echo $d | sudo tee $CHIP/pwm0/duty_cycle; sleep 0.03; done
for d in $(seq 50000 -25000 0); do # descente
echo $d | sudo tee $CHIP/pwm0/duty_cycle; sleep 0.03; done
done
Expected verification
After export, /sys/class/pwm/pwmchip0/pwm0/ contains period, duty_cycle, and enable. The script varies the LED brightness from off to full intensity in 5% steps (duty cycle 0 → 50000/500000 ns). cat duty_cycle reflects the written value. For frequency, at 20 kHz: period=50000, duty=25000 for 50%.
Common errors
exportrefuses the channel: it is reserved by the device-tree (display driver or fan); tryecho 1or another pwmchip.- period/duty in the wrong order: write period before duty, otherwise duty > period is rejected (EINVAL).
- Frequency too low: below ~100 Hz the LED visibly flickers; stay within 1–20 kHz.
- Confusing duty_ratio and duty_cycle: duty_cycle is expressed in nanoseconds, not in percentage.
- The PWM channel for pin 13 is not enabled in the DTB: on some images an overlay is required (
dtoverlay/overlays=in config) — > - Forgetting
enable=1: nothing blinks even if duty_cycle is non-zero.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash