🛠 Bouton poussoir et messages console

Hardkernel ODROID-C2 (Amlogic S905) · debutant · Schéma : IMG-odroid-c2-debutant-2-odroidc2-bouton-console.svg — bouton poussoir avec résistance de pull-up 10 kΩ vers 3,3 V sur le connecteur J2

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials