What to use instead of AdaFruit GPIO in Debian 11?

I’m trying to find an easy way to manipulate a couple of GPIOs, preferably from Python but C would do if it’s more straightforward.

An internet search simply brings up thousands of hits which all use Adafruit_BBIO.GPIO which is no use at all because it doesn’t work in Debian 11. I can’t find anything remotely similar for Debian 11.

Is there nothing out there for simple GPIO access? … or do I really need to dig down into the nitty gritty of sysfs and such?

I’m beginning to think I’m fated, even things in the Debian 11 distribution are broken. I installed python3-rpi.gpio but when I try to import it I get:-

chris@bbb$ python3 
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/RPi/GPIO/__init__.py", line 23, in <module>
    from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!
>>>

Debian has a libgpiod python module. That should work

Yes (to gpiod) but I have been there and didn’t really get very far, maybe I should try again.

Searching in apt gives the followiing:-

chris@bbb$ apt search gpiod
Sorting... Done
Full Text Search... Done
gpiod/oldstable,now 1.6.2-1 armhf [installed]
  Tools for interacting with Linux GPIO character device - binary

libgpiod-dev/oldstable 1.6.2-1 armhf
  C library for interacting with Linux GPIO device - static libraries and headers

libgpiod-doc/oldstable 1.6.2-1 all
  C library for interacting with Linux GPIO device - library documentation

libgpiod2/oldstable,now 1.6.2-1 armhf [installed,automatic]
  C library for interacting with Linux GPIO device - shared libraries

libpigpiod-if-dev/oldstable 1.78-1 armhf
  Development headers for client libraries for Raspberry Pi GPIO control

libpigpiod-if1/oldstable 1.78-1 armhf
  Client library for Raspberry Pi GPIO control (deprecated)

libpigpiod-if2-1/oldstable 1.78-1 armhf
  Client library for Raspberry Pi GPIO control

python3-libgpiod/oldstable,now 1.6.2-1 armhf [installed]
  Python bindings for libgpiod (Python 3)

chris@bbb$

… and the default installation has installed them:-


 
chris@bbb$ dpkg -l | grep gpio
ii  gpiod                                      1.6.2-1                                 armhf        Tools for interacting with Linux GPIO character device - binary
ii  libgpiod2:armhf                            1.6.2-1                                 armhf        C library for interacting with Linux GPIO device - shared libraries
ii  python3-libgpiod:armhf                     1.6.2-1                                 armhf        Python bindings for libgpiod (Python 3)
chris@bbb$

It does at least seem to load OK into Python and ‘help (gpiod)’ produces voluminous output. I can’t find much other documentation though. However, I will persevere, thank you! :slight_smile:

Here is a “toggle pin” example for python-gpiod I had in my personal git…

#!/usr/bin/env python3
# //////////////////////////////////////
# 	toggle1.py
#  Toggles the P8_18 pin on BBAI64 as fast as it can. P8_18 is line 4 on chip 1.
# 	Wiring:	Attach an oscilloscope to P8_18 to see the squarewave or 
#          uncomment the uleep and attach an LED.
#SYSTEM SETUP:	sudo apt update; pip install gpiod
# 	            https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python/examples
#
#PROGRAM / VARIABLE SETUP:
# LED_CHIP, run: gpioinfo | grep -i -e chip -e HEADER_PIN (ie. P8_18)
# LED_LINE_OFFSET, run: gpioinfo | grep -i -e chip -e HEADER_PIN (ie. P8_18)
# //////////////////////////////////////

import gpiod
import time

LED_CHIP = 'gpiochip1'
LED_LINE_OFFSET = [4]  
LOOPS = 5000000
DELAY = 0.002

chip = gpiod.Chip(LED_CHIP)

lines = chip.get_lines(LED_LINE_OFFSET)
lines.request(consumer='toggle1.py', type=gpiod.LINE_REQ_DIR_OUT)

start_time=time.time()
cnt=1

while (cnt<LOOPS):
    lines.set_values([1])
    if (DELAY>0):
        time.sleep(DELAY)
    lines.set_values([0])
    if (DELAY>0):
        time.sleep(DELAY)
    cnt=cnt+1

end_time=time.time()
net_time=end_time-start_time
print("Completed %d loops in %.4f secs (%.4f loops per sec)" % (LOOPS, net_time,(LOOPS/net_time)))
1 Like

Thank you! Yes, I have now managed to get things working and I can make a GPIO line go up and down.

The gpiod library is a bit awkward though. It needs a decent higher level wrapper.