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

Orange Pi PC (Allwinner H3) · intermediaire · Schéma : IMG-orangepi-pc-intermediaire-1-opipc-pwm-led.svg — LED externe avec résistance connectée à une broche PWM du connecteur 40 broches de l'Orange Pi PC, signal PWM et rapport cyclique illustrés par une respiration lumineuse.

Objectives

Use the H3's PWM (pulse-width modulation) output to make an LED "breathe": vary the brightness by changing the duty cycle rather than by rapid blinking. The 40-pin connector on the Orange Pi PC exposes PWM functions (available functions: UART, I2C, SPI, PWM, CAN, I2S, SPDIF, LRADC, ADC). Under the mainline kernel, PWM is handled via the sysfs pwmchip class, without any vendor library.

Hardware

Complete Code

  1. Enable PWM and identify the channel:
# Activer l'overlay PWM (armbian-config > Hardware > Overlays > pwm)
sudo sed -i 's/^overlays=.*/& pwm/' /boot/armbianEnv.txt && sudo reboot
# Après reboot : lister les contrôleurs PWM exposés
ls /sys/class/pwm/            # pwmchip0, pwmchipN...
echo 0 | sudo tee /sys/class/pwm/pwmchip0/export   # exporter le canal 0
  1. Breathing script in Python:
#!/usr/bin/env python3
"""respiration.py — effet breathing LED via sysfs PWM (Orange Pi PC)."""
from pathlib import Path
import time

PWM = Path("/sys/class/pwm/pwmchip0/pwm0")   # canal exporté ci-dessus
PERIOD_NS = 20_000                            # 20 kHz : inaudible, sans scintillement

def main -> None:
    (PWM / "period").write_text(str(PERIOD_NS))
    (PWM / "polarity").write_text("normal")
    (PWM / "enable").write_text("1")
    try:
        while True:
            for pct in list(range(0, 101, 2)) + list(range(98, -1, -2)):
                duty = PERIOD_NS * pct // 100
                (PWM / "duty_cycle").write_text(str(duty))
                time.sleep(0.02)
    finally:
        (PWM / "enable").write_text("0")

if __name__ == "__main__":
    main

Expected Verification

The LED brightness rises and falls continuously (breathing cycle ~4 seconds), without visible flickering. cat /sys/class/pwm/pwmchip0/pwm0/duty_cycle follows the variations.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials