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:
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.
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
if len(sys.argv) > 2:
LED_CHIP = sys.argv
LED_LINE_OFFSET = int(sys.argv)
python3 blink.py <chip> <line offset>''')
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
To install it I needed to use pip:
pip3 install gpiod
I was able to blink P9.15 using:
python3 blink.py 1 16
Ok, I tried the python3-libgpiod library again and did get it to work as well:
line = gpiod.find_line("P9_15A")
lines = chip.get_lines([line.offset()])
lines.request(consumer='foobar', type=gpiod.LINE_REQ_DIR_OUT, default_vals=)
Modified from an example here: Manage the GPIO lines in Python3 and C with libgpiod