Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Physical Computing with Python and Raspberry Pi - campug

Physical Computing with Python and Raspberry Pi - campug

Physical Computing with Python and Raspberry Pi - session delivered at the Cambridge Python user group (campug) in January 2017

Ben Nuttall

January 10, 2017
Tweet

More Decks by Ben Nuttall

Other Decks in Programming

Transcript

  1. Physical Computing with Python
    and Raspberry Pi
    Ben Nuttall
    Raspberry Pi Foundation
    UK Charity 1129409

    View Slide

  2. Ben Nuttall

    Raspberry Pi Community Manager

    Creator of gpiozero python library

    github.com/bennuttall

    @ben_nuttall on Twitter

    View Slide

  3. 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

    View Slide

  4. Raspbian Jessie with PIXEL

    View Slide

  5. GPIO Pins – General Purpose Input/Output

    View Slide

  6. Physical computing

    Flashy lights

    Motors & robots

    Photo & video

    Sensors

    Internet of Things

    Engaging and empowering

    View Slide

  7. GPIO

    3V3, 5V

    GPIO, SPI, I2C, UART

    GPIO = variable 3V3

    View Slide

  8. GPIO components

    View Slide

  9. Add-on boards / HATs

    View Slide

  10. 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)

    View Slide

  11. Python - GPIO Zero
    from gpiozero import LED
    from time import sleep
    led = LED(17)
    while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

    View Slide

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

    View Slide

  13. 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()

    View Slide

  14. 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

    View Slide

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

    View Slide

  16. GPIO Zero supports...

    View Slide

  17. GPIO Zero Device Hierarchy!

    View Slide

  18. Dial up the brightness!
    from gpiozero import PWMLED, MCP3008
    led = PWMLED(2)
    pot = MCP3008()
    while True:
    led.value = pot.value

    View Slide

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

    View Slide

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

    View Slide

  21. Dial up the brightness!
    from gpiozero import PWMLED, MCP3008
    led = PWMLED(2)
    pot = MCP3008()
    led.source = pot.values

    View Slide

  22. Source / Values
    Output Device
    .value
    .values
    .source
    Custom generator

    View Slide

  23. Source / Values
    Output Device
    .value
    .values
    .source
    Iterator

    View Slide

  24. Custom value generators
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values
    Custom generator

    View Slide

  25. Source Tools
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values
    Source tool

    View Slide

  26. Source Tools
    from gpiozero import LED, Button
    from gpiozero.tools import all_values
    led = PWMLED(4)
    btn_a = Button(2)
    btn_b = Button(3)
    led.source = all_values(btn_a.values, btn_b.values)

    View Slide

  27. Supporting multiple back-ends
    1) RPi.GPIO
    2) RPIO
    3) pigpio
    4) Native

    View Slide

  28. 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()

    View Slide

  29. 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()

    View Slide

  30. Picamera

    8 megapixels (v2)
    – v1 was 5mpx

    Visible light & infra-red
    versions available

    1080p30, 720p60 and
    VGA90 video

    Command line interface
    and Python library

    View Slide

  31. 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()

    View Slide

  32. 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()

    View Slide

  33. 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()

    View Slide

  34. 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()

    View Slide

  35. Picamera image effects

    View Slide

  36. Sense HAT workshop

    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

    View Slide

  37. Sense HAT

    View Slide

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

    View Slide

  39. Random sparkles!
    from sense_hat import SenseHat
    from random import randint
    from time import sleep
    sense = SenseHat()
    while True:
    x = randint(0, 7)
    y = randint(0, 7)
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)
    sense.set_pixel(x, y, r, g, b)
    sleep(0.01)

    View Slide

  40. ...and breathe...
    while True:
    print(sense.humidity)
    if sense.humidity < 60:
    sense.clear((255, 255, 255))
    sleep(0.5)
    else:
    sense.clear((255, 0, 0))
    sleep(2)

    View Slide

  41. Joystick
    def foo(ev):
    if ev.action == 'pressed':
    sense.show_message(str(sense.temperature))
    sense.stick.direction_up = foo

    View Slide

  42. Astro Pi

    View Slide

  43. Documentation

    gpiozero.readthedocs.io

    picamera.readthedocs.io

    pythonhosted.org/sense-hat

    View Slide