🛠 Variateur de lumière : PWM matériel sur GPIO18

Raspberry Pi Model A (2013) — Board Sheet · intermediaire · Schéma : IMG-model-a-intermediaire-4-modela-pwm-led.svg — Raspberry Pi Model A avec une LED sur GPIO18 (broche 12, sortie PWM matériel) et sa résistance, dégradé lumineux de faible à forte intensité illustrant le rapport cyclique

Objectives

Hardware

Complete code

#!/usr/bin/env python3
"""Fondu PWM matériel sur GPIO18 (broche 12)."""
import RPi.GPIO as GPIO, time

PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN, GPIO.OUT)
p = GPIO.PWM(PIN, 500)          # 500 Hz
p.start(0)
try:
    while True:
        for d in list(range(0, 101, 5)) + list(range(95, -5, -5)):
            p.ChangeDutyCycle(d) # rapport cyclique 0..100 %
            time.sleep(0.03)
except KeyboardInterrupt:
    pass
finally:
    p.stop
    GPIO.cleanup
# Vérifier le brochage PWM matériel
pinout  # ou consulter https://pinout.xyz

Expected verification

The LED gradually brightens and dims without perceptible flickering. At 0% it is off, at 100% it is at full intensity; only pin 12 (GPIO18) provides hardware PWM on the 26-pin connector.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials