🛠 Lire un bouton poussoir avec libgpiod (pull-up interne)

Libre Computer Amlogic boards — libre-amlogic (flagship: Le Potato AML-S905X-CC) · debutant · Schéma : IMG-libre-amlogic-debutant-2-libre-amlogic-debutant-bouton-libgpiod.svg — Bouton entre la broche 11 (GPIOX_3) et la masse sur Le Potato, pull-up interne activé par libgpiod.

Objectives

Read the state of a push button on the Le Potato using libgpiod in C, enabling the internal pull-up resistor (no external resistor required) and displaying the state in a loop.

Hardware

Complete code

/* bouton.c — gcc bouton.c -o bouton -lgpiod */
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
    /* Chip/ligne à ajuster selon « lgpio info 11 » ou « gpioinfo » */
    struct gpiod_chip *chip = gpiod_chip_open_by_name("gpiochip3");
    if (!chip) { perror("gpiochip3"); return 1; }
    struct gpiod_line *line = gpiod_chip_get_line(chip, 8);
    int flags = GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP;
    if (!line || gpiod_line_request_input_flags(line, "bouton", flags)) {
        perror("request"); return 1;
    }
    printf("Appuyez sur le bouton (Ctrl-C pour quitter)\n");
    for (;;) {
        int etat = gpiod_line_get_value(line);
        printf("bouton %s\n", etat ? "relâché" : "APPUYÉ");
        usleep(50000);
    }
}

Expected verification

./bouton displays "released" at rest (the pull-up maintains the high state) and "PRESSED" when the contact to ground is closed. gpioinfo gpiochip3 confirms the pull-up bias on the requested line.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials