Why GPIO Zero Is Better Than RPi.GPIO for Raspberry Pi Projects

This approach makes sense, but the GPIO Zero way of doing it is much simpler:

Setting up an LED with GPIO Zero

computer science computer science computer science computer science computer science

GPIO Zero has a module for LEDs, imported at the start. This means you can declare the pin number, and call the led.on() method. computer science computer science computer science computer science computer science

Why Is GPIO Zero’s Approach Better?

There are some reasons why this method of working is an improvement on RPi.GPIO.

Firstly, it meets the “easy to read, short as possible” requirement. While the RPi.GPIO setup statements are easy enough to understand, they’re not necessary. An LED will always be an output, so GPIO Zero sets up the pins behind the scenes. The result is just three lines of code to set up, then light an LED. computer science computer science computer science computer science computer science

You might notice that there is no board mode setup in the GPIO Zero example. The library only uses Broadcom (BCM) numbering for the pins. Library designer Ben Nuttall explains why in a 2015 RasPi.tv interview:

“BOARD numbering might seem simpler but I’d say it leads new users to think all the pins are general purpose—and they’re not. Connect an LED to pin 11, why not connect some more to pins 1, 2, 3 and 4? Well 1 is 3V3. 2 and 4 are 5V. A lack of awareness of what the purpose of the pins is can be dangerous.”

Put this way, it makes absolute sense to use the BCM numbers. Given that it GPIO Zero will be standard in the Raspberry Pi documentation going forward, it’s worth learning!

Is GPIO Zero Actually Better?

While it seems more straightforward on the surface, does the new library have any problems? As with any new coding library, it is a matter of opinion. On the one hand, removing the setup code is excellent for beginners and seasoned coders alike. Writing code is more straightforward and quicker. computer science computer science computer science computer science computer science

On the other hand, knowing exactly what is going on is important for learning. Take the example of setting up a button from the GPIO Zero documentation:

Prev2 of 4Next

Leave a Reply

Your email address will not be published. Required fields are marked *