clignotante.py

NanoPi R5S (FriendlyElec) · 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. python3-gpiod drives the GPIO chip straight through the kernel, with no proprietary library.

Hardware

A NanoPi R5S (Rockchip RK3568B2, 4 Cortex-A55, 2-4 GB) running FriendlyCore/Ubuntu, a 1× GbE WAN + 2× 2.5 GbE LAN router with M.2 NVMe. 1 LED + 330 Ω resistor (the pins are 3.3 V on all these boards).

Wiring

LED anode on the chosen GPIO pin (see the board schematic and gpioinfo) through 330 Ω, cathode to GND.

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

On FriendlyCore: apt install gpiod python3-gpiod; the GPIO header follows the FriendlyElec R5S pinout — identify the chip with gpiodetect and the line number with gpioinfo.

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 python3-gpiod (API 1.x, Debian/Armbian).
# Installation : sudo apt install python3-gpiod gpiod
# Puce disponible : gpiodetect ; broches : gpioinfo <puce>
import gpiod
from time import sleep

PUCE = "gpiochip1"   # a verifier avec gpiodetect (varie selon la carte)
LIGNE = 17           # numero GPIO du schema de la carte

chip = gpiod.Chip(PUCE)
line = chip.get_line(LIGNE)
line.request(consumer="clignotante", type=gpiod.LINE_REQ_DIR_OUT,
             default_val=0)

print("Clignotement (Ctrl-C pour arreter)")
try:
    while True:
        line.set_value(1)
        print("LED ON")
        sleep(1)
        line.set_value(0)
        print("LED OFF")
        sleep(1)
finally:
    line.release()

← Back to the board