The Raspberry Pi is the perfect computer for learning. The Linux-based Raspbian OS has Python built in, making it a great first system for novice programmers. Its General Purpose Input/Output (GPIO) pins allow budding makers to experiment with electronic designs.

This is especially easy when you use code libraries that manage these pins, and the popular Python RPi.GPIO library is a great example of such a library. But is it the best way 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 optimizes Python code for most common Raspberry Pi use cases.

Combining simple naming techniques and descriptive features, GPIO Zero is more accessible to beginners. Even experienced users of the RPi.GPIO library may prefer it — and to see why, let’s see 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. This is a robust library that allows users to control GPIO pins from code. It is present in almost every project. we have already considered.

Despite widespread use, RPi.GPIO was never intended for end users. This is a testament to the good design of the RPi.GPIO that many beginners use.

What’s good about GPIO Zero?

When you study Python code, you will learn that it should be easy to read and as short as possible. GPIO Zero aims to cover both points. Based on RPi.GPIO as a front-end language wrapper, it makes it easy to set up and use the GPIO.

Consider the following example of setting up and turning on an LED:

Code for setup and LED for output using RPi.GPIO library

The above code should be familiar to anyone who has used their pi’s to drive LEDs.

The RPi.GPIO library has been imported and the pin for the LED has been declared. Pinout type (BCM and BOARD modes are described in our GPIO manual.) and this pin is configured as an output. Then the pin is turned on.

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

Setting up an LED with GPIO Zero

GPIO Zero has a module for LEDs imported at startup. This means that you can declare a pin number code and call the method led.on() .

Why is the GPIO Zero approach better?

There are several reasons why this method of operation is an improvement on the RPi.GPIO.

First, it meets the requirement of «easy to read, as little as possible». While the RPi.GPIO setup statements are simple enough to understand, they are not needed. The LED will always be the output, so GPIO Zero makes the pins behind the scenes. The result is just three lines of code to set up, and then the LED lights up.

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

“The board numbering may seem simpler, but I would argue that it makes new users think that all the pins have a general purpose, which they do not. Connect an LED to pin 11, why not connect a few more to pins 1, 2, 3 and 4? Well 1 3V3. 2 and 4 5V. Lack of awareness of pin assignments can be dangerous.”

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

Is GPIO Zero really better?

While this seems simpler at first glance, does the new library have any issues? As with any new coding library, this is a matter of opinion. On the one hand, removing the installation code is great for both beginners and experienced programmers. Writing code is easier and faster.

On the other hand, knowing what is going on is important for learning. Take the button setup example from the GPIO Zero documentation:

GPIO null button code example

Module buttons simplifies button setup. It knows that the buttons are input, so it uses the declared pin number to set it up. It’s also easier to check if a button is pressed, with .is_pressed to detect button presses.

We used this exact functionality in the tutorial this is a great way to familiarize yourself with the differences in the libraries.

Users of the RPi.GPIO library will notice that the Pi’s internal pull-down/pull-down resistors are not specified in the code. This raises an interesting question. Is it mandatory for beginners to know pull-up/pull-down resistors? Once again, Ben Nuttall has the answer to this question:

“You may argue that it is good to know about pull-ups and descents, and you would be right, but why should I teach it on the first day? […] If you want to teach electronics in more depth, there’s plenty of room to do so — but it shouldn’t be mandatory if you’re just starting out.»

All in all, the simple GPIO Zero approach is probably useful for beginners and veterans alike. Also, RPi.GPIO isn’t going anywhere. He will always be there to switch back 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 C programming, then Wiring Pi will help you.

Also, if you’re already a JavaScript programmer, Node.js can be easily installed on the Pi. GPIO access is available through the rpi-gpio npm library. Ruby on Rails can also be installed on a Raspberry Pi, although the Pi might not be the best way to learn Rails!

All of these alternatives, along with multilingual libraries like the excellent pigpio, can make library selection confusing. This is where the GPIO Zero excels: for beginners wondering how and where to start.

If you’re at the point where you need something it doesn’t provide, you’ll be more than willing to dive into those other libraries at your own pace.

Getting Started with GPIO Zero Yourself

GPIO Zero is the latest library built for the Pi, and for good reason. For most users, this makes coding for GPIO pins easier to read and faster.

Given the use of the Raspberry Pi in education, anything that makes learning more natural is a good thing. While the RPi.GPIO has been perfect so far, 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 Musical Door Sensor. and transfer it to the new library.

Похожие записи