clignotante.py

Raspberry Pi Compute Module 5 (compute-module-5) · 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 required

A Compute Module 5 (BCM2712 + RP1) on a CM5 IO Board, powered by 5 V USB-C (up to 25 W over PD), running Raspberry Pi OS Bookworm. 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. On Bookworm: sudo apt install python3-gpiozero then python3 your_script.py. The connector 28 GPIOs reuse the Pi 5 BCM numbering; the module adds a PWR_BUT button and an RTC (VBAT).

Going further

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