clignotante.py

Raspberry Pi Zero W (2017) — 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 Raspberry Pi Zero W (BCM2835, 512 Mo) sous Raspberry Pi OS, avec Wi-Fi 2,4 GHz et Bluetooth. 1 LED + résistance 330 Ω (les broches sont en 3,3 V sur toutes ces cartes).

Câblage

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.

Exécution

Copier le script (scp), chmod +x, lancer. Raspberry Pi OS recent : les acces GPIO se font sans sudo (groupe gpio).

Variantes

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