0

I recently bought myself a raspberry pi 5 and am learning how to work with the gpio pins. The gpio pins require a library to control them. These library’s that are compatible with the Raspberry Pi 5 are the GPIOZERO and gpiod libraries but I find the GPIOZERO library to be too unintuitive so I prefer the gpiod library. While using the gpiod library, I found that it doesn’t support internal pull up/down resistors, nor pwm. Although I could just use external pull up resistors, it would be a lot more efficient to use internal ones. I’ve tried this so far:

import gpiod
Switch = 17
chip = gpiod.Chip('gpiochip4')
Switchline = chip.get_line(Switch)
Switchline.request(consumer="MYDEVICE", type=gpiod.LINE_REQ_DIR_OUT) 
yourline.set_value(1)
chip = gpiod.Chip('gpiochip4')
Switchline = chip.get_line(Switch)
Switchline.request(consumer="MYDEVICE", type=gpiod.LINE_REQ_DIR_IN)
Value = Switchline.get_value()
print(Value)

This was unsuccessful due to the line already being set as an output, and it wouldn’t let me change it. Does anyone know of any other ways I could enable an internal pull up resistor into my code?

Kent Gibson
2443 silver badges11 bronze badges
asked Aug 8, 2024 at 21:23
1
  • NOTE the code you posted is nonsense. It would generate a python error independent of gpiod. Commented Aug 9, 2024 at 9:56

2 Answers 2

1

libgpiod has supported bias since v1.5.

If you are writing new code for libgpiod you should upgrade to the latest version (v2.2.1 at time of writing) using

pip install gpiod

That has a different API from v1, and more functionality including debounce. There are examples using the latest API, with watch_line_value.py demonstrating how to request a line as input with pull-up, as well as debounce and edge detection. You can skip those if you don't need them.

Just requesting the line as an input with pull-up could look like:

import gpiod
Switch = 17
Switchline = gpiod.request_lines(
 "/dev/gpiochip4",
 config={
 Switch: gpiod.LineSettings(
 direction=gpiod.Direction.INPUT,
 bias=gpiod.Bias.PULL_UP,
 )
 })

If you still want to use libgpiod v1, you need to provide a flags parameter specifying additional configuration for the line and specify the appropriate bias flag:

import gpiod
Switch = 17
chip = gpiod.Chip('gpiochip4')
Switchline = chip.get_line(Switch)
Switchline.request(consumer="MYDEVICE", type=gpiod.LINE_REQ_DIR_IN, flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)
Value = Switchline.get_value()
print(Value)

That prints 1 for me when run on a Pi4, as I don't have a Pi5 at hand, and so using gpiochip0 rather than gpiochip4.

You can generate the documentation for the libgpiod python bindings locally using

python -m pydoc -p 1234

and pointing your browser at http://localhost:1234/gpiod.html.

answered Aug 9, 2024 at 3:27
1
  • Yes I will accept Commented Aug 11, 2024 at 10:17
1

gpiod DOES support pullup (although the documentation is poor).

# LINE_REQ_FLAG_BIAS_DISABLE = 8
# LINE_REQ_FLAG_BIAS_PULL_DOWN = 16
# LINE_REQ_FLAG_BIAS_PULL_UP = 32

NONE of the existing libraries support hardware PWM, only software PWM (at least I am unaware of any). libgpiod ONLY supports GPIO functionality it was never intended to support other functions.

Note gpiozero uses lgpio as its underlying library. This is reasonably documented and behaves more like the older libraries.


Fixing the errors in your code the following should work; the following for pre Pi5 models (gpiochip0). (I do not think this is good code.)

import gpiod
Switch = 17
chip = gpiod.Chip('gpiochip0')
Switchline = chip.get_line(Switch)
Switchline.request(consumer="MYDEVICE", type=gpiod.LINE_REQ_DIR_OUT) 
Switchline.set_direction_input()
Value = Switchline.get_value()
print(Value)
Switchline.set_flags(gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)
print(Value)
answered Aug 8, 2024 at 23:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.