🛠 Lire un bouton et afficher les appuis dans la console

Raspberry Pi Model B+ (2014) — Board Sheet · debutant · Schéma : IMG-model-b-plus-debutant-2-modelbp-bouton-console.svg — bouton-poussoir entre GPIO27 (broche 13) et GND (broche 20), pull-up interne activé par gpio=27=pup dans config.txt.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials