clignotante.py

Raspberry Pi Compute Module 4 (CM4, 2020) — Fiche carte · Fichier : clignotante.py · Langage : Python · Usage : Faire clignoter une LED depuis un script Python

Le script de mise en jambe sur SBC : une LED branchée sur le connecteur GPIO clignote à 1 Hz. gpiozero est la bibliothèque de référence sur cet OS : elle gère les puces GPIO sans manipulation root.

Matériel requis

Un Compute Module 4 (BCM2711) sur son Compute Module IO Board, alimenté en 5 V par USB-C, sous Raspberry Pi OS 64 bits (Bookworm). 1 LED + résistance 330 Ω (les broches sont en 3,3 V sur toutes ces cartes).

Branchement

Anode de la LED sur la broche physique 11 (GPIO17) via 330 Ω, cathode sur une broche GND (ex. 14).

Fonctionnement

Le script configure la broche en sortie, alterne niveau haut/bas avec sleep(1), et rend proprement la broche (finally) même sur Ctrl-C. Sous Bookworm : sudo apt install python3-gpiozero (backend libgpiod) puis python3 votre_script.py. Les GPIO suivent la numérotation BCM du connecteur 40 broches.

Pour aller plus loin

Remplacez sleep par un motif (rampe PWM via gpiozero PWM / gpiod + threads), ou branchez plusieurs LED en anneau.

clignotante.py

#!/usr/bin/env python3
# Clignotement LED avec gpiozero (bibliotheque recommandee sur cet OS).
# Installation : sudo apt install python3-gpiozero
from gpiozero import LED
from time import sleep

led = LED(17)   # GPIO17 = broche physique 11 du connecteur

print("Clignotement : Ctrl-C pour arreter")
try:
    while True:
        led.on()     # la broche passe a 3,3 V
        print("LED ON")
        sleep(1)
        led.off()    # la broche repart a 0 V
        print("LED OFF")
        sleep(1)
except KeyboardInterrupt:
    print("Arret propre")
finally:
    led.close()      # rend la broche a l'etat au repos

← Retour à la fiche