🛠 Bouton poussoir et messages console
Objectives
Read a push button on the ODROID-C2 and display a message on the console at each press, by combining GPIO input (wiring-odroid) and stdout output — the basis of any human-machine interface.
Hardware
- ODROID-C2 running Arch Linux ARM (wiring-odroid) or vendor image
- Push button, 10 kΩ pull-up resistor to 3.3 V (the pins on connector J2 do not offer a guaranteed internal pull-up via this API)
- Physical pin 11 as input, GND for the button
- uart_AO serial console (115200 8N1) or SSH session to view the messages
Complete code
/* bouton.c — gcc bouton.c -lwiringPi -o bouton */
#include <wiringPi.h>
#include <stdio.h>
#include <time.h>
#define BTN 11 /* broche physique 11, bouton vers GND */
int main(void) {
if (wiringPiSetupPhys == -1) { perror("wiringPiSetupPhys"); return 1; }
pinMode(BTN, INPUT); /* pull-up 10k externe vers 3,3 V requis */
int last = HIGH, appuis = 0;
for (;;) {
int etat = digitalRead(BTN);
if (etat == LOW && last == HIGH) {
appuis++;
printf("Appui n%d — bouton actif (niveau bas)\n", appuis);
fflush(stdout);
delay(50); /* anti-rebond logiciel */
}
last = etat;
delay(5);
}
}
Expected verification
Each press displays a line "Press n1", "Press n2"… on the console. Releasing and pressing again counts as a new press; holding the button down must not repeat the message (debouncing).
Common errors
- Forgetting the external pull-up resistor: the input floats and messages appear in bursts.
- Confusing active-low (LOW when pressed) and active-high.
- Running without sudo: GPIO access via wiringPi requires it depending on the image.
- >
- Debounce time too short: single presses count as double; increase delay(50) if needed.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash