🛠 Piloter les GPIO du NanoPi R5S avec libgpiod

NanoPi R5S (FriendlyElec) · intermediaire · Schéma : IMG-nanopi-r5s-intermediaire-1-nanopir5s-gpio-libgpiod.svg — connecteur FPC 12 pins du NanoPi R5S avec la broche GPIO3_C3 (3,3 V) reliée à une résistance et une LED, et le bouton Reset sur GPIO4_A0

Objectives

The 12-pin FPC connector on the NanoPi R5S exposes 8 GPIOs at 3.3 V levels (including GPIO3_C3, pin 3 in the FriendlyElec wiki pinout table). This tutorial uses libgpiod (the modern Linux GPIO API, as the legacy /sys/class/gpio is deprecated) to generate a square wave output, and explains why the Reset button cannot be read via raw GPIO: the gpio-keys driver claims it, so you must use input events instead.

Hardware

Complete code

#!/usr/bin/env python3
"""blink_fpc.py — sortie GPIO3_C3 (broche 3 du FPC) via libgpiod.
   RK3568 : GPIO3 -> gpiochip3, ligne = groupe*8 + n ; C3 -> 2*8+3 = 19.
   TOUJOURS vérifier d'abord : gpiodetect && gpioinfo gpiochip3

import gpiod, time

chip3 = gpiod.Chip("gpiochip3")
req = chip3.request_lines(
    config={19: gpiod.LineSettings(
        direction=gpiod.line.Direction.OUTPUT,
        output_value=gpiod.line.Value.INACTIVE)},
    consumer="demo-r5s")
try:
    for _ in range(5):
        req.set_value(19, gpiod.line.Value.ACTIVE);   time.sleep(0.2)
        req.set_value(19, gpiod.line.Value.INACTIVE); time.sleep(0.2)
finally:
    req.release_lines

# Bouton Reset : piloté par gpio-keys -> lire en input events :
#   sudo evtest   (KEY_RESTART)   ou   pip install evdev

Expected verification

gpiodetect lists at least gpiochip0 to gpiochip4; gpioinfo gpiochip3 shows line 19 as free. Running the code blinks the LED 5 times (200 ms ON / 200 ms OFF), measurable with a multimeter between 0 and 3.3 V. evtest captures KEY_RESTART when pressing Reset. If request_lines fails with “Device or resource busy”, the line is already claimed by a driver or another consumer.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials