Pi-Plates
Posts: 27
Joined: Sun Feb 08, 2015 12:34 pm

gpiod and Pullups

Tue Nov 28, 2023 2:59 am

Maybe I'm looking in the wrong places or just plain sleepy but can somebody explain to me how to enable a pull up on an input with gpiod?

Here's my code so far:

ACK=23
chip=gpiod.Chip('gpiochip4')
ppACK=chip.get_line(ACK))
ppACK.request(consumer="ACK", type=gpiod.LINE_REQ_DIR_IN)

Looking at the help file for gpiod mentions this attribute: BIAS_PULL_UP = 3 but I can't figure out where or how to apply it.

Pi-Plates
Posts: 27
Joined: Sun Feb 08, 2015 12:34 pm

Re: gpiod and Pullups

Tue Nov 28, 2023 11:01 am

Got some sleep and tried again. At the end of the gpiod help file is a list of flags including this one: LINE_REQ_FLAG_BIAS_PULL_UP = 32

Executing this caused an error:

Code: Select all

ppACK.request(consumer="ACK", type=gpiod.LINE_REQ_DIR_IN, flags=LINE_REQ_FLAG_BIAS_PULL_UP)
NameError: name 'LINE_REQ_FLAG_BIAS_PULL_UP' is not defined

But, just putting the value in did work:

Code: Select all

ppACK.request(consumer="ACK", type=gpiod.LINE_REQ_DIR_IN, flags=32)
Conclusion: debugging code is easier when you've had rest.

hippy
Posts: 15878
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: gpiod and Pullups

Tue Nov 28, 2023 1:39 pm

That -
ppACK.request(consumer="ACK", type=gpiod.LINE_REQ_DIR_IN, flags=LINE_REQ_FLAG_BIAS_PULL_UP)
should be -
ppACK.request(consumer="ACK", type=gpiod.LINE_REQ_DIR_IN, flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)
But whether that works will depend on what Python binding library you have.

On my Pi 3B Buster using 'sudo apt install python3-gpiod' gives version 1.2 and it's not available -

Code: Select all

pi@Pi3B:/tmp $ python3
Python 3.7.3 (default, Oct 11 2023, 09:51:27)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiod
>>> dir(gpiod)
['Chip', 'ChipIter', 'LINE_REQ_DIR_AS_IS', 'LINE_REQ_DIR_IN', 'LINE_REQ_DIR_OUT', 'LINE_REQ_EV_BOTH_EDGES',
LINE_REQ_EV_FALLING_EDGE', 'LINE_REQ_EV_RISING_EDGE', 'LINE_REQ_FLAG_ACTIVE_LOW',
'LINE_REQ_FLAG_OPEN_DRAIN', 'LINE_REQ_FLAG_OPEN_SOURCE', 'Line', 'LineBulk', 'LineEvent', 'LineIter',
'__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'find_line', 'version_string']
>>> gpiod.version_string()
'1.2'
>>> gpiod.LINE_REQ_FLAG_BIAS_PULL_UP
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'gpiod' has no attribute 'LINE_REQ_FLAG_BIAS_PULL_UP'
But it is there on my Pi 4B 1GB Bookworm 32-bit, version 1.6.3 -

Code: Select all

pi@Pi4B:/tmp $ python3
Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiod
>>> dir(gpiod)
['Chip', 'ChipIter', 'LINE_REQ_DIR_AS_IS', 'LINE_REQ_DIR_IN', 'LINE_REQ_DIR_OUT', 'LINE_REQ_EV_BOTH_EDGES',
'LINE_REQ_EV_FALLING_EDGE', 'LINE_REQ_EV_RISING_EDGE', 'LINE_REQ_FLAG_ACTIVE_LOW',
'LINE_REQ_FLAG_BIAS_DISABLE', 'LINE_REQ_FLAG_BIAS_PULL_DOWN', 'LINE_REQ_FLAG_BIAS_PULL_UP',
'LINE_REQ_FLAG_OPEN_DRAIN', 'LINE_REQ_FLAG_OPEN_SOURCE', 'Line', 'LineBulk', 'LineEvent', 'LineIter',
'__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'find_line',
'version_string']
>>> gpiod.version_string()
'1.6.3'
>>> gpiod.LINE_REQ_FLAG_BIAS_PULL_UP
32

Pi-Plates
Posts: 27
Joined: Sun Feb 08, 2015 12:34 pm

Re: gpiod and Pullups

Tue Nov 28, 2023 3:49 pm

Good stuff! I figured out the gpiod.LINE_REQ_FLAG_BIAS_PULL_UP right after I posted.

hippy
Posts: 15878
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: gpiod and Pullups

Wed Nov 29, 2023 10:41 am

Having spent more time recently in MicroPython than Python 3, I had forgotten this will work to provide missing named attributes -

Code: Select all

import gpiod
try    : gpiod.LINE_REQ_FLAG_BIAS_PULL_UP
except : gpiod.LINE_REQ_FLAG_BIAS_PULL_UP = 32
With that, the same code now runs on Buster (importing gpiod 1.2) and Bookworm (importing gpiod 1.6.7), though I didn't check what's actually happening with the physical GPIO itself.

Return to “Python”