User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

GPIO Zero - a new Python library designed for education

Thu Oct 01, 2015 10:35 am

[Cross-posted from the Education sub-forum]

I've been working on a new Python library that provides a simple interface to everyday GPIO components. It sits on top of the standard RPi.GPIO but requires much less code to get going. It's called GPIO Zero.

Simple example:

Code: Select all

from gpiozero import LED, Button

led = LED(2)
button = Button(3)

button.when_pressed = led.on
button.when_released = led.off
I'm aiming for minimal boilerplate (no setmode, setwarnings), easy to read, guessable and obvious.

I've released a public beta and I plan to do a full release within a month. I'd like to get some feedback, particularly from teachers, to help get the language of the interface right (function names and such), and look at some simple and complex examples to see if it's achieving its goals.

Install it on your Pi with:

Code: Select all

sudo apt-get update
sudo apt-get install python-pip python3-pip
sudo pip install gpiozero
sudo pip-3.2 install gpiozero
The library supports Python 3 and Python 2. Python 3 is recommended!

Just open IDLE or a Python shell, import the bits you're using and get going. Full documentation is available at http://pythonhosted.org/gpiozero/

Please feel free to leave feedback here or in the Google Doc: https://docs.google.com/document/d/1Ebb ... edit?pli=1#
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

scotty101
Posts: 3749
Joined: Fri Jun 08, 2012 6:03 pm

Re: GPIO Zero - a new Python library designed for education

Thu Oct 01, 2015 11:09 am

Not a teacher but I've been a STEM ambassador and have taught MindStorms to kids and developed an Arduino Scratch workshop.

Only comment so far, without running it, is on the following syntax

Code: Select all

is_active
I think I'd replace this with

Code: Select all

is_on
and

Code: Select all

is_off
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: GPIO Zero - a new Python library designed for education

Thu Oct 01, 2015 11:18 am

Good point. Thanks! Are you referring to generic output devices? We have domain-specific aliases for things like Button where "is_active" becomes "is_pressed" and "is_inactive" becomes "is_released". Would make sense for things like LEDs which have on/off state.
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: GPIO Zero - a new Python library designed for education

Sat Oct 10, 2015 4:19 pm

Beta 2 out now!
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

Mamvcivm
Posts: 8
Joined: Sun Mar 09, 2014 11:04 am

Re: GPIO Zero - a new Python library designed for education

Wed Nov 11, 2015 9:44 pm

Hi there, just created a new install of Jessie and ran the commands listed below:-

sudo apt-get update - succeeded
sudo apt-get install python-pip python3-pip - succeeded
sudo pip install gpiozero - failed
sudo pip-3.2 install gpiozero - succeeded

The error for the third line is:-

building 'spidev' extension

creating build

creating build/temp.linux-armv7l-2.7

arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c spidev_module.c -o build/temp.linux-armv7l-2.7/spidev_module.o

spidev_module.c:20:20: fatal error: Python.h: No such file or directory

#include <Python.h>

^

compilation terminated.

error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-KLl1oE/spidev/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-R_yDS6-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip-build-KLl1oE/spidev
Storing debug log for failure in /root/.pip/pip.log


I'm guessing this is only a problem if I chose to use Python 2, but any ideas as to what's gone wrong?

Thanks,

Andrew

DirkS
Posts: 10006
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: GPIO Zero - a new Python library designed for education

Thu Nov 12, 2015 7:02 am

spidev_module.c:20:20: fatal error: Python.h: No such file or directory
That file is part of the package python-dev.
Install it with

Code: Select all

sudo apt-get update
sudo apt-get install python-dev

Mamvcivm
Posts: 8
Joined: Sun Mar 09, 2014 11:04 am

Re: GPIO Zero - a new Python library designed for education

Thu Nov 12, 2015 9:08 pm

Cheers DirkS that sorted it! :D

Return to “Python”