physical computing with python and raspberry pi
TRANSCRIPT
![Page 1: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/1.jpg)
Physical computingwith Python and Raspberry Pi
Ben NuttallRaspberry Pi Foundation
![Page 2: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/2.jpg)
![Page 3: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/3.jpg)
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
![Page 4: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/4.jpg)
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)
![Page 5: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/5.jpg)
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)
![Page 6: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/6.jpg)
Ben Croston – beer brewing
![Page 7: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/7.jpg)
Dave Jones - microscopy
picamera.readthedocs.org
![Page 8: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/8.jpg)
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
![Page 9: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/9.jpg)
GPIO Pins – General Purpose Input/Output
rasp.io
![Page 10: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/10.jpg)
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
![Page 11: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/11.jpg)
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)
![Page 12: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/12.jpg)
3V3 = always on
![Page 13: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/13.jpg)
GPIO = user controllable
![Page 14: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/14.jpg)
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)
![Page 15: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/15.jpg)
Push button stop motion
![Page 16: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/16.jpg)
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)
![Page 17: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/17.jpg)
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()
![Page 18: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/18.jpg)
GPIO Music Box
![Page 19: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/19.jpg)
GPIO Music Box – GPIO events
GPIO.add_event_detect(
button,
GPIO.FALLING,
callback=play,
bouncetime=1000
)
www.raspberrypi.org/learning/gpio-music-box/
![Page 20: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/20.jpg)
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/
![Page 21: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/21.jpg)
CamJam EduKit
camjam.me/edukit
![Page 22: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/22.jpg)
The Gertboard
![Page 23: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/23.jpg)
Ryanteck RPi Motor Controller Board
![Page 24: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/24.jpg)
Pimoroni - Pibrella
![Page 25: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/25.jpg)
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()
![Page 26: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/26.jpg)
Pibrella – button press event
def flash(pin):
pibrella.light.on()
sleep(1)
pibrella.light.off()
pibrella.button.pressed(flash)
![Page 27: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/27.jpg)
Energenie – remote controlled power sockets
![Page 28: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/28.jpg)
Energenie – remote controlled power sockets
import energenie
from time import sleep
energenie.switch_on()
sleep(5)
energenie.switch_off()
pythonhosted.org/energenie
![Page 29: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/29.jpg)
Energenie – web app
pythonhosted.org/energenie/examples/web/
![Page 30: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/30.jpg)
26->40 pin header
![Page 31: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/31.jpg)
Raspberry Pi HATs – Hardware Attached on Top
![Page 32: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/32.jpg)
Sous Vide cooking
![Page 33: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/33.jpg)
Chef HAT
WIP – github.com/bennuttall/chef-hatpypi.python.org/pypi/chef-hat
![Page 34: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/34.jpg)
Chef HAT – temperature moderation
if self.temperature < self.target_temperature:
self.turn_cooker_on()
else:
self.turn_cooker_off()
![Page 35: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/35.jpg)
DOTS board
www.raspberrypi.org/dots
![Page 36: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/36.jpg)
DOTS board software
def get_selected_colors():
return [COLOR_PINS[pin] for pin in COLOR_PINS if pin_is_active(pin)]
def enough_dots_connected():
active_pins = sum(pin_is_active(pin) for pin in DOT_PINS)
return active_pins > MINIMUM_DOTS_REQUIRED
def pin_is_active(pin):
GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
state = GPIO.input(pin)
GPIO.setup(pin, GPIO.IN, GPIO.PUD_OFF)
return state == 0
pypi.python.org/pypi/rpi-dots
![Page 37: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/37.jpg)
Plantpot Greenhouse
![Page 38: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/38.jpg)
Capacitive Touch HAT
![Page 39: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/39.jpg)
Pimoroni - HATs
http://shop.pimoroni.com/collections/hats
![Page 40: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/40.jpg)
Weather Station kit
![Page 41: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/41.jpg)
Astro Pi
![Page 42: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/42.jpg)
Astro Pi / Sense HAT
● 8x8 RGB LED matrix● Temperature● Humidity● Pressure● Accelerometer● Gyroscope● Magnetometer● Mini joystick
pypi.python.org/pypi/astro-pi
![Page 43: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/43.jpg)
![Page 44: Physical computing with Python and Raspberry Pi](https://reader033.vdocuments.site/reader033/viewer/2022042511/55c9a0d7bb61eba8678b4769/html5/thumbnails/44.jpg)
Thank you - any questions?