python-gpio.py

Arduino Portenta X8 · File : python-gpio.py · Language : Python · Use case : Script Python complet : bouton, LED et journalisation

The typical application script on the X8 Linux: input, output and timestamped CSV logging with python3-gpiod. The board becomes a standalone counter on the Linux side while the Arduino core keeps real-time.

Hardware

An Arduino Portenta X8 (ST STM32MP1 module: quad Cortex-A7 Linux + dual Cortex-M4 real-time core, 8 GB eMMC, 2 GB LPDDR4, on-board Wi-Fi/BT, optional LTE). 1 LED + 330 Ω, 1 button (to the pin, pull per the schematic).

Wiring

LED and button on connector pins (carrier board), common grounds. Check each pin's max logic level.

How it works

The loop polls the button line at 50 Hz, lights the LED and appends an ISO line to the CSV on rising edge; finally releases the kernel lines.

Running it

Linux side: SSH access (routed USB-C or network) on the Portenta Linux image, bash/Python scripts. Real-time side: Arduino IDE with the « Arduino Mbed OS Portenta X8 Boards » core, application M7 core, flashing via mboot from Linux.

Variants

Replace the loop with an edge-event wait (gpiod edge event), or push the CSV to the cloud via a systemd service.

python-gpio.py

#!/usr/bin/env python3
# Bouton + LED + trace CSV en Python avec python3-gpiod (API 1.x).
# sudo apt install python3-gpiod
import gpiod
from time import sleep
from datetime import datetime

PUCE = "gpiochip0"     # gpiodetect
LED = 27               # broche carrier (exemple, voir schema)
BTN = 23

chip = gpiod.Chip(PUCE)
led = chip.get_line(LED)
led.request(consumer="app", type=gpiod.LINE_REQ_DIR_OUT, default_val=0)
btn = chip.get_line(BTN)
btn.request(consumer="app", type=gpiod.LINE_REQ_DIR_IN)

print("Appuis sur le bouton -> LED + /tmp/appuis.csv")
try:
    dernier = btn.get_value()
    while True:
        etat = btn.get_value()
        if etat != dernier and etat == 1:
            led.value(1)
            with open("/tmp/appuis.csv", "a") as f:
                f.write(f"{datetime.now().isoformat()},appui\n")
            print("appui enregistre")
        else:
            led.value(0)
        dernier = etat
        sleep(0.02)
finally:
    led.release()
    btn.release()

← Back to the board