Ben Nuttall is the Community Manager at Raspberry Pi and has with Dave Jones (GitHub) done some extraordinary work with GPIO Zero to produce a simple and lightweight interface to GPIO devices , all based on python. GPIOZero is obviously specific to the Raspberry Pi and works across all models of Pi including the newest PiZeroW for as long as you run a flavour of Raspian. It comes pre-installed with Raspian Jessie but if you have an older or different version of linux see this page for manual installation (including PC/Mac Remote GPIO). Let’s quickly compare a ‘traditional python script’ and a GPIO Zero script for the same function(s) – blinking an LED:
1st a conventional python script;
## Import time and GPIO library import time import RPi.GPIO as GPIO ## Use board pin numbering and set GPIO Pin 17 to OUT
GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) while True: GPIO.output(17,True) ## Turn on Led time.sleep(1) ## Wait for one second GPIO.output(17,False) ## Turn off Led time.sleep(1) ## Wait for one second
Lets compare this to the equivalent using the GPIO Zero library;
from gpiozero import LED from time import sleep led = LED(17) while True: led.on() sleep(1) led.off() sleep(1)
This is ultimately so much more ‘readable’ for the beginner programmer – I say ‘NO CONTEST’ – GPIO Zero rocks !!