🛠 Lire un bouton et afficher dans la console

ODROID-M1 (Hardkernel) · debutant · Schéma : IMG-odroid-m1-debutant-2-odroidm1-bouton-console.svg — bouton-poussoir entre une broche GPIO et la masse, résistance de pull-up interne activée, terminal console à l'écran

Objectives

Configure a pin on the 40-pin connector as an input with internal pull-up, read a push button in Python using libgpiod, and display each press in the console. This is the basis for any physical interaction with the ODROID-M1.

Hardware

Complete Code

#!/usr/bin/env python3
"""Compteur d'appuis sur bouton, libgpiod (chip/ligne à adapter)."""
import time
from gpiod import chip, line

CHIP, LIGNE = "gpiochip1", 22  # exemple banque gpio1 du RK3568B2
compteur = 0
with chip(CHIP) as g:
    g.request_lines(consumer="bouton", config={
        LIGNE: line.RequestConfig(
            line=LIGNE, consumer="bouton",
            request_type=line.RequestType.INPUT,
            bias=line.Bias.PULL_UP),
    })
    print("En attente d'appuis (Ctrl-C pour quitter)...", flush=True)
    while True:
        if g.get_value(LIGNE) == 0:      # bouton fermé vers la masse
            compteur += 1
            print(f"Appui n°{compteur}", flush=True)
            time.sleep(0.3)              # anti-rebond logiciel
        time.sleep(0.05)

Expected Verification

Each press displays "Press n°X" in the console; releasing the button generates nothing thanks to the internal pull-up.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials