Method for using GPIO Pins with Bullseye, Python and gpiod

I’ve been trying to work out gpio controls this afternoon using the 3c and didn’t find a lot of clarity on the forum so thought I would describe a method that has worked for me with the latest bullseye (36) image. This is just to set pins high or low, nothing fancy, but hopefully can save someone else some time as I failed to find a very complete method described in full. I’m ssh’d onto my board as it is xfce but currently not showing anything through HDMI - (shows a connection but nothing displayed) an issue for later as I wanted CLI mostly anyway.

I’ve installed libgpiod with the following command:
sudo apt update
sudo apt install python3-libgpiod

Used gpiodetect to see the chips available:
gpiodetect

OUTPUT:
gpiochip0 [gpio0] (32 lines)
gpiochip1 [gpio1] (32 lines)
gpiochip2 [gpio2] (32 lines)
gpiochip3 [gpio3] (32 lines)
gpiochip4 [gpio4] (32 lines)
gpiochip5 [rk817-gpio] (1 lines)

Run gpioinfo on each chip (0-5 for me) to identify the chip each pin resides on and its line
gpioinfo gpiochip3

OUTPUT:
rock@rock-3c:~/scripts$ gpioinfo gpiochip3
gpiochip3 - 32 lines:
line 0: unnamed unused input active-high
line 1: “PIN_11” unused output active-high
line 2: “PIN_13” unused output active-high
line 3: “PIN_12” unused output active-high
line 4: “PIN_35” unused input active-high
line 5: “PIN_40” unused input active-high
line 6: “PIN_38” unused input active-high
line 7: “PIN_36” unused input active-high
line 8: “PIN_15” unused input active-high
line 9: “PIN_16” unused input active-high
line 10: “PIN_18” unused input active-high
line 11: “PIN_29” unused input active-high
line 12: “PIN_31” unused input active-high
line 13: unnamed unused input active-high
line 14: unnamed unused input active-high
line 15: unnamed unused input active-high
line 16: unnamed unused output active-high
line 17: “PIN_22” unused input active-high
line 18: “PIN_32” unused input active-high
line 19: “PIN_33” unused input active-high
line 20: “PIN_7” unused input active-high
line 21: unnamed unused input active-high
line 22: unnamed unused output active-high
line 23: unnamed unused input active-high
line 24: unnamed unused input active-high
line 25: unnamed unused input active-high
line 26: unnamed unused input active-high
line 27: unnamed unused input active-high
line 28: unnamed unused input active-high
line 29: unnamed unused input active-high
line 30: unnamed unused input active-high
line 31: unnamed unused input active-high

Controlled the pin state using the CLI (PIN_11 which is physical pin 11 on the pin map but line 1 above (https://wiki.radxa.com/Rock3/hardware/3c/gpio)
gpioset --mode=time --sec=1 gpiochip3 1=1

OUTPUT:
Doesn’t say anything, but if you have a LED attached you’ll see some action.

And have made a little python script to set the pin using import gpiod

import gpiod
import time

def set_pin_val(chip_name, pin_offset, pin_val):
try:
    with gpiod.Chip(chip_name) as chip:
        line = chip.get_line(pin_offset)
        line.request(consumer="my_gpio_script", type=gpiod.LINE_REQ_DIR_OUT)

        line.set_value(pin_val)  # Set the pin to low

        line.release()
except Exception as e:
    print("Error:", e)

# Specify the GPIO chip name and pin offset
chip_name = "gpiochip3"
pin_offset = 1
pin_val = 0

# Call the function to set the pin to low
set_pin_val(chip_name, pin_offset, pin_val)

time.sleep(1)

pin_val = 1

set_pin_val(chip_name, pin_offset, pin_val)

I hope this might help others.

Thanks. The support for Rock 3C is awful, especially if you’re a beginner and bought this board for educational purposes.

Yes there’s definitely not the support there is for boards like the Raspberry Pi but from my limited experience they’re very capable boards and as a community, and with support from the manufacturer, we can hopefully draw the best from them. I think it would be great if the boffins at Radxa were to say “Hey we’ve got a really great stable of boards here already, lets shift our focus a little to sorting out high quality software solutions for our customers and put new board development on the back burner for a little while while we really nail things for the end user”. Dreams are free!

2 Likes

There are python examples in kernel.org ( you need to be careful about which version of libgpiod that you are using ) for example the current ROCK 4SE Bullseye CLI image has libgpiod v1.6.2 so this is the appropriate link https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python/examples?h=v1.6.2

After the latest update I’ve noticed that you can now use mraa on Rock 3C, it’s easier to use than gpiod, especially when coding. It also helped me to read data from a sensor I had trouble with when using smbus2. I just edited the sensor module and changed the approach for i2c bus, instead of using smbus2, i changed it to mraa, now it works fine.

1 Like