🛠 GPIO avec libgpiod : faire clignoter une LED et lire un bouton

Radxa ZERO 3W · debutant · Schéma : IMG-rz3w-debutant-3-rz3w-gpio-libgpiod-blink.svg — Radxa ZERO 3W avec son connecteur 40 broches compatible Raspberry Pi (non soudé sur les SKUs de base), breadboard : LED + résistance 330 Ω entre la broche 35 (GPIO3_A4) et GND, bouton poussoir entre la broche 11 (GPIO3_A1) et 3V3 ; broches 3/5/27/28 barrées en rouge (résistances de pull-up I2C).

Objectives

Learn the GPIOs of the ZERO 3W as of 2026: no sysfs (deprecated), but libgpiod, the recommended path in Radxa documentation. The 40-pin connector is electrically compatible with Raspberry Pi (3.3 V logic, never 5 V on the pins!), with its specific pitfalls: pins 3, 5, 27, and 28 are unusable as GPIOs due to I2C pull-up resistors, and pins 8/10 = U-Boot serial console. You will write a blink program in C and an interrupt-driven button in Python.

Hardware

Pinout rules (source: official hardware-interface)

Blink in C (pin 35, GPIO3_A4)

/* blink.c — compile : gcc blink.c -o blink -lgpiod */
#include <gpiod.h>
#include <stdio.h>
int main(void) {
    struct gpiod_chip *chip = gpiod_chip_open_by_name("gpiochip3");
    if (!chip) { perror("gpiochip3"); return 1; }
    struct gpiod_line *line = gpiod_chip_get_line(chip, 4);   /* A4 = ligne 4 */
    if (gpiod_line_request_output(line, "blink", 0) < 0) { perror("request"); return 1; }
    for (int i = 0; i < 10; i++) {          /* 5 cycles ON/OFF */
        gpiod_line_set_value(line, 1); sleep(1);
        gpiod_line_set_value(line, 0); sleep(1);
    }
    gpiod_line_release(line); gpiod_chip_close(chip);
    return 0;
}

Without compiling: gpioset -m signal $(gpiofind GPIO3_A4)=1 — gpiofind avoids hardcoding chip/line.

Event-driven button (pin 11, GPIO3_A1)

#!/usr/bin/env python3  # apt install python3-libgpiod gpiod
# Bouton : broche 11 -> 3V3 ; pull-down (flag gpiod ou résistance externe vers GND)
import gpiod
chip = gpiod.Chip("gpiochip3")
line = chip.get_line(1)                      # A1 = ligne 1
line.request(consumer="bouton", type=gpiod.LINE_REQ_EV_RISING_EDGE)
while True:
    if line.event_wait(sec=None): line.event_read; print("appui")

Expected verification

gcc blink.c -o blink -lgpiod && ./blink makes the LED blink 5 times. The Python script prints “press” on each button press, without polling (kernel blocking). gpioinfo gpiochip3 shows your requested lines.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials