What Python library to use GPIO with Debian 11

Previously (Debian 9 and 10) I used the Adafruit_BBIO for accessing GPIO pins from Python. This is now no longer supported and anyway fails to build on a Debian 11 BBB.

So, what do I need to be able to access GPIO pins easily from Python 3? I’ll probably want ADC and UART as well (which are also in Adafruit_BBIO).

For gpio there is libgpiod (might be a - after lib) using apt or gpiod using pip. They are different but do similar stuff. For UART just use the standard python serial library. No idea about ADC.

Thanks, python3-libgpiod seems to provide what I need.

ADC pins can be found here:

IIOPATH='/sys/bus/iio/devices/iio:device0'
IIODEV='/dev/iio:device0'

Also…I found this info. from the docs. Although they may change, /sys/bus/iio/ are your ADC pins in the sysfs interface.

I think some people may try to make a /dev/bone/adc port to that location in the filesystem for the Cape Interface Spec. soon. But…that is only a guess.

Seth

But I can’t get it to actually work. Can someone point me to some code using python3-libgpiod that actually sets a GPIO pin to output and sets it high/low?

It looks like there’s an apt package python3-libgpiod that installs the python package libgpiod. I also was not able to get that to work. There is another pure python package gpiod, which did work for me using the example code provided in their documentation: Python gpiod | loliot

import gpiod
import sys
import time

if len(sys.argv) > 2:
    LED_CHIP = sys.argv[1]
    LED_LINE_OFFSET = int(sys.argv[2])
else:
    print('''Usage:
    python3 blink.py <chip> <line offset>''')
    sys.exit()

chip = gpiod.chip(LED_CHIP)
led = chip.get_line(LED_LINE_OFFSET)

config = gpiod.line_request()
config.consumer = "Blink"
config.request_type = gpiod.line_request.DIRECTION_OUTPUT

led.request(config)

print(led.consumer)

while True:
    led.set_value(0)
    time.sleep(0.1)
    led.set_value(1)
    time.sleep(0.1)

To install it I needed to use pip:
pip3 install gpiod

I was able to blink P9.15 using:
python3 blink.py 1 16

1 Like

Ok, I tried the python3-libgpiod library again and did get it to work as well:

import gpiod
import time

chip=gpiod.Chip('gpiochip1')
line = gpiod.find_line("P9_15A")
lines = chip.get_lines([line.offset()])
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)

Modified from an example here: Manage the GPIO lines in Python3 and C with libgpiod

https://github.com/TexasInstruments/ti-gpio-py

See also. :smiley: