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

Beginners • Re: Newbie: gpio vs lgpio vs gpiozero?

$
0
0
By "gpio" I think you refer to RPi.GPIO.

gpiozero is a wrapper for other GPIO libraries, including RPi.GPIO and lgpio. It provides a common API so you can use whichever backend library suits without having to change your code. You may be able to do more, or less, with the backend library directly, and you may have personal preferences for one API over another.
lgpio is the default backend for recent gpiozero, though it will fall back to other libraries if lgpio is not installed for whatever reason. FWIW, the Pi engineers recommend gpiozero as the Python module of choice, with lgpio as the backend.

The backend libraries fall into two broad classes - those that access hardware registers directly, e.g. RPi.GPIO, and those that use the standard Linux kernel interface, e.g. lgpio and gpiod. The libraries that use the standard kernel interface can live peacefully together, as the kernel prevents any contention. Libraries that access hardware registers directly are faster, as writing to registers is much quicker than asking the kernel device driver to do it, but they provide no protection against concurrent access and can conflict with each other and themselves and with the libraries that use the kernel. You use the libraries that access registers directly at your own risk. Also, those libraries do not work with the Pi5 which has a drastically different architecture with the GPIO managed by the RPI1 chip which is not backwardly compatible with the older Pis. The kernel interface, and the libraries that use it, works on all Pis.

The "if __name__ = '__main__': " statement allows your Python code to be imported by another Python module. The code inside the if is only called when the module is run directly by Python, but not if another module imports it. If you aren't going to import your code anywhere else then it is not needed, but it commonly used anyway - just in case.

TL;DR:
1. All can be installed, and gpiozero is a wrapper library, but wrt living peacefully - Yes (kernel interface libraries) and No (direct register access libraries).
2. Link provided above for lgpio.
3. There is no short answer for this one, and I only scratch the surface above. But then your question is rather broad. How about you specify some requirements so we can suggest and explain the best fit in your case?

Statistics: Posted by warthog618 — Sun Sep 01, 2024 3:59 am



Viewing all articles
Browse latest Browse all 5157

Trending Articles