GPIO Zero for the Raspberry Pi – by George, I think I’ve got it!

When Ben Nuttall announced his GPIO Zero Python library a few weeks ago, I was very sceptical. It seemed to me to be dumbing-down the flexibility of RPi.GPIO. I didn’t like it. The truth is, I’d got so comfortable (and, perhaps, complacent) with RPi.GPIO that I was resistant to the idea that Python could be used in a different way to control the GPIO pins of the Pi.

However, Cat Lamin, who came up with the idea of Coding Evenings, asked on Twitter whether someone could help her to understand GPIO Zero and whether it could be used to teach slightly younger children (year 6, I believe it was) about the basics of Python in an exciting way where they could interact with the real world. So, spurred on by Tim Richardson (who had attended a Peterborough Coding Evening where Ben spoke about the new library), I decided to take the plunge. I took the robot I’d built using CamJam EduKit 3 and installed GPIO Zero on it. I then looked at the documentation for the library and within about half an hour (some of which was me being really stupid) I had a working robot with very few lines of code:

from gpiozero import Robot
import time
robot = Robot(right=(10,9), left(8,7))

robot.forward()
time.sleep(1)
robot.left()
time.sleep(1)
robot.right()
time.sleep(1)
robot.backward()
time.sleep(1)
robot.stop()

The above is a simple ‘demo’ script that moves the robot in all four directions and then stops it. It’s so blindingly easy!

RPi.GPIO still has its place (in fact, it’s what GPIO Zero is built on) if you want to do complicated things or if you want to drill down into the lower level stuff. But for just doing things like interacting with simple electrical components, and doing it in a clear, concise manner, GPIO Zero is tough to beat.

From an education point of view, it’s got great possibilities. If a child is moving from a visual programming language like Scratch to text-based programming in Python, GPIO Zero is a healthy stepping-stone.

So, I guess that teaches me a valuable lesson – don’t jump to conclusions if you haven’t tried something!

If you’d like to install and try out the library yourself, go to this page and follow the instructions. If you want a concise presentation showing you what you can do with it at a basic level, check out the SpeakerDeck slides below (these are what Ben used at the Peterborough Coding Evening):

I really commend Ben and Dave Jones for the development of this new library – it’s easy-to-use and has a commendable clarity. There are some exciting developments coming up in future versions, too, which is sure to make it more widely used. The future is bright for GPIO Zero.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.