🛠 Variateur d'intensité : faire varier une LED en PWM matériel

Raspberry Pi 4 Model B (2019) — Board Sheet · intermediaire · Schéma : IMG-model-4b-intermediaire-1-model4b-pwm-led.svg — Signal PWM sur GPIO18 (broche 12) vers LED avec résistance, courbe de rapport cyclique 0 à 100 %

Objectives

Use the BCM2711 hardware PWM output (the SoC has a hardware PWM) to vary the brightness of an LED: duty cycle, frequency, breathing. You will enable the device tree overlay and then control it via the command line and Python.

Hardware

Complete code

# 1) Activer le PWM : sudo raspi-config -> Interface Options -> PWM
#    ou ajouter dtoverlay=pwm-2chan dans /boot/firmware/config.txt
sudo reboot
gpioset --pwm gpiochip0 18=100=0     # 0 % de rapport cyclique
for d in 100 75 50 25 0; do echo "Duty $d %"; gpioset --pwm gpiochip0 18=$d=0; sleep 1; done
# breathing.py - respiration de la LED via gpiozero
from gpiozero import PWMLED
from signal import pause
led = PWMLED(18)          # GPIO18, PWM materiel
led.pwm_frequency = 1000  # Hz
led.source = "pulse"      # oscillation continue du rapport cyclique
pause

Expected verification

The LED fades progressively at each step of the shell script, then breathes continuously with the Python script. At 0% it is off, at 100% it is fully on; the eye perceives the variation thanks to a frequency of a few hundred Hz.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials