🛠 Piloter les GPIO du NanoPi R5S avec libgpiod
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
- NanoPi R5S running vendor kernel 6.1 or mainline, with
python3-gpiodandgpiod(sudo apt install gpiod python3-gpiod) - 0.5 mm 12-pin FPC connector (FPC to breadboard adapter required)
- LED + 1 kΩ resistor (3.3 V levels, never 5 V on these pins)
- No ADC on the connectors: the RK3568 SARADC is not exposed
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
- Wrong line: forgetting that C3 = group C (index 2) × 8 + 3 = line 19, not “3”.
- Assuming the gpiochip number: it varies depending on the kernel; run
gpiodetectbefore wiring. - Injecting 5 V into a 3.3 V FPC pin: risk of permanent damage; the 2×8 connector (SDIO/I2S) is at 1.8 V.
- Looking for Reset via raw GPIO: gpio-keys monopolizes it, use /dev/input instead.
- Confusing it with the Raspberry Pi pinout: the FPC connector has no 5 V or GND on an easy-to-access side, refer to the wiki table.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash