🛠 Lire un bouton et afficher dans la console
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
- ODROID-M1 running Armbian (python3-gpiod package)
- 1 push button (no external resistor required: internal pull-up)
- >
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
- Forgetting the pull-up: the input floats and displays phantom presses.
- Wiring the button to 3.3 V instead of ground with pull-up: inverted logic.
- Debounce time too short: a single physical press counts multiple times.
- Line already claimed by another program: libgpiod refuses the request, you must free the other process.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash