🛠 Lire un bouton poussoir avec libgpiod (pull-up interne)
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
- Le Potato AML-S905X-CC running Armbian mainline
- Push button between physical pin 11 (GPIOX_3 — chip/line to be confirmed with
lgpio info 11, example below: gpiochip3 line 8) and a ground pin sudo apt install libgpiod-dev gpiod
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
- Wrong chip/line: The GPIOAO/GPIOH/GPIOE/GPIODV/GPIOX banks are distributed across several gpiochips; always verify with
lgpio info <pin>orgpioinfo. - Missing pull-up: Without
BIAS_PULL_UP, the input floats and displays random states. - Expected inverted logic: With a pull-up, button pressed = 0 (active low).
- 5 V on the pin: Forbidden, the Le Potato has no 5 V tolerant pins.
Other tutorials for this board
- Faire clignoter une LED en C avec libgpiod sur Le Potato
- GPIO en Python avec python3-libgpiod sur Le Potato
- Fondu LED progressif avec PWM sous sysfs (meson)
- Capteur BME280 (I2C) sur Le Potato : overlay et lecture
- Console série (ttyAML0) et SSH sur Le Potato
- Boot SD/eMMC et récupération (pyamlboot) sur Amlogic S905X
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash