GPIO pins not responding in code

Hello.

I’ve been stuck all day trying to get any of the GPIO pins to change state from a small python script.

I followed the example in the wiki, and the IRQTest.py just said false with every loop (ran as root).

I used this library https://github.com/vsergeev/python-periphery that is also in the IRQtest.py with this code:

import time
from periphery import GPIO

gpio = GPIO("/dev/gpiochip0", 40, "out")
gpio.write(False)
time.sleep(1)
gpio.write(True)
time.sleep(2)

gpio.close()

Basically, I am trying to activate a relay.

The pin stays at 1.38 volts while the script runs (as root). I’ve tried /dev/gpiochip1 as well, no change.

I can change the pin state in the bios, and when I set it to low, the pin reads at .03 volts. So I think the GPIO pins themselves are fine. I am stumped as to why doing anything from linux just doesn’t work.

I tried Debian 10 and Fedora 32

Thanks

Some more digging on my end…
I am trying to use libgpiod, which accesses the gpio pins through a character device. The sysfs method is deprecated and removed in newer kernels, so everybody is going to have to adjust anyway.

See https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/

So, what I am now essentially asking is…What is the correlation between the physical pin number and the line number? 4 gpiochips show up, and 0 and 1 both have 80 lines.

# gpiodetect
gpiochip0 [INT3453:00] (80 lines)
gpiochip1 [INT3453:01] (80 lines)
gpiochip2 [INT3453:02] (20 lines)
gpiochip3 [INT3453:03] (35 lines)

Which gpiochip is the 40 pin setup on the board?

gpioinfo
gpiochip0 - 80 lines:
	line   0:      unnamed       unused   input  active-high 
	line   1:      unnamed       unused   input  active-high 
	line   2:      unnamed       unused   input  active-high 
	line   3:      unnamed       unused   input  active-high 
	line   4:      unnamed       unused   input  active-high 
	line   5:      unnamed       unused   input  active-high 
	line   6:      unnamed       unused   input  active-high 
	line   7:      unnamed       unused   input  active-high 
	line   8:      unnamed       unused   input  active-high 
	line   9:      unnamed       unused   input  active-high 
	line  10:      unnamed       unused  output  active-high 
	line  11:      unnamed       unused  output  active-high 
	line  12:      unnamed       unused  output  active-high 
	line  13:      unnamed       unused   input  active-high 

From other pages I’ve seed, the broadcom chips have labels. And there is no correlation between pin number and line number. I’ve already caused a crash and reboot by setting line 47 to high.

I did some trial and error and nailed down the chip and line number of a few pins using a relay and patience.

I then stared at the numbers in the bios for the pins and realized there is a pattern.

Here are my results, most of them checked by hand using the gpioset utility.

bios_number physical_pin chip_number line_number
60 10 0 60
61 8 0 61
79 23 1 3
80 24 1 4
81 26 1 5
82 21 1 6
83 19 1 7
88 11 1 12
110 3 1 34
111 5 1 35
112 27 1 36
113 28 1 37
114 22 1 38
115 32 1 39
134 36 1 58
136 13 1 60
137 15 1 61
139 29 1 63
140 31 1 64
141 33 1 65
143 37 1 67
145 16 1 69
146 18 1 70
161 7 2 5
162 12 2 6
163 35 2 7
164 38 2 8
165 40 2 9
3 Likes

Now, going back to my original snippet, I see that physical pin 40 (off in the far corner :stuck_out_tongue:) is on /dev/gpiochip2 on line 9. And now it works as expected!

If at somepoint Ubuntu or Debian finally drops GPIO sysfs, I hope someone googling around sees this post. Or if you are using Fedora IoT right now.

I’ll probably make a fork of python-periphery to integrate this mapping more easily, so you only have to worry about the physical pin layout number.

3 Likes

Thanks @zerrofour4 for the good work!

This is very useful for other users to see. :smiley:

Thanks so much for this @zerrofour4, saved me a lot of time and frustration :smile:

1 Like

Thank you for the fine detective work. I just started looking into the GPIO thing on the Odyssey definitely saved me some work by posting your findings