Wednesday 13 January 2016

GPIO Zero - making coding less language intensive.




In a previous post Getting Physical with Python I wrote about the difficulty some of the younger children in my computing group had with the volume of typing required to get started with physical computing. They did not struggle with understanding but it took too much time and help to enter the volume of text required. This took away some of the excitement and slowed things down.

At the time I thought to speed this up it would be good to write a python library to reduce the amount of text needed to get things to happen. Unfortunately I had lots of other things to do and this never went anywhere. However someone else also thought it would be good to make it easy to get started with physical computing and was able to do something about it.

That person was Ben Nuttall of the Raspberry Pi Foundation. Along with Martin O'Hanlon and Dave Jones he has created GPIO Zero. You can read his account of how it happened on his blog.

This python library can be used to very simply control components using the GPIO pins. The initial function set is based around the popular CamJam EduKits  (Kit 1- Starter, Kit 2 - Sensors) and makes a great starting point for physical computing using python.

A simple light and button combination can be controlled with the below example:

from gpiozero import LED, Button led = LED(15) button = Button(14) button.when_pressed = led.on button.when_released = led.off

instead of something like this:

import os 
import time  
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False) 


GPIO.setup(14, GPIO.IN)
GPIO.setup(15, GPIO.OUT) 

while True: 
        if GPIO.input(14) == False: 
              GPIO.output(15, HIGH)   
        else: 
               GPIO.output(15, LOW)  

 time.sleep(0.5) 

The reduction in volume of code and setup required is brilliant. GPIO Zero is an amazing tool for education. This is especially true where the volume of text entry is a barrier (either with younger or SEN children).

The tool allows the focus to be on the programming concepts and not on the entry of text. When i worked with my HomeEd computing group there was a difficulty fro a number of the students (aged between 5 and 15) in using the GPIO library as there was a lot of code to enter. They were generally happy with what they were trying to achieve but found that it took a long time to enter the lines of code required just to light up the LEDs.

This meant that in the one hour session that is what we achieved, lighting up the LEDs. Whilst this was a success and the children were happy with getting there it would have been much better to spend more of the time in the session exploring what could be done rather than entering lots of text.

GPIO Zero takes away some of the burden allowing children to focus on what they are trying to achieve rather than on copying out lots of lines of code (especially the set up parts that are conceptually more difficult to grasp and result in questions about what is BCM etc).

I have found that where I have used this it has meant I can more on more quickly and cover more of the computational thinking ideas where previously there would have been more time waiting for the students to catch up with the typing required. It also works well to satiate the desire for instant gratification that appears to be fairly common among my pupils. They only have to spend a short time entering code before they can see a result.

It is also much easier in a classroom to debug the code they have written if there are errors. The reduced volume of code makes for less searching to find the capitol that should't be there. This make students more able to do it themselves or makes it quicker for me when they can't see what is wrong. The reduction in time taken here give me the opportunity to get to more pupils and help them to progress.



I have also used this at home with my son (8) whilst he has been creating a robot using the CamJam EduKit 3 - Robots. This was really powerful because it allowed him to achieve results in short pockets of time before he lost focus and wanted to move on. He used the provided worksheets to set up the robot and connect the components and I translated the code parts into GPIO Zero for him to get the robot working.

So in summary the feedback is - Thanks Ben this is an awesome tool to help me teach computing.

If you are interested in using GPIO Zero there is a great getting started guide on the Raspberry Pi website in the resources 'Learn' Section.

More information can be found on pythonhosted.org or on GitHub. There is also a Google Doc with information and a place to add comments / requests.