Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4856

Python • Re: PI5, Bookworm and GPIO control

$
0
0
Python is totally fine for this level of control, though you could also just use the command line tools in the gpiod package if that is all you are doing.

e.g. using the gpiod tools packaged in Pi OS (which are still v1):

Code:

gpioset 4 5=0
Note that whether the value set by a process sticks after that process exits depends on the GPIO driver in your kernel. For old kernels it will reset to the default on exit, but for recent kernels, and I'm pretty sure that includes all Pi5 kernels, it will remain as set. This is a function of the GPIO driver and there is nothing gpiod or the tools can do about it.

If you want to go the Python route, the latest gpiod available via PyPI is version 2, while the version packaged by Pi OS, and used in the Tom's Hardware example, is still version 1.
gpiod version 2 has a totally different API, as it was impossible to add new features to the old API in a sane fashion so it got a major rework.

For gpiod version 2 there are examples of common use cases in the source tree - https://github.com/brgl/libgpiod/tree/m ... n/examples. If those aren't sufficient then feel free to suggest additions. And, as I've written in viewtopic.php?p=2207369&hilit=gpiod#p2207369, you can use pydoc to browse the module documentation.
MFET_line.set_value(1) in both instances (GPIO21 and GPIO5) causes the relay to switch off - this says to me that regardless of the pull down default the MFET_line.set_value(1) tells the PI to go High on the GPIO? Is this correct? This kind of hurts my brain as I was expecting (1) to basically trigger the GPIO to the opposite of whatever its state is ie if high on boot - go low and vice versa - I assume this assumption has made an ass of me?!
Yeah, value and bias are independent, and the function is called set_value(), not toggle_value() or do_magical_things_based_on the_line_state() ;). It is stateless and sets the line to the logical level you asked, which is typically the same as the physical level, or the reverse if the line is configured active low.

So the gpioset example above could also be:

Code:

gpioset --active-low 4 5=1
In Python, using the latest gpiod, and using active low, might look something like this:

Code:

import gpiodfrom gpiod.line import Direction, Valuechip = "/dev/gpiochip4"line = 5config={line: gpiod.LineSettings(direction=Direction.OUTPUT, active_low=True, output_value=Value.ACTIVE )}req = gpiod.request_lines(chip, config=config)
The req is captured in case you want to subsequently change the value - else you can just exit.

Can't help you with gpiozero - never used it, and not sure why you would in this case - gpiod is sufficient and more portable.

Statistics: Posted by warthog618 — Wed Apr 03, 2024 12:58 am



Viewing all articles
Browse latest Browse all 4856

Trending Articles