Slide 1

Slide 1 text

Physical computing with Python and Raspberry Pi Ben Nuttall Raspberry Pi Foundation

Slide 2

Slide 2 text

Ben Nuttall ● Education Developer Advocate at Raspberry Pi Foundation – Software & project development – Learning resources & teacher training – Outreach ● Based in Cambridge, UK ● @ben_nuttall on Twitter ● Speaker at EuroPython, PyConUK, PySS, PyCon Ireland & EuroSciPy in 2014, EuroPython in 2015

Slide 3

Slide 3 text

Raspberry Pi 2 ● 900MHz quad core ARM7 ● 1GB RAM ● Backwards-compatible with Pi 1 ● Same form factor as B+ ● Still $35 (B+ now $25, A+ $20)

Slide 4

Slide 4 text

All about Education ● Raspberry Pi Foundation, registered UK charity 1129409 ● Founded in 2009 to aid computing education ● On general sale since 2012, available to all worldwide – Sold to education, industry and hobbyists – Sold 6 million to date ● Free learning resources for makers and educators ● Free teacher training - Picademy (currently UK, soon USA)

Slide 5

Slide 5 text

Ben Croston – beer brewing

Slide 6

Slide 6 text

Dave Jones - microscopy picamera.readthedocs.org

Slide 7

Slide 7 text

Raspbian ● Foundation-issued Debian-based distribution – Currently based on Wheezy – Jessie image coming soon ● Image supports Pi 1 and Pi 2 ● Software pre-installed – Python 3 (also Python 2), Ruby, Java, Mathematica, etc. – GPIO, Picamera, (soon pip too) ● Alternative distributions are available, but not supported, e.g: – Ubuntu MATE (Pi 2) – Ubuntu Snappy Core (Pi 2) – Arch Linux www.raspberrypi.org/downloads

Slide 8

Slide 8 text

GPIO Pins – General Purpose Input/Output rasp.io

Slide 9

Slide 9 text

Analogue? ● No native analogue on Raspberry Pi ● Options: – Analogue inputs can be read via ADC – Various Arduino-compatible add-on boards available – Use PySerial to read Arduino inputs over USB

Slide 10

Slide 10 text

Python library - RPi.GPIO ● Included in Raspbian ● Implemented in C ● Features: – Configure pins as input/output – Read inputs (high/low) – Set outputs (high low) – Wait for edge (wait for input to go high/low) – Pin event detection (callback on input pin change)

Slide 11

Slide 11 text

3V3 = always on

Slide 12

Slide 12 text

GPIO = user controllable

Slide 13

Slide 13 text

Flash LED with RPi.GPIO from RPi import GPIO from time import sleep GPIO.setmode(GPIO.BCM) led = 2 GPIO.setup(led, GPIO.OUT) while True: GPIO.output(led, True) sleep(1) GPIO.output(led, False) sleep(1)

Slide 14

Slide 14 text

Push button stop motion

Slide 15

Slide 15 text

Push button stop motion from picamera import PiCamera from RPi import GPIO from time import sleep button = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(button, GPIO.IN, GPIO.PUD_UP)

Slide 16

Slide 16 text

Push button stop motion with PiCamera() as camera: camera.start_preview() frame = 1 while True: GPIO.wait_for_edge(button, GPIO.FALLING) camera.capture('/home/pi/animation/frame%03d.jpg' % frame) frame += 1 camera.stop_preview()

Slide 17

Slide 17 text

GPIO Music Box

Slide 18

Slide 18 text

GPIO Music Box – GPIO events GPIO.add_event_detect( button, GPIO.FALLING, callback=play, bouncetime=1000 ) www.raspberrypi.org/learning/gpio-music-box/

Slide 19

Slide 19 text

GPIO Music Box – GPIO events sound_pins = { 2: drum, 3: cymbal, } def play(pin): sound = sound_pins[pin] sound.play() www.raspberrypi.org/learning/gpio-music-box/

Slide 20

Slide 20 text

CamJam EduKit camjam.me/edukit

Slide 21

Slide 21 text

The Gertboard

Slide 22

Slide 22 text

Ryanteck RPi Motor Controller Board

Slide 23

Slide 23 text

Pimoroni - Pibrella

Slide 24

Slide 24 text

Pibrella – traffic lights import pibrella from time import sleep pibrella.light.green.on() sleep(1) pibrella.light.amber.on() sleep(1) pibrella.light.red.on()

Slide 25

Slide 25 text

Pibrella – button press event def flash(pin): pibrella.light.on() sleep(1) pibrella.light.off() pibrella.button.pressed(flash)

Slide 26

Slide 26 text

Energenie – remote controlled power sockets

Slide 27

Slide 27 text

Energenie – remote controlled power sockets import energenie from time import sleep energenie.switch_on() sleep(5) energenie.switch_off() pythonhosted.org/energenie

Slide 28

Slide 28 text

Energenie – web app pythonhosted.org/energenie /examples/web/

Slide 29

Slide 29 text

26->40 pin header

Slide 30

Slide 30 text

Raspberry Pi HATs – Hardware Attached on Top

Slide 31

Slide 31 text

Sous Vide cooking

Slide 32

Slide 32 text

Chef HAT WIP – github.com/bennuttall/chef-hat pypi.python.org/pypi/chef-hat

Slide 33

Slide 33 text

Chef HAT – temperature moderation if self.temperature < self.target_temperature: self.turn_cooker_on() else: self.turn_cooker_off()

Slide 34

Slide 34 text

Pimoroni - HATs http://shop.pimoroni.com/collections/hats

Slide 35

Slide 35 text

Astro Pi

Slide 36

Slide 36 text

Astro Pi

Slide 37

Slide 37 text

Sense HAT ● 8x8 RGB LED matrix ● Temperature ● Humidity ● Pressure ● Accelerometer ● Gyroscope ● Magnetometer ● Mini joystick pypi.python.org/pypi/sense-hat

Slide 38

Slide 38 text

GPIO import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) led = 2 GPIO.setup(led, GPIO.OUT) GPIO.output(led, True)

Slide 39

Slide 39 text

GPIO Zero from gpiozero import LED led = LED(2) led.on()

Slide 40

Slide 40 text

GPIO Zero from gpiozero import LED, Button from time import sleep def hello(pin): led.on() sleep(1) led.off() led = LED(2) button = Button(3) button.when_pressed(hello)

Slide 41

Slide 41 text

Join Raspberry Pi at the Sprints! ● GPIO Zero ● PyGame Zero ● Port Pi in the Sky software to Python ● Puppy editor

Slide 42

Slide 42 text

Thank you - any questions?