If you’re a RasPi.TV regular, you’ll know that one of my very favourite things is hacking around with the GPIO ports on the Raspberry Pi and sharing my findings with the world. Sometimes this involves documenting a new feature, sometimes it involves making a new project, using a new chip or just getting something working that I haven’t tried before.
But it nearly always involves Python programming and the brilliant RPi.GPIO Python library by Ben Croston. I’ve written all sorts of tutorials (~16) on RPi.GPIO because it is one of my favourite things. You may also have heard of the RasPiO GPIO Ruler that I Kickstarted recently.
Now There’s A New Front-end Called GPIO Zero
Last time I visited Pi Towers, a couple of months ago, Ben Nuttall showed me some embryonic code he’d started working on to make RPi.GPIO more user-friendly for end-users. Last week he took it to Beta release and started gently letting the world know about it. It’s called GPIO Zero. It looks like an interesting development and I wanted to blog about it, so yesterday I asked him some questions about GPIO Zero. Here are the answers (Ben’s words in italics)…
Why did you create GPIO Zero?
Ben N: I started the project to provide simple interfaces to everyday components like LEDs and buttons, to make playing around with bits & pieces from the CamJam Edukit much more accessible. I would open up a Python shell and come up with little examples and it proved to be very fast for prototyping ideas and seeing what works – just a few lines to get going and it’s easy to take it in any direction from there. It’s only been a few weeks and it’s already become a very usable library.
Who is it aimed at?
Ben N: I’ve designed it with education in mind, to help teachers and kids get going with physical computing without the friction of worrying about pull ups, edges and all the setup. But I think most people will find it very handy.
Does it supersede RPi.GPIO?
Ben N: It will probably supersede RPi.GPIO for general use, but GPIO Zero is entirely built on RPi.GPIO so that’s not going away! When I showed Ben Croston (the author of RPi.GPIO) an early draft he said it was great – and that his library was never written to be an end-user library, just to bridge the gap providing Python access to the GPIO pins. People will be able to do most general purpose stuff with GPIO Zero, and if a component isn’t provided, they can use the underlying general InputDevice and OutputDevice classes, or they can just use RPi.GPIO.
I’m sure many people will continue to use RPi.GPIO purely because that’s what all the tutorials on the web use. Hopefully GPIO Zero will catch on and people will see how much neater their code can be!
I must say I’m eternally grateful to Ben for maintaining it! Not to mention all the projects I’ve used it for the last three years.
Why did you choose BCM over any other pin numbering system?
Ben N: We made a decision in the Foundation last year that we would always use BCM numbering for our resources. 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. Also, counting pins is a nightmare whichever way you’re counting them – you’re better off using a port label like your RasPiO Portsplus
I completely agree with Ben about that. I detest counting pins and think it is an exercise best avoided altogether by having nice clear labels. That’s why I designed the above RasPiO Portsplus Board.
Will all official Raspberry Pi resources use GPIO Zero in future or is it
mainly for the most basic stuff?
Ben N: I imagine so. I’ve drafted up the code for some of the resources to see what they would look like – and I think they become much neater and make it easier to teach the concepts without getting bogged down with the code.
Some people might argue that you are abstracting away too much or making it too easy. How would you respond to that?
Ben N: I run a session for teachers at Picademy and in order to get them to make a button press control the camera module, I have to teach the concept of pull-ups. Then they have to teach that to their students. It’s not intended to be an electronics lesson, and it distracts from the purpose of the code, and the objective of the exercise. 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? I’ve got a button, and I’ve connected it to ground and pin 17 – now let’s make it take a picture. 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.
GPIO Zero is a Pythonic API, it’s a nice clean interface that allows you to get stuff done. That’s what high level languages and frameworks are designed to provide. Python code should be easy to write and easy to read. The API should be so simple that once you know how to do one thing, you can probably guess how to do another. It’s good to be able to say what you want code to do, rather than how to do it.
Is there anything else you’d like to tell the world about GPIO Zero?
Ben N: Expect a full release within a month. It’ll be pre-installed in Raspbian from then on. Until then, install the beta with pip. Check out the recipes for ideas and you’ll get the gist of how it works. If there’s anything you think should be added, just let me know on GitHub or the Google Doc.
Thank you Ben for answering those questions.
Where Can I Get It?
The current Beta release and installation instructions are found here.
So What’s it Like?
Well for starters it’s simpler and more “natural language” in its approach. Let’s do a side by side comparison of the code needed to light an LED on GPIO25 and switch it off after 5 seconds. (Don’t try to run this code, as it contains both)…
# RPi.GPIO GPIO Zero from RPi import GPIO from gpiozero import LED from time import sleep from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setup(25, GPIO.OUT) led = LED(25) GPIO.output(25,1) led.on() sleep(5) sleep(5) GPIO.output(25,0) led.off()
GPIO Zero removes the need to set up the GPIO ports manually. All you do is tell the program what you want to do and it handles that part for you. It also sets warnings off (a feature I never actually use, but it’s an extra line of code you had to type if you did).
Natural Sounding Language
But Ben’s main point in developing GPIO Zero is to create a Python GPIO interface that can be addressed with natural sounding language, which makes it very approachable for learning.
Rather than having to get your head around…
GPIO.setup(25, GPIO.OUT) “I want to set up GPIO25 as an output”, you have…
led = LED(25) “I want to put an LED on GPIO25”, which makes more sense to a human being.
GPIO.output(25,1) “Switch our output on GPIO25 to On”, we have…
led.on() “Switch on our LED”
There is even a function for
blink(), so you could blink an LED in 3 lines of code, which is ideal to grab the interest of a ‘small person’ within the limited time of their attention span.
from gpiozero import LED led = LED(25) led.blink()
In my teaching experience, if you can ‘get a result’ within a few minutes, you’ve got them hooked and interested. If it takes too long, you’re doomed!
More To Come
I haven’t yet had an in-depth play with GPIO Zero, but will now do so and report my findings in my next blog post.
When I learned to drive, I happily drove round in a manual (stick shift) car for 20 years before I bought my first automatic. I took to an automatic very quickly and love it and I don’t think I would want to go back to manual.
I suspect it may be a bit like this with GPIO Zero. There’s nothing wrong with RPi.GPIO – I’ve learned how to use it, it works well and I love it. But if you don’t need to ‘get your hands dirty under the hood’, GPIO Zero provides a nice clean way of getting the job done in a manner that is a lot easier to understand and quicker for learners. It’s another option, and it’s good to have choices.