🛠 Lire un bouton poussoir en Python avec python3-libgpiod

Radxa ROCK 3B (Rockchip RK3568) · debutant · Schéma : IMG-radxa-rock-3b-debutant-2-rock3b-bouton-python.svg — bouton poussoir avec résistance de pull câblé entre une broche GPIO et la masse sur la Radxa ROCK 3B, fenêtre Python à côté de la carte

Objectives

Wire a push button to the 40-pin connector of the ROCK 3B and display each press in the console using python3-libgpiod, utilizing interrupt events (rising edge) rather than polling in a loop. This is the first Python script in the series: it serves as a foundation for sensor and IoT tutorials.

Hardware

Complete Code

#!/usr/bin/env python3
"""Bouton sur le connecteur 40 broches — affiche les appuis (ROCK 3B).
 Résolvez CHIP/LINE avec `gpiofind PIN_11` sur VOTRE image : les numéros
 diffèrent entre noyau constructeur (Radxa) et noyau mainline >= 6.11."""
from gpiod import Chip, LineRequest

CHIP, LINE = "gpiochip3", 1   # exemple GPIO3_A1 derrière PIN_11 — CONFIRMER

with Chip(CHIP) as chip:
    req = chip.request_lines(
        consumer="rock3b-demo",
        config={LINE: LineRequest.REQUEST_INPUT},
    )
    print("appui attendu (front montant), Ctrl-C pour quitter")
    while True:
        for ev in req.event_wait(timeout_ms=None):
            e = req.event_get
            if e and e.event_type == 1 and e.line == LINE:  # RISING_EDGE
                print("bouton pressé")

Expected Verification

sudo python3 bouton.py starts without errors; each press (rising edge) displays "button pressed". Ctrl-C releases the line cleanly. With a hardware pull-up + button to ground, the idle state is 1 and the pressed state is 0: if nothing is displayed, swap the button/cable or test sudo gpioget $(gpiofind PIN_11) with a logic multimeter.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials