🛠 Variation d'intensité d'une LED avec PWM

Hardkernel ODROID-C2 (Amlogic S905) · intermediaire · Schéma : IMG-odroid-c2-intermediaire-1-odroidc2-pwm-led.svg — LED pilotée par un signal PWM via le connecteur J2 avec transistor de puissance

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials