Slide 1

Slide 1 text

Physical computing with Raspberry Pi Ben Nuttall Raspberry Pi Foundation UK Charity 1129409

Slide 2

Slide 2 text

Ben Nuttall ● Raspberry Pi Community Manager ● Based in Cambridge, UK ● Columnist on opensource.com ● @ben_nuttall on Twitter

Slide 3

Slide 3 text

Over 10 million Raspberry Pis sold

Slide 4

Slide 4 text

Over 15 million Raspberry Pis sold

Slide 5

Slide 5 text

Raspberry Pi Foundation ● Educational charity founded in 2009 ● Incorporates: – Raspberry Pi Trading Ltd – Code Club – CoderDojo – Raspberry Pi Foundation North America ● Trading profits fund education programmes raspberrypi.org/about

Slide 6

Slide 6 text

Our mission “Putting the power of digital making into the hands of people all over the world” So that people are: ● Capable of understanding and shaping an increasingly digital world ● Able to solve the problems that matter to them, both as makers and entrepreneurs ● Equipped for the jobs of the future raspberrypi.org/about

Slide 7

Slide 7 text

We do this by providing... ● Low-cost, high-performance computers ● Outreach and education programmes ● Free resources and teacher training p

Slide 8

Slide 8 text

Current models ● Raspberry Pi 3 ● 64-bit quad-core ARMv8 @ 1.2GHz ● 1GB RAM ● $35 ● Raspberry Pi Zero / Zero W ● 32-bit single-core ARMv6 @ 1GHz ● 512MB RAM ● $5 / $10 raspberrypi.org/products

Slide 9

Slide 9 text

Raspbian desktop raspberrypi.org/downloads

Slide 10

Slide 10 text

Raspbian x86 rpf.io/x86

Slide 11

Slide 11 text

GPIO Pins – General Purpose Input/Output

Slide 12

Slide 12 text

Physical computing ● Flashy lights ● Motors & robots ● Photo & video ● Sensors ● Internet of Things ● Home automation

Slide 13

Slide 13 text

GPIO ● 3V3, 5V ● GPIO, SPI, I2C, UART ● GPIO = variable 3V3 pinout.xyz

Slide 14

Slide 14 text

GPIO components

Slide 15

Slide 15 text

Add-on boards / HATs

Slide 16

Slide 16 text

GPIO with Scratch

Slide 17

Slide 17 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 18

Slide 18 text

Python - GPIO Zero from gpiozero import LED led = LED(17) led.blink()

Slide 19

Slide 19 text

GPIO Zero supports...

Slide 20

Slide 20 text

GPIO Zero Device Hierarchy!

Slide 21

Slide 21 text

Multi-paradigm: procedural from gpiozero import LED, Button led = LED(17) button = Button(4) while True: if button.is_pressed: led.on() else: led.off()

Slide 22

Slide 22 text

Multi-paradigm: event-driven from gpiozero import LED, Button led = LED(17) button = Button(4) button.when_pressed = led.on button.when_released = led.off

Slide 23

Slide 23 text

Multi-paradigm: declarative from gpiozero import LED, Button led = LED(17) button = Button(4) led.source = button.values

Slide 24

Slide 24 text

.value >>> led = PWMLED(17) >>> led.value 0.0 >>> led.on() >>> led.value 1.0 >>> led.value = 0

Slide 25

Slide 25 text

.value >>> led = PWMLED(17) >>> pot = MCP3008() >>> led.value = pot.value

Slide 26

Slide 26 text

.value >>> led = PWMLED(17) >>> pot = MCP3008() >>> led.value = pot.value >>> while True: ... led.value = pot.value

Slide 27

Slide 27 text

Source / Values Output Device .value .values .source Input Device .value .values

Slide 28

Slide 28 text

Source / Values Output Device .value .values .source Input Device .value .values

Slide 29

Slide 29 text

Source / Values from gpiozero import LED, Button led = LED(17) button = Button(2) led.source = button.values

Slide 30

Slide 30 text

Processing values Output Device .value .values .source Input Device .value .values function

Slide 31

Slide 31 text

Source tools from gpiozero import Button, LED from gpiozero.tools import negated led = LED(4) button = Button(17) led.source = negated(button.values)

Slide 32

Slide 32 text

Combining values Output Device .value .values .source Input Device .value .values Source tool Input Device .value .values

Slide 33

Slide 33 text

AND logic gate from gpiozero import Button, LED from gpiozero.tools import all_values button_a = Button(2) button_b = Button(3) led = LED(17) led.source = all_values(button_a.values, button_b.values)

Slide 34

Slide 34 text

Energenie

Slide 35

Slide 35 text

Energenie tortoise lamp from gpiozero import Energenie, TimeOfDay from datetime import time lamp = Energenie(1) daytime = TimeOfDay(time(8), time(20)) lamp.source = daytime.values

Slide 36

Slide 36 text

Supporting multiple pin libraries ● RPi.GPIO ● Implemented in C, current default ● Available in PyPI & apt repository (installed by default in Raspbian) ● RPIO ● Implemented in C, supports hardware PWM, supports Pi 1 only (dead project) ● Available in PyPI ● pigpio ● Python wrapper for C library, supports lots of protocols, runs as daemon, supports remote connections ● Available in PyPI & apt repository (installed by default in Raspbian) ● Native ● Pure Python, limited functionality, experimental ● Included in gpiozero source ● MockPin & MockPWMPin ● Pure Python, used in test suite ● Included in gpiozero source

Slide 37

Slide 37 text

pigpio - remote GPIO from Pi or PC

Slide 38

Slide 38 text

pigpio - remote GPIO from Pi or PC from gpiozero import LED from gpiozero.pins.pigpio import PiGPIOFactory from signal import pause factory = PiGPIOFactory('192.168.0.2') led = LED(22, pin_factory=factory) led.blink() pause()

Slide 39

Slide 39 text

pigpio - remote GPIO from Pi or PC $ export GPIOZERO_PIN_FACTORY=pigpio $ export PIGPIO_ADDR=192.168.0.2 $ python3 led.py from gpiozero import LED from signal import pause led = LED(22) led.blink() pause()

Slide 40

Slide 40 text

MockPin $ GPIOZERO_PIN_FACTORY=mock python3 >>> from gpiozero import LED >>> led = LED(22) >>> led.blink() >>> led.value True >>> led.value False

Slide 41

Slide 41 text

MockPin >>> from gpiozero import LED >>> led = LED(22) >>> button = Button(23) >>> led.source = button.values >>> led.value False >>> button.pin.drive_low() >>> led.value True

Slide 42

Slide 42 text

pinout command line tool

Slide 43

Slide 43 text

Sense HAT ● Special made for Tim Peake's Astro Pi mission ● Sensors, LED display & joystick ● Great for science, games and creativity ● Works on any Pi model ● Emulators also available raspberrypi.org/products/sense-hat

Slide 44

Slide 44 text

Sense HAT

Slide 45

Slide 45 text

Sense HAT >>> from sense_hat import SenseHat >>> sense = SenseHat() >>> sense.show_message(“Hello world”)

Slide 46

Slide 46 text

Sense HAT >>> sense.temperature 25.0 >>> sense.humidity 45.0 >>> sense.accelerometer {'pitch': 0.0, 'roll': 0.0, 'yaw': 0.0} >>> sense.gyroscope {'pitch': 0.0, 'roll': 0.0, 'yaw': 0.0} >>> sense.orientation {'pitch': 0.0, 'roll': 0.0, 'yaw': 0.0}

Slide 47

Slide 47 text

Sense HAT from sense_hat import SenseHat sense = SenseHat() while True: r = 255 * sense.humidity / 100 sense.clear(r, 0, 0) pythonhosted.org/sense-hat

Slide 48

Slide 48 text

Astro Pi: Your code in space astro-pi.org

Slide 49

Slide 49 text

Sense HAT Web Emulator trinket.io/sense-hat

Slide 50

Slide 50 text

Sense HAT Desktop Emulator sense-emu.readthedocs.io

Slide 51

Slide 51 text

Picamera ● 8 megapixels (v2) – v1 was 5mpx ● Visible light & infra-red versions available ● 1080p30, 720p60 and VGA90 video ● Command line interface and Python library raspberrypi.org/products/camera-module-v2

Slide 52

Slide 52 text

Picamera - capture from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(3) camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview()

Slide 53

Slide 53 text

Picamera – record video from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() camera.start_recording('/home/pi/video.h264') sleep(10) camera.stop_recording() camera.stop_preview()

Slide 54

Slide 54 text

Picamera + GPIO push button from picamera import PiCamera from gpiozero import Button camera = PiCamera() button = Button(17) camera.start_preview() button.wait_for_press() camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview()

Slide 55

Slide 55 text

Picamera image effects from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() for effect in camera.IMAGE_EFFECTS: camera.image_effect = effect camera.annotate_text = effect sleep(1) camera.stop_preview()

Slide 56

Slide 56 text

Picamera image effects

Slide 57

Slide 57 text

Web Streaming github.com/waveform80/pistreaming

Slide 58

Slide 58 text

Picamera + OpenCV pyimagesearch.com

Slide 59

Slide 59 text

Google AIY Projects kit ● Free with The MagPi #57 ● Now available to buy ● Google Voice HAT + speaker ● Google assistant ● Write code to process custom voice commands – “Lights on” – “Robot go forward” – “Take a picture” aiyprojects.withgoogle.com

Slide 60

Slide 60 text

Mythic Beasts Pi Cloud ● Raspberry Pi 3 in the cloud a data centre ● NFS filesystem (no SD card) ● SSH into a Pi in minutes mythic-beasts.com

Slide 61

Slide 61 text

The MagPi ● Community magazine established in 2012 (as free PDF download) ● Now the official Raspberry Pi magazine ● Paper copies on sale in UK/US shops and online ● Still a free PDF download ● Occasionally comes with a free computer or other giveaway ● Book series (buy or download for free) raspberrypi.org/magpi

Slide 62

Slide 62 text

How you can get involved...

Slide 63

Slide 63 text

Raspberry Jam ● Independently organised community events ● Family-friendly ● Mix of meetup / conference / workshop styles ● Raspberry Jam Guidebook and more resources available ● Contact me about setting one up! raspberrypi.org/jam

Slide 64

Slide 64 text

Raspberry Jam near you? raspberrypi.org/jam

Slide 65

Slide 65 text

Raspberry Jam big birthday weekend rpf.io/bday

Slide 66

Slide 66 text

Code Club ● Free volunteer-led after school clubs for children aged 9-13 ● Projects provided using Scratch, HTML and Python ● Training and support provided for volunteers ● Help translating materials codeclubworld.org

Slide 67

Slide 67 text

CoderDojo ● Free volunteer-led youth clubs for under-18s ● Informal, lots of variety ● Resources online coderdojo.com

Slide 68

Slide 68 text

Raspberry Pi booth ● Booth #41 ● Behind the registration desk ● Come say hi! ● Ask questions ● Tell me about your projects ● Find a Raspberry Jam near you – or help set one up

Slide 69

Slide 69 text

Physical computing with Raspberry Pi Ben Nuttall Raspberry Pi Foundation UK Charity 1129409