clignotante.py

Raspberry Pi Zero (2015) — 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 (BCM2835, 1 GB) running Raspberry Pi OS, reached over Ethernet via an adapter or serial/SSH. 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 to the board (scp), make it executable (chmod +x) then run it. On recent Raspberry Pi OS, GPIO scripts run without sudo thanks to the 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