Slide 1

Slide 1 text

Python and 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 ● Creator of gpiozero python library ● Columnist on opensource.com ● github.com/bennuttall ● twitter.com/ben_nuttall ● [email protected]

Slide 3

Slide 3 text

Over 11 million Raspberry Pis sold

Slide 4

Slide 4 text

Raspberry Pi Foundation ● Educational charity founded in 2009 ● Owns trading subsidiary Raspberry Pi Trading Ltd ● Trading profits fund educational programmes

Slide 5

Slide 5 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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Current models ● Raspberry Pi 3 ● 64-bit quad-core ARMv8 @ 1.2GHz ● 1GB RAM ● $35 ● Raspberry Pi Zero ● 32-bit single-core ARMv6 @ 1GHz ● 512MB RAM ● $5

Slide 8

Slide 8 text

Raspbian Jessie with PIXEL

Slide 9

Slide 9 text

PIXEL x86

Slide 10

Slide 10 text

GPIO Pins – General Purpose Input/Output

Slide 11

Slide 11 text

Physical computing ● Flashy lights ● Motors & robots ● Photo & video ● Sensors ● Internet of Things ● Engaging and empowering

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

GPIO components

Slide 14

Slide 14 text

Add-on boards / HATs

Slide 15

Slide 15 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 16

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

Slide 17 text

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

Slide 18

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

Slide 19 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 20

Slide 20 text

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

Slide 21

Slide 21 text

GPIO Zero supports...

Slide 22

Slide 22 text

GPIO Zero Device Hierarchy!

Slide 23

Slide 23 text

Supporting multiple back-ends ● RPi.GPIO ● Implemented in C, current default ● RPIO ● Implemented in C ● pigpio ● Python wrapper for C library, runs as daemon, remote pins ● Native ● Pure Python, limited functionality, experimental ● MockPin & MockPWMPin ● Pure Python, used in test suite

Slide 24

Slide 24 text

pigpio - remote GPIO from Pi or PC

Slide 25

Slide 25 text

pigpio - remote GPIO from Pi or PC from gpiozero import LED from gpiozero.pins.pigpiod import PiGPIOPin led = LED(PiGPIOPin(22, host='192.168.0.2')) led.blink()

Slide 26

Slide 26 text

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

Slide 27

Slide 27 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

Slide 28

Slide 28 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 29

Slide 29 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 30

Slide 30 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 31

Slide 31 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: %s" % effect sleep(5) camera.stop_preview()

Slide 32

Slide 32 text

Picamera image effects

Slide 33

Slide 33 text

Web Streaming

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

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

Slide 37

Slide 37 text

Sense HAT

Slide 38

Slide 38 text

Sense HAT >>> from sense_hat import SenseHat >>> sense = SenseHat() >>> sense.show_message(“Hello world”) >>> sense.temperature >>> sense.humidity >>> sense.accelerometer >>> sense.gyroscope >>> sense.orientation

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Astro Pi: Your code in space

Slide 41

Slide 41 text

Sense HAT Web Emulator

Slide 42

Slide 42 text

Sense HAT Desktop Emulator

Slide 43

Slide 43 text

Read the docs! ● gpiozero.readthedocs.io ● picamera.readthedocs.io ● pythonhosted.org/sense-hat

Slide 44

Slide 44 text

How you can get involved...

Slide 45

Slide 45 text

Python libraries ● Contribute to: – gpiozero – picamera – sense hat – more! ● Help maintainers upgrade modules to Python 3 ● Create your own Python libraries for Raspberry Pi things

Slide 46

Slide 46 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 ● Book series (also available for free) ● raspberrypi.org/magpi

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Python and Raspberry Pi Ben Nuttall Raspberry Pi Foundation UK Charity 1129409