🛠 Bouton poussoir et lecture d'état

Hardkernel ODROID-XU4 (Samsung Exynos5422) · debutant · Schéma : IMG-odroid-xu4-debutant-2-odroidxu4-bouton-console.svg — Bouton poussoir relié à une broche GPIO et à la masse sur l'ODROID-XU4, sortie console série ttySAC2 indiquée

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials