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

ODROID-C4 (Hardkernel) · intermediaire · Schéma : IMG-odroid-c4-intermediaire-1-odroidc4-pwm-led.svg — LED avec résistance de 330 Ω reliée à une broche PWM du connecteur 40 broches de l'ODROID-C4 et au GND, signal carré illustré.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials