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.