🛠 Lire un bouton et afficher l'état dans la console

ODROID-N2 / ODROID-N2+ (Hardkernel) · debutant · Schéma : IMG-odroid-n2-debutant-2-odroidn2-bouton-console.svg — Bouton poussoir entre la broche PIN_11 (GPIOX_3) et une broche GND de l'ODROID-N2, résistance de pull-up interne activée par le logiciel.

Objectives

Read the state of a push button on the ODROID-N2 GPIO and display it in the console. We use the internal pull-up resistor (libgpiod bias pull-up), so no external resistor is needed: the button connects the pin to ground when pressed.

Hardware

Complete code

#!/usr/bin/env bash
# bouton.sh — état du bouton sur PIN_11 en temps réel (libgpiod v2)
set -e
gpiomon --num-events=0 --falling-edge --rising-edge gpiochip0 PIN_11 &
MON=$!
trap "kill $MON" EXIT
while true; do
    etat=$(gpioset --quiet --state gpiochip0 PIN_11 2>/dev/null || true)
    lu=$(gpioget gpiochip0 PIN_11)
    [ "$lu" = 0 ] && echo "BOUTON APPUYÉ" || echo "relâché"
    sleep 0.1
done

C version (libgpiod) with event handling: gcc bouton.c -o bouton -lgpiod, requesting PIN_11 as input with GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP, then reading at 10 Hz.

Expected verification

At startup, the console displays "released" in a loop (the internal pull-up keeps the line at 1). While holding the button pressed, the lines become "BUTTON PRESSED". With gpiomon gpiochip0 PIN_11, each press/release produces a timestamped event.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials