🛠 Lire un bouton et afficher l'état dans la console
Objectives
Read the state of a push button on the ODROID-N2 GPIO and display it in the console. We use the internal pull-up resistor (libgpiod bias pull-up), so no external resistor is needed: the button connects the pin to ground when pressed.
Hardware
- ODROID-N2/N2+ running Armbian (mainline)
- 1 push button
- 2 Dupont cables
- Pin used: PIN_11 (GPIOX_3), GND on PIN_25
Complete code
#!/usr/bin/env bash
# bouton.sh — état du bouton sur PIN_11 en temps réel (libgpiod v2)
set -e
gpiomon --num-events=0 --falling-edge --rising-edge gpiochip0 PIN_11 &
MON=$!
trap "kill $MON" EXIT
while true; do
etat=$(gpioset --quiet --state gpiochip0 PIN_11 2>/dev/null || true)
lu=$(gpioget gpiochip0 PIN_11)
[ "$lu" = 0 ] && echo "BOUTON APPUYÉ" || echo "relâché"
sleep 0.1
done
C version (libgpiod) with event handling: gcc bouton.c -o bouton -lgpiod, requesting PIN_11 as input with GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP, then reading at 10 Hz.
Expected verification
At startup, the console displays "released" in a loop (the internal pull-up keeps the line at 1). While holding the button pressed, the lines become "BUTTON PRESSED". With gpiomon gpiochip0 PIN_11, each press/release produces a timestamped event.
Common errors
- Button between PIN_11 and 3.3 V instead of GND: without software pull-up, the line floats and the state is random.
- Missing debounce: a few bounces are displayed in addition to the press; filter by logic (ignore changes < 50 ms).
- Confusing PIN_11 (physical pin 11) and GPIO 11: the
PIN_xxnames in the device-tree are physical, verify withgpioinfo gpiochip0. gpiogetreturns an error: the line might be occupied by another process (gpioinfoshows the consumer).- >
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash