Manage GPIO lines in Python3 with libgpiod

Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use the character device instead. libgpiod encapsulates the ioctl calls and data structures behind a straightforward API.

If you are using Debian Buster 10 install gpiod by typing these commands:

sudo apt update
sudo apt install python3-libgpiod

Blinking led

import gpiod
import time

chip=gpiod.Chip('gpiochip0')

lines = chip.get_lines([ 24 ])
lines.request(consumer='foobar', type=gpiod.LINE_REQ_DIR_OUT, default_vals=[ 0 ])

while True:
        lines.set_values([ 1 ])
        time.sleep(1)
        lines.set_values([ 0 ])
        time.sleep(1)

Links

Sergio Tanzilli
System designer, webmaster and Acme Systems co-founder

Personal email: tanzilli@acmesystems.it
Webpage: https://www.acmesystems.it
Github repositories: https://github.com/tanzilli and https://github.com/acmesystems
Telegram group dedicated to the Acme Systems boards: https://t.me/acmesystemssrl