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

Raspberry Pi 3 Model A+ (2018) — Board Sheet · intermediaire · Schéma : IMG-model-3a-plus-intermediaire-1-model3a-pwm-led.svg — Raspberry Pi 3 A+ : LED + 330 Ω sur GPIO18 (broche 12, PWM matériel), GND broche 14, oscillogramme du rapport cyclique

Objectives

Vary the intensity of an LED using Pulse Width Modulation (PWM) on the Raspberry Pi 3 A+: hardware PWM on GPIO18 (pin 12, "PWM" signal on the 40-pin connector) versus software PWM with gpiozero, duty cycle, and frequency.

Hardware

Complete Code

#!/usr/bin/env python3
# pwm_led.py — respiration de LED (GPIO18 = PWM matériel)
from gpiozero import PWMLED
from time import sleep

led = PWMLED("GPIO18", pwm=True)   # GPIO18 = broche 12, PWM matériel
led.pwm_frequency = 100            # Hz ; défaut gpiozero : 100 Hz

# respiration : montée puis descente du rapport cyclique
for i in range(0, 101, 5):
    led.value = i / 100.0          # 0.0 = éteinte, 1.0 = pleine
    sleep(0.05)
for i in range(100, -1, -5):
    led.value = i / 100.0
    sleep(0.05)

# clignotement précis piloté par le contrôleur PWM matériel
led.blink(on_time=0.5, off_time=0.5, fade_in_time=0.2, fade_out_time=0.2)
sleep(5)
led.off
python3 pwm_led.py
# Vérifier le brochage PWM : le site de référence du connecteur 40 broches

Expected Verification

The LED gradually increases in brightness over ~1 s, decreases, then blinks with fades. With a multimeter on GPIO18, the average voltage follows the duty cycle (≈1.65 V at 50% based on 3.3 V).

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials