🛠 Lire un bouton poussoir dans la console
Objectives
Wire a push button to the 40-pin connector of the ODROID-C4 and display each press in the console using gpiomon (libgpiod). You will understand the mapping from physical pin to GPIO line (PIN_13 = GPIOX_4, line 4 of the gpio chip), the benefit of the internal pull-up resistor, and the golden rule: all connector logic operates at 3.3 V.
Hardware
- ODROID-C4 running Armbian current (mainline)
- 1 push button (momentary NO contact)
- Male-to-Female jumper wires
- No external resistor needed: the pull-up is handled in software
sudo apt install -y gpiodif libgpiod is not already installed
Complete Code
#!/usr/bin/env bash
# Câblage : bouton entre PIN_13 (GPIOX_4, chip "gpio", ligne 4)
# et PIN_14 (GND). Jamais de signal 5 V : logique 3,3 V max.
# pull-up interne activée -> état au repos = 1, appui = 0.
# Événements montants (relâchement) et descendants (appui) :
sudo gpiomon --num-events=20 --rising-edge --falling-edge --bias=pull-up gpio 4
# Variante script : réagir à un seul appui puis afficher un message
while sudo gpiomon --num-events=1 --falling-edge --bias=pull-up gpio 4 ; do
echo "Appui détecté à $(date +%T)"
done
Expected Verification
Each press prints an event line with a timestamp (event: event=falling edge), and each release prints a rising edge line. As long as you do not press the button, the command remains silently blocked: this is normal, as gpiomon waits on a poll. With gpioget gpio 4 without pressing the button, you should read 1 (internal pull-up).
Common Errors
- Choosing the wrong chip: GPIOX_4 is on the
gpiochip (line 4), not ongpio_ao, which is reserved for always-on pins. - Forgetting
--bias=pull-up: without it, the input floats and triggers phantom presses. - Connecting the pin to 5 V: all connector I/Os are 3.3 V; 5 V can damage the Amlogic S905X3 SoC.
- Interpreting mechanical bounce as multiple presses: add a delay (
sleep 0.05) or filter it in your script. - >
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash