Physical computing with Python and Raspberry Pi - PyCon Russia
An introduction to the Raspberry Pi, and the Foundation's charitable work in education, and to getting started with physical computing using Python and GPIO Zero on the Raspberry Pi.
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
Raspberry Pi Foundation ● Educational charity founded in 2006 ● Owns trading subsidiary Raspberry Pi Trading Ltd ● Trading profits fund educational programmes
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
Raspberry Pi computers ● We produce low-cost high- power computers ● They keep getting better and/or cheaper :) ● Used in – education – hobby projects – industry
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
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...
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
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)
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
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)
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
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)
Source Tools from gpiozero import PWMLED, MCP3008 from gpiozero.tools import inverted led = PWMLED(4) pot = MCP3008(channel=0) led.source = inverted(pot.values)
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()
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
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)
Raspberry Pi Learning Resources ● Teach, Learn and Make ● Free ● Open source ● Creative Commons ● Created by Raspberry Pi Education Team ● Scratch, Python and more
Raspberry Pi Weekly ● Free weekly email newsletter ● Raspberry Pi news, projects and articles ● 3 years of issues on the website ● raspberrypi.org/weekly
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)
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
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]