🛠 Lire un bouton et afficher l'état dans la console
Objectives
Learn how to read a digital input on the Orange Pi PC: configure a pin as an input with an internal pull-up, detect button presses, and display each event in the console. You will use gpiomon (libgpiod) to wait for edges, and you will discover that the board already has a button: SW4, the power button, wired to PL3 (gpiochip1, r_pio) and active low.
Hardware
- Orange Pi PC running Armbian, 5 V / 2 A power supply on the DC jack
- 1 push button
- Female-to-male Dupont cables
- A GPIO pin from the 40-pin connector of your choice (3.3 V logic) + a GND pin
- gpiod package installed: sudo apt install gpiod
Complete Code
Bash script that monitors a button on a named pin (adjust the pin name to match your wiring):
#!/usr/bin/env bash
# bouton.sh — détection d'appui avec gpiomon (libgpiod)
set -euo pipefail
# Exemple avec le bouton d'alimentation SW4 = PL3 (chip r_pio, actif-low)
LINE="$(gpiofind PL3)"
echo "Surveillance de ${LINE} (Ctrl-C pour quitter)..."
# rising/falling : chaque front est affiché horodaté
gpiomon --num-events=0 --rising-edge "${LINE}" | while read -r _ _ event _; do
echo "$(date '+%H:%M:%S') — SW4 relâché (${event})"
done
Equivalent Python version using python3-libgpiod:
#!/usr/bin/env python3
import gpiod, time
chip = gpiod.Chip("gpiochip0")
ligne = chip.get_line(15) # PA15 réutilisé en entrée pour l'exo
ligne.request(consumer="bouton", type=gpiod.LINE_REQ_EV_RISING_EDGE,
flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)
print("En attente de fronts montants...")
while True:
evts = ligne.event_wait(sec=1)
if evts:
ligne.event_read
print(time.strftime("%H:%M:%S"), "— appui détecté")
Expected Verification
Each press/release displays a timestamped line in the terminal. With the SW4 button, a long press also triggers shutdown (KEY_POWER): for this experiment, press briefly.
Common Errors
- Forgetting the pull-up (internal or external): the input floats and gpiomon spits out spurious events.
- Choosing the wrong chip: PL3 is on gpiochip1 (r_pio), while PA to PH are on gpiochip0.
- Waiting for a rising edge on an active-low button wired to GND: the useful edge is falling.
- Connecting the button to a power pin on the connector instead of a GPIO pin.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash