🛠 Lire un bouton poussoir et afficher dans la console
Objectives
Read the state of a push button on the 40-pin connector and display a message in the console on each press, using gpiomon (libgpiod) which waits for edges on a GPIO line. We reuse pin 7 (GPIO1_D6 = gpiochip1, line 30) already wired in the LED tutorial: remove the LED before starting.
Hardware
- Powered Orange Pi 5 Plus (Type-C side of the Ethernet ports, 5 V / 4 A)
- 1 push button + 1 10 kΩ resistor (pull-up to 3.3 V)
- Jumper cables, terminal access
gpiodpackage installed (sudo apt install -y gpiod)
Complete code
# Attente d'un seul appui (front descendant : repos au niveau haut grâce au pull-up)
sudo gpiomon --num-events=1 --falling-edge gpio1 30
# Boucle complète : un message par appui, anti-rebond 50 ms
while sudo gpiomon --num-events=1 --falling-edge \
--debounce-edge-period 50000000 gpio1 30 >/dev/null; do
echo "Bouton pressé à $(date +%T)"
done
Expected verification
gpiomon blocks and then displays a line like event: 1 falling edge on the first press. With the loop, each press prints "Button pressed" with the time; without debouncing, a single press can trigger 2 to 5 events (mechanical contact bounce).
Common errors
- No pull-up resistor: the line floats, gpiomon triggers continuously at the slightest noise. An internal pull-up resistor can be requested via libgpiod bias options, but an external 10 kΩ pull-up is more robust for beginners.
- Inverted edge: with a pull-up + button to ground, pressing produces a falling edge (
--falling-edge), not a rising one. - Wrong line: pin 7 = line 30 of gpiochip1 (GPIO1_D6), not line 7 nor gpiochip0.
- >
Other tutorials for this board
- Allumer une LED via le GPIO 40 broches (libgpiod)
- Premier script Python : fiche signalétique de la carte et LED
- Variation d'intensité d'une LED avec PWM (sysfs pwm)
- Lire un capteur I2C (BMP280) depuis le bus du connecteur
- Exploiter le double Ethernet 2,5 GbE et sécuriser le SSH
- Booter sur NVMe (M.2 PCIe 3.0 x4) et récupérer une carte brique via MaskROM
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash