The Raspberry Pi is the perfect computer for learning. The Linux-based Raspbian OS has Python built in, which makes it a great first system for beginner coders. Its General Purpose Input/Output (GPIO) pins make it easy for budding makers to experiment with DIY electronics projects.
It’s especially easy when you use code libraries that control these pins, and the popular RPi.GPIO Python library is an excellent example of such a library. But is it the best path for beginners? Join us as we investigate.
What Is GPIO Zero?
The GPIO Zero library is a Python library for working with GPIO pins. It was written by Raspberry Pi community manager Ben Nuttall. Aimed at being intuitive and “friendly,” it streamlines Python code for most regular Raspberry Pi use cases.
Combining simple naming practices and descriptive functions, GPIO Zero is more accessible for beginners to understand. Even seasoned users of the RPi.GPIO library may prefer it—and to understand why, let’s take a look at how RPi.GPIO compares to GPIO Zero.
What’s Wrong With RPi.GPIO?
Nothing. Nothing at all. RPi.GPIO was released in early 2012 by developer Ben Croston. It is a robust library allowing users to control GPIO pins from code. It features in almost every beginner project we’ve covered.
Despite its extensive use, RPi.GPIO was never designed for end users. It is a testament to RPi.GPIO’s good design that so many beginners use it nonetheless.
What’s So Good About GPIO Zero?
When you are learning Python code, you learn that it should be easy to read and as short as possible. GPIO Zero aims to cover both points. Built on top of RPi.GPIO as a front-end language wrapper, it simplifies GPIO setup and usage.
Consider the following example, setting up and turning on an LED:
The above code should be pretty familiar to anyone who has used their Pi to control LEDs.
The RPi.GPIO library is imported, and a pin for the LED is declared. The pin layout type is set up (BCM and BOARD mode are explained in our GPIO guide), and the pin is set up as an output. Then, the pin is turned on.
This approach makes sense, but the GPIO Zero way of doing it is much simpler:
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.
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.
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.
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:
The button module simplifies setup for push buttons. It knows buttons are inputs, so uses the declared pin number for setup. Checking for a button press is easier too, with the .is_pressed to detect button presses.
We used this exact functionality in the Raspberry Pi button tutorial, which is a great way to familiarize yourself with the differences in the libraries.
Users of the RPi.GPIO library will notice that the internal pull-up/pull-down resistors of the Pi are not set up in code. This raises an interesting question. Is it essential for beginners to know about pull-up/down resistors? Again, Ben Nuttall has an answer to this question:
“You might argue that it’s good to know about pull ups and pull downs, and you’d be right—but why do I have to teach that on day one?[…] If you want to teach the electronics in more depth there’s plenty of scope for that—but it shouldn’t be mandatory if you’re just getting started.”
On the whole, the simple approach of GPIO Zero is likely a good thing for beginners and veterans alike. Besides, RPi.GPIO isn’t going anywhere. It will always be there to switch back to if needed.
Is Python the Only Option?
Python is the language the Pi is known for, but it’s not the only option. If you are already familiar with programming in the C language, then Wiring Pi has you covered.
Alternatively, if you already program in JavaScript, Node.js can easily be installed on the Pi. GPIO access is available through the rpi-gpio npm library. Ruby on Rails can also be installed on the Raspberry Pi, though the Pi might not be the best way to learn Rails!
All of these alternatives, along with multi-language libraries like the excellent pigpio can make choosing a library confusing. This is where GPIO Zero excels: for beginners wondering how and where to start.
If you are at a point where you need something it does not provide, you will be more than ready to dive into these other libraries at your own pace.
Getting Started With GPIO Zero Yourself
GPIO Zero is the newest library to make a splash for the Pi and with good reason. For most users, it makes coding for GPIO pins simpler to read and quicker to write.
Given the Raspberry Pi’s usage in education, anything that makes learning more natural is a good thing. While RPi.GPIO has been perfect up until now, GPIO Zero takes a good idea and makes it even better.
A great way to get started with GPIO Zero is to take a beginner project like the Musical Door Sensor and port it to the new library.
Read the full article: Why GPIO Zero Is Better Than RPi.GPIO for Raspberry Pi Projects
from MakeUseOf https://ift.tt/2xQzyRk
via IFTTT
No comments:
Post a Comment