Physical Computing with Python
Ben Nuttall
Raspberry Pi Foundation
UK Charity 1129409
Slide 2
Slide 2 text
Ben Nuttall
●
Raspberry Pi Community Manager
– Programmes & outreach
– Software & project development
– Learning resources & teacher training
●
Hobbyist turned employee
●
Based in Cambridge, UK
●
@ben_nuttall on Twitter
Slide 3
Slide 3 text
Raspberry Pi
Slide 4
Slide 4 text
Raspberry Pi Foundation
●
Educational charity founded
in 2006
●
Owns trading subsidiary
Raspberry Pi Trading Ltd
●
Trading profits fund
educational programmes
Slide 5
Slide 5 text
A Computer (1981)
Slide 6
Slide 6 text
A Computer (2010)
Slide 7
Slide 7 text
Prototypes
Slide 8
Slide 8 text
Raspberry Pi (2012)
Raspberry Pi Model B
700MHz single core 32-bit CPU
VideoCoreIV 3D GPU
256MB RAM
$35
Slide 9
Slide 9 text
Raspberry Pi Community
Slide 10
Slide 10 text
Raspberry Jam
Slide 11
Slide 11 text
Raspberry Jam
raspberrypi.org/jam
Slide 12
Slide 12 text
Made in the UK
●
Since 2013, Raspberry Pi has
been manufactured by Sony
in Wales
●
20k units per day at peak
Slide 13
Slide 13 text
Raspberry Pi Compute Module
Prototyping development board
Build your product around the
Raspberry Pi
Slide 14
Slide 14 text
Raspberry Pi Zero (2015)
1GHz single core 32-bit CPU
VideoCoreIV 3D GPU
512MB RAM
$5
Slide 15
Slide 15 text
Raspberry Pi 3 Model B (2016)
1.2GHz quad core 64-bit CPU
VideoCoreIV 3D GPU
1GB RAM
$35
Slide 16
Slide 16 text
Over 9 Million Raspberry Pis sold
●
Minor revisions
●
Model A
●
Model B+
●
Model A+
●
Raspberry Pi 2 Model B
●
Raspberry Pi Zero
●
Raspberry Pi 3 Model B
Slide 17
Slide 17 text
Raspberry Pi Foundation Mission
“Putting the power of digital making into the hands of people all
over the world”
Slide 18
Slide 18 text
Raspberry Pi computers
●
We produce low-cost high-
power computers
●
They keep getting better
and/or cheaper :)
●
Used in
– education
– hobby projects
– industry
Slide 19
Slide 19 text
Raspberry Pi in Schools
Slide 20
Slide 20 text
Education
●
We train teachers in
computing
●
We create free resources for
use at home and school
●
We run programmes to
engage young people in
digital making
●
We support a network of Code
Clubs in Primary Schools
Slide 21
Slide 21 text
Raspbian
Slide 22
Slide 22 text
Raspbian
●
Launch programming
applications
●
Web browser
●
Preferences and settings
●
Shutdown & reboot
●
Look – it's a real computer!
Slide 23
Slide 23 text
Physical computing
●
Flashy lights
●
Motors & robots
●
Photo & video
●
Sensors
●
Internet of Things
●
Engaging and empowering
Slide 24
Slide 24 text
GPIO Pins – General Purpose Input/Output
Slide 25
Slide 25 text
GPIO components
Slide 26
Slide 26 text
Add-on boards / HATs
Slide 27
Slide 27 text
Python - RPi.GPIO
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT)
while True:
GPIO.output(17, GPIO.HIGH)
sleep(1)
GPIO.output(17, GPIO.LOW)
sleep(1)
Slide 28
Slide 28 text
GPIO in Scratch
●
GPIO Server
●
Use broadcast blocks
●
Drag & drop programming
– except when you have to
write code in those broadcast
blocks and get the syntax
right as there's no error
reporting...
Slide 29
Slide 29 text
Something more approachable?
●
Teachers saying Python is too hard to teach with
– Particularly RPi.GPIO and PyGame
●
Teachers staying in Scratch too long
– Need help moving to Python
– GPIO in Scratch is difficult
●
Problems with RPi.GPIO
– Too much boilerplate code required for even simple examples
– Lots of copy/paste for complex components
– Not Pythonic
Slide 30
Slide 30 text
Python - GPIO Zero
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
Slide 31
Slide 31 text
Python - GPIO Zero
from gpiozero import LED
led = LED(17)
led.blink()
Slide 32
Slide 32 text
LED
from gpiozero import LED
led = LED(17)
led.on() # on
led.off() # off
led.toggle() # on>off or off>on
led.blink() # flash on/off continuously
Slide 33
Slide 33 text
Buzzer
from gpiozero import Buzzer
buzzer = Buzzer(18)
buzzer.on() # on
buzzer.off() # off
buzzer.toggle() # on>off or off>on
buzzer.beep() # beep on/off continuously
Slide 34
Slide 34 text
GitHub
Slide 35
Slide 35 text
Button polling (pull-up) – RPi.GPIO
GPIO.setup(4, GPIO.IN, GPIO.PUD_UP)
while True:
if not GPIO.input(4):
print(“Pressed”)
Slide 36
Slide 36 text
Button polling (pull-down) – RPi.GPIO
GPIO.setup(4, GPIO.IN, GPIO.PUD_DOWN)
while True:
if GPIO.input(4):
print(“Pressed”)
Slide 37
Slide 37 text
Button polling (pull-up) – GPIO Zero
button = Button(4)
while True:
if button.is_pressed:
print(“Pressed”)
Slide 38
Slide 38 text
Button polling (pull-down) – GPIO Zero
button = Button(4, pull_up=False)
while True:
if button.is_pressed:
print(“Pressed”)
Button + LED
from gpiozero import LED, Button
led = LED(17)
button = Button(4)
button.when_pressed = led.on
button.when_released = led.off
Slide 48
Slide 48 text
LED – PWM – RPi.GPIO
GPIO.setup(17, GPIO.OUT) # pin 17
p = GPIO.PWM(17, 100) # pin 17, frequency 100Hz
p.start(0) # initial duty cycle
for i in range(101):
p.ChangeDutyCycle(i)
sleep(0.01)
Slide 49
Slide 49 text
LED – PWM – GPIO Zero
from gpiozero import PWMLED
led = PWMLED(17)
led.on() # on
led.off() # off
led.value = 0.5 # half brightness
led.pulse() # blink with fade in/out
Slide 50
Slide 50 text
Full colour LED
from gpiozero import RGBLED
led = RGBLED(red=2, green=3, blue=4)
led.red.on() # full red
led.color = (1, 0, 1) # purple
led.blue = 0.3 # dim the blue value to 0.3
# now (1, 0, 0.3)
Slide 51
Slide 51 text
Traffic Lights
from gpiozero import TrafficLights
lights = TrafficLights(9, 10, 11)
lights.on() # all on
lights.off() # all off
lights.red.on() # red on
lights.toggle() # swap state of all lights
Motion sensor
from gpiozero import LED, MotionSensor
led = LED(2)
sensor = MotionSensor(3)
sensor.when_motion = led.on
sensor.when_no_motion = led.off
Slide 57
Slide 57 text
Light sensor
from gpiozero import LED, LightSensor
led = LED(2)
sensor = LightSensor(3)
while True:
sensor.wait_for_light()
print("It's light!")
sensor.wait_for_dark()
print("It's dark")
Slide 58
Slide 58 text
Motor
from gpiozero import Motor
from time import sleep
motor = Motor(forward=17, backward=18)
while True:
motor.forward()
sleep(5)
motor.backward()
sleep(5)
Slide 59
Slide 59 text
Robot
from gpiozero import Robot
from time import sleep
robot = Robot(left=(17, 18), right=(22, 23))
while True:
robot.forward()
sleep(10)
robot.left()
sleep(1)
Source Tools
from gpiozero import PWMLED, MCP3008
from gpiozero.tools import inverted
led = PWMLED(4)
pot = MCP3008(channel=0)
led.source = inverted(pot.values)
Slide 82
Slide 82 text
LEDBoard
from gpiozero import LEDBoard
from time import sleep
leds = LEDBoard(2, 3, 4, 5, 6)
for led in leds:
led.on()
sleep(1)
leds.off()
pigpio - remote GPIO from Pi or PC
from gpiozero import LED
from gpiozero.pins.pigpiod import PiGPIOPin
led = LED(PiGPIOPin(12, host='192.168.0.2'))
led.blink()
Slide 87
Slide 87 text
GPIO Zero Timeline
●
12 Sept – Idea sparked
●
14 Sept – Initial commit on GitHub
●
15 Sept – Named GPIO Zero, first PR, first alpha released on PyPI
●
28 Sept – v0.6 public beta 1
●
25 Oct – v0.9 public beta 4
●
16 Nov – v1.0 released
●
21 Nov – Released in Raspbian Jessie
●
8 Feb – v1.1 released
●
10 Apr – v1.2 released
Slide 88
Slide 88 text
v1.0
●
200 commits
●
2 contributors (+4 minor contributions)
●
103 GitHub issues (53 issues, 50 PRs)
●
4 alpha releases
●
4 beta releases
●
68 days between initial commit and major release
Slide 89
Slide 89 text
v1.3 (a work in progress)
●
522 commits
●
5 contributors (+8 minor contributions)
●
390 GitHub issues (178 issues, 212 PRs)
Slide 90
Slide 90 text
Future development
●
Add more components
●
Probably move default from RPi.GPIO to pigpio
●
Better remote GPIO support
●
Promote use of “gpiozero standard” to allow other modules to
provide objects which plug-in to gpiozero objects easily (e.g.
source/values)
Slide 91
Slide 91 text
Install GPIO Zero
●
Pre-installed in Raspbian Jessie since November
●
Update with:
sudo aptget update
●
Install with:
sudo aptget install python3gpiozero
or:
sudo aptget install pythongpiozero
Slide 92
Slide 92 text
GPIO Zero
●
gpiozero.readthedocs.io
– Installation instructions
– API Documentation
– Examples
●
GitHub Issues
– Suggestions
– Feedback
●
Contact me
– [email protected]
– @ben_nuttall on Twitter
●
#gpiozero on Twitter
Slide 93
Slide 93 text
Zero all the things!
●
PyGame Zero
●
GPIO Zero
●
Network Zero
●
BlueZero
Slide 94
Slide 94 text
How can you get involved?
Slide 95
Slide 95 text
www.raspberrypi.org
●
Daily blog articles – news,
projects and stories
●
OS Downloads
●
Help pages
●
Documentation
●
Community sites
●
Forums
Slide 96
Slide 96 text
Raspberry Pi Learning Resources
●
Teach, Learn and Make
●
Free
●
Open source
●
Creative Commons
●
Created by Raspberry Pi
Education Team
●
Scratch, Python and more
Slide 97
Slide 97 text
Raspberry Pi Weekly
●
Free weekly email newsletter
●
Raspberry Pi news, projects
and articles
●
3 years of issues on the
website
●
raspberrypi.org/weekly
Slide 98
Slide 98 text
The MagPi
●
Community magazine established
in 2012 (as free PDF download)
●
Now the official Raspberry Pi
magazine
●
Paper copies on sale in UK shops
and online
●
Still a free PDF download
●
Occasionally comes with a free
computer
●
Book series (also available for free)
Slide 99
Slide 99 text
Raspberry Jam in Russia
●
Start a Raspberry Jam in your
area
●
Create opportunities for kids
to learn programming
●
Share skills and projects
●
See www.raspberrypi.org/jam
●
Contact me for support
Slide 100
Slide 100 text
Code Club in Russia
●
Translate our projects
●
Run a club for 9-11 year olds
●
Become a country
coordinator
●
See www.codeclubworld.org
●
Contact me or
[email protected]