🛠 Lire un bouton et afficher dans la console

Raspberry Pi 5 (model-5) · debutant · Schéma : IMG-model-5-debutant-2-model5-d2-bouton-console.svg — Bouton relié entre la broche physique 7 (GPIO4) et GND, pull-up interne activé, terminal console à l'écran

Objectives

Wire a push button to the Raspberry Pi 5 and display a message in the console on each press, using gpiomon (libgpiod) and then a small Shell script. This introduces GPIO inputs, internal pull-up resistors, and edge detection.

Hardware

Complete code

# Écouter les fronts sur GPIO4 (libgpiod v2)
sudo apt install -y gpiod
gpiomon --num-events=0 --rising-edge gpiochip0 4

# Script bouton.sh : journaliser chaque appui
cat > bouton.sh <<'EOF'
#!/usr/bin/env bash
# Bouton sur GPIO4 (broche physique 7), pull-up interne : appui = niveau 0
echo "Surveillance de GPIO4... Ctrl-C pour terminer"
gpiomon --num-events=0 --falling-edge --debounce-period 0.05 gpiochip0 4 | while read -r _; do
    echo "$(date '+%H:%M:%S') - bouton pressé !"
done
EOF
chmod +x bouton.sh
./bouton.sh

Expected verification

Each press of the button displays a timestamped line "button pressed!" in the terminal. With gpiomon --rising-edge, it is the release that triggers the event.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials