clignotante.py

Raspberry Pi Zero 2 W (2021) — Board Sheet · File : clignotante.py · Language : Python · Use case : Faire clignoter une LED depuis un script Python

The getting-underway script on an SBC: an LED on the GPIO header blinks at 1 Hz. gpiozero is the reference library on this OS: it handles the GPIO chip without root tricks.

Hardware

A Raspberry Pi Zero 2 W (six-core RP3A0 SoC, 512 MB) running 64-bit Raspberry Pi OS, with 2.4 GHz Wi-Fi and Bluetooth. 1 LED + 330 Ω resistor (the pins are 3.3 V on all these boards).

Wiring

LED anode on physical pin 11 (GPIO17) through 330 Ω, cathode on a GND pin (e.g. 14).

How it works

The script sets the pin as output, alternates high/low with sleep(1), and cleanly releases the pin (finally) even on Ctrl-C.

Running it

Copy the script (scp), chmod +x, run. Recent Raspberry Pi OS: GPIO access works without sudo (gpio group).

Variants

Replace sleep with a pattern (PWM ramp via gpiozero PWM / gpiod + threads), or wire several LEDs in a ring.

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

← Back to the board