🛠 Lire un bouton et afficher les appuis dans la console
Objectives
Complete the first project by adding an input: wire a push button to GPIO27, enable the internal pull-up resistor, and display each press in the console using gpiomon, without writing any code.
Hardware
- Raspberry Pi Model B+ running Raspberry Pi OS legacy 32-bit, connected via Ethernet
- 1 push button (4-pin "push" switches), Dupont wires
- Optional: 10 kΩ resistor if the internal pull-up is not available
Complete code
# 1) Activer le pull-up interne de GPIO27 au démarrage
# (ligne firmware récente ; sinon câbler 10 kΩ entre GPIO27 et 3,3 V)
echo "gpio=27=pup" | sudo tee -a /boot/config.txt
sudo reboot
# 2) Écouter les 5 prochains appuis : passage 1 -> 0 (bord descendant)
sudo gpiomon --num-events=5 --falling-edge gpiochip0 27
# 3) Variante « compteur » : état instantané de la broche
while true; do sudo gpioget gpiochip0 27; sleep 0.2; done
Wiring: one leg of the button to pin 13 (GPIO27), the other to pin 20 (GND). Without a press, the pull-up keeps the pin at 1; pressing it connects it to 0.
Expected verification
After rebooting, gpioinfo gpiochip0 shows line 27 with the pull-up active. gpiomon displays one event line per press ("event: edge: falling edge") and exits after 5 presses. gpioget returns 1 at rest, 0 when held.
Common errors
- Pull-up forgotten: the pin "floats", gpiomon triggers constantly or never.
- Button wired between GPIO and 3.3 V without a pull-down: inverted logic, you look for the rising edge instead.
- Mechanical bouncing: multiple events for a single press — normal, we will filter this in the script.
- Wrong GND pin: the circuit does not close, no press detected.
Other tutorials for this board
- Allumer une LED depuis la GPIO 17 avec libgpiod
- Premier script Python : faire clignoter la LED
- Variation d'intensité d'une LED avec le PWM matériel (GPIO18)
- Interroger un capteur I2C (BME280) sur le bus du 40 broches
- Mettre le B+ en réseau par Ethernet et piloter en SSH
- Amorçage microSD, pannes de boot et procédure de recouvrement
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython