🛠 Lire un bouton poussoir et afficher l'état dans la console
Objectives
Wire a push button to the 40-pin connector of the Orange Pi 3B, read its state from the command line using gpioget, and display each state change in the console.
Hardware
- Orange Pi 3B running Armbian, 5 V / 3 A power supply
- 1 push button, 1 10 kΩ resistor (pull-up to 3.3 V), Dupont cables
gpiodpackage installed (sudo apt install gpiod)
Complete code
# 1. Lecture ponctuelle de la broche (gpioget la configure en entrée)
sudo gpioget -c gpio1 35 # 0 ou 1 selon l'état du bouton
# 2. Journal des appuis dans la console
#!/bin/bash
# fichier : bouton.sh -> bash bouton.sh
CHIP=1
PIN=35 # gpio1_b3 en exemple : vérifier une broche libre sur le plan officiel
dernier=""
while true; do
etat=$(sudo gpioget -c gpio$CHIP $PIN)
if [ "$etat" != "$dernier" ]; then
echo "$(date +%T) - bouton = $etat"
dernier=$etat
fi
sleep 0.05
done
Expected verification
At rest, gpioget returns 1 (pull-up to 3.3 V); when the button is pressed, it returns 0. The script displays a timestamped line for each state change, both on press and release.
Common errors
- Forgetting the pull-up resistor and leaving the pin floating: the state will then display random values. The internal libgpiod pull (bias) also works, but the hardware pull-up is more educational.
- Choosing a pin that is already in use (UART, or the I2C pins 3/5 of the connector).
- Confusing logic 0 with the button being pressed: with a pull-up + button to ground, the logic is active-low (inverted).
- Running the script without
sudo: GPIO lines are reserved for the gpio group if udev is not configured. - Mechanical bounce: a single press may produce multiple lines; filter them with
sleep 0.05or a software debounce.
Other tutorials for this board
- Faire clignoter une LED via le GPIO avec libgpiod
- Premier script Python : bouton + LED avec python3-libgpiod
- Variation d'intensité d'une LED avec un signal PWM
- Interroger un capteur I2C sur les broches 3/5 du connecteur
- Configurer Ethernet gigabit et Wi-Fi puis se connecter en SSH
- Booter sur un SSD NVMe et récupérer une carte bloquée (MaskROM)
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash