🛠 Bouton poussoir et lecture d'état
Objectives
Read the state of a push button on mainline Linux using libgpiod (Python) and turn on an LED in response. You will also discover the ttySAC2 serial console, which is invaluable when the board fails to boot — at 1.8 V only.
Hardware
- ODROID-XU4 running Armbian (kernel 6.6.x)
- 1 push button + 1 LED + 330 Ω resistor
python3-gpiodpackage (sudo apt install python3-gpiod lgpio)- Optional: 1.8 V USB-TTL serial adapter for the console
Complete Code
#!/usr/bin/env python3
"""button.py — allume une LED quand le bouton (vers GND) est presse."""
from gpiod import chip, LineFlags
import time
c = chip("gpiochip0") # gpiodetect liste les puces exynos pinctrl
btn = c.get_line("gpio-XX") # remplacer XX : une entree du header
btn.request(consumer="demo", type=LineFlags.REQUEST_FLAGS_INPUT)
led = c.get_line("gpio-YY") # remplacer YY : une broche sortie
led.request(consumer="demo", type=LineFlags.REQUEST_FLAGS_OUTPUT)
print("appui bouton (vers GND), Ctrl-C pour quitter")
while True:
led.set_value(1 if btn.get_value else 0)
time.sleep(0.05)
Expected Verification
Run python3 button.py then press the button: the LED lights up while the button is pressed. gpioinfo gpiochip0 shows both lines requested by the consumer "demo". If there are issues, the ttySAC2 serial console (115200 baud) displays kernel messages.
Common Errors
- Plugging a 5 V UART adapter into the serial header: the console is 1.8 V without protection, and some boards have been destroyed this way — use only 1.8 V hardware.
- Wrong pin number: the
gpio-XXnames depend on the Exynos controller, never those of a Raspberry Pi; verify withgpioinfo. - Forgetting pull resistors: without a pull-up, the input floats and the state changes randomly.
- Confusing
gpiochip0with other chips listed bygpiodetect.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash