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

ODROID-N2 / ODROID-N2+ (Hardkernel) · intermediaire · Schéma : IMG-odroid-n2-intermediaire-1-odroidn2-pwm-led.svg — LED avec résistance branchée sur la sortie PWM (broche PIN_13, GPIOX_4) de l'ODROID-N2, contrôlée via sysfs pwmchip pour un fondu lumineux.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials