$30 off During Our Annual Pro Sale. View Details »

Physical computing with Python and Raspberry Pi - PyCon Russia

Physical computing with Python and Raspberry Pi - PyCon Russia

An introduction to the Raspberry Pi, and the Foundation's charitable work in education, and to getting started with physical computing using Python and GPIO Zero on the Raspberry Pi.

This was the closing talk at PyCon Russia 2016.

Ben Nuttall

July 04, 2016
Tweet

More Decks by Ben Nuttall

Other Decks in Programming

Transcript

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

    View Slide

  2. Ben Nuttall

    Raspberry Pi Community Manager
    – Programmes & outreach
    – Software & project development
    – Learning resources & teacher training

    Hobbyist turned employee

    Based in Cambridge, UK

    @ben_nuttall on Twitter

    View Slide

  3. Raspberry Pi

    View Slide

  4. Raspberry Pi Foundation

    Educational charity founded
    in 2006

    Owns trading subsidiary
    Raspberry Pi Trading Ltd

    Trading profits fund
    educational programmes

    View Slide

  5. A Computer (1981)

    View Slide

  6. A Computer (2010)

    View Slide

  7. Prototypes

    View Slide

  8. Raspberry Pi (2012)
    Raspberry Pi Model B
    700MHz single core 32-bit CPU
    VideoCoreIV 3D GPU
    256MB RAM
    $35

    View Slide

  9. Raspberry Pi Community

    View Slide

  10. Raspberry Jam

    View Slide

  11. Raspberry Jam
    raspberrypi.org/jam

    View Slide

  12. Made in the UK

    Since 2013, Raspberry Pi has
    been manufactured by Sony
    in Wales

    20k units per day at peak

    View Slide

  13. Raspberry Pi Compute Module
    Prototyping development board
    Build your product around the
    Raspberry Pi

    View Slide

  14. Raspberry Pi Zero (2015)
    1GHz single core 32-bit CPU
    VideoCoreIV 3D GPU
    512MB RAM
    $5

    View Slide

  15. Raspberry Pi 3 Model B (2016)
    1.2GHz quad core 64-bit CPU
    VideoCoreIV 3D GPU
    1GB RAM
    $35

    View Slide

  16. Over 9 Million Raspberry Pis sold

    Minor revisions

    Model A

    Model B+

    Model A+

    Raspberry Pi 2 Model B

    Raspberry Pi Zero

    Raspberry Pi 3 Model B

    View Slide

  17. Raspberry Pi Foundation Mission
    “Putting the power of digital making into the hands of people all
    over the world”

    View Slide

  18. Raspberry Pi computers

    We produce low-cost high-
    power computers

    They keep getting better
    and/or cheaper :)

    Used in
    – education
    – hobby projects
    – industry

    View Slide

  19. Raspberry Pi in Schools

    View Slide

  20. Education

    We train teachers in
    computing

    We create free resources for
    use at home and school

    We run programmes to
    engage young people in
    digital making

    We support a network of Code
    Clubs in Primary Schools

    View Slide

  21. Raspbian

    View Slide

  22. Raspbian

    Launch programming
    applications

    Web browser

    Preferences and settings

    Shutdown & reboot

    Look – it's a real computer!

    View Slide

  23. Physical computing

    Flashy lights

    Motors & robots

    Photo & video

    Sensors

    Internet of Things

    Engaging and empowering

    View Slide

  24. GPIO Pins – General Purpose Input/Output

    View Slide

  25. GPIO components

    View Slide

  26. Add-on boards / HATs

    View Slide

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

  28. GPIO in Scratch

    GPIO Server

    Use broadcast blocks

    Drag & drop programming
    – except when you have to
    write code in those broadcast
    blocks and get the syntax
    right as there's no error
    reporting...

    View Slide

  29. Something more approachable?

    Teachers saying Python is too hard to teach with
    – Particularly RPi.GPIO and PyGame

    Teachers staying in Scratch too long
    – Need help moving to Python
    – GPIO in Scratch is difficult

    Problems with RPi.GPIO
    – Too much boilerplate code required for even simple examples
    – Lots of copy/paste for complex components
    – Not Pythonic

    View Slide

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

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

    View Slide

  32. LED
    from gpiozero import LED
    led = LED(17)
    led.on() # on
    led.off() # off
    led.toggle() # on­>off or off­>on
    led.blink() # flash on/off continuously

    View Slide

  33. Buzzer
    from gpiozero import Buzzer
    buzzer = Buzzer(18)
    buzzer.on() # on
    buzzer.off() # off
    buzzer.toggle() # on­>off or off­>on
    buzzer.beep() # beep on/off continuously

    View Slide

  34. GitHub

    View Slide

  35. Button polling (pull-up) – RPi.GPIO
    GPIO.setup(4, GPIO.IN, GPIO.PUD_UP)
    while True:
    if not GPIO.input(4):
    print(“Pressed”)

    View Slide

  36. Button polling (pull-down) – RPi.GPIO
    GPIO.setup(4, GPIO.IN, GPIO.PUD_DOWN)
    while True:
    if GPIO.input(4):
    print(“Pressed”)

    View Slide

  37. Button polling (pull-up) – GPIO Zero
    button = Button(4)
    while True:
    if button.is_pressed:
    print(“Pressed”)

    View Slide

  38. Button polling (pull-down) – GPIO Zero
    button = Button(4, pull_up=False)
    while True:
    if button.is_pressed:
    print(“Pressed”)

    View Slide

  39. Button wait_for_edge (pull-up) – RPi.GPIO
    GPIO.setup(4, GPIO.IN, GPIO.PUD_UP)
    while True:
    GPIO.wait_for_edge(4, GPIO.FALLING)
    print(“Pressed”)

    View Slide

  40. Button wait_for_edge (pull-down) – RPi.GPIO
    GPIO.setup(4, GPIO.IN, GPIO.PUD_DOWN)
    while True:
    GPIO.wait_for_edge(4, GPIO.RISING)
    print(“Pressed”)

    View Slide

  41. Button wait_for_press (pull-up) – GPIO Zero
    button = Button(4)
    while True:
    button.wait_for_press()
    print(“Pressed”)

    View Slide

  42. Button wait_for_press (pull-down) – GPIO Zero
    button = Button(4, pull_up=False)
    while True:
    button.wait_for_press()
    print(“Pressed”)

    View Slide

  43. Button callback – RPi.GPIO
    GPIO.setup(4, GPIO.IN, GPIO.PUD_UP)
    def pressed(pin):
    print(“Pressed”)
    GPIO.add_event_detect(4, GPIO.FALLING, pressed)

    View Slide

  44. Button callback – GPIO Zero
    button = Button(4)
    def pressed():
    print(“Pressed”)
    button.when_pressed = pressed

    View Slide

  45. Button callback – RPi.GPIO
    GPIO.setup(4, GPIO.IN, GPIO.PUD_UP)
    def pressed(pin):
    print(“Pin %s Pressed” % pin)
    GPIO.add_event_detect(4, GPIO.FALLING, pressed)

    View Slide

  46. Button callback – GPIO Zero
    button = Button(4)
    def pressed(button):
    print(“Pin %s pressed” % button.pin.number)
    button.when_pressed = pressed

    View Slide

  47. Button + LED
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(4)
    button.when_pressed = led.on
    button.when_released = led.off

    View Slide

  48. LED – PWM – RPi.GPIO
    GPIO.setup(17, GPIO.OUT) # pin 17
    p = GPIO.PWM(17, 100) # pin 17, frequency 100Hz
    p.start(0) # initial duty cycle
    for i in range(101):
    p.ChangeDutyCycle(i)
    sleep(0.01)

    View Slide

  49. LED – PWM – GPIO Zero
    from gpiozero import PWMLED
    led = PWMLED(17)
    led.on() # on
    led.off() # off
    led.value = 0.5 # half brightness
    led.pulse() # blink with fade in/out

    View Slide

  50. Full colour LED
    from gpiozero import RGBLED
    led = RGBLED(red=2, green=3, blue=4)
    led.red.on() # full red
    led.color = (1, 0, 1) # purple
    led.blue = 0.3 # dim the blue value to 0.3
    # now (1, 0, 0.3)

    View Slide

  51. Traffic Lights
    from gpiozero import TrafficLights
    lights = TrafficLights(9, 10, 11)
    lights.on() # all on
    lights.off() # all off
    lights.red.on() # red on
    lights.toggle() # swap state of all lights

    View Slide

  52. Traffic Lights sequence
    while True:
    lights.green.on()
    lights.amber.off()
    lights.red.off()
    sleep(10)
    lights.green.off()
    lights.amber.on()
    sleep(1)
    lights.amber.off()
    lights.red.on()
    sleep(10)
    lights.amber.on()
    sleep(1)
    lights.green.on()
    lights.amber.off()
    lights.red.off()

    View Slide

  53. Traffic Lights sequence
    while True:
    lights.value = (1, 0, 0)
    sleep(10)
    lights.value = (0, 1, 0)
    sleep(1)
    lights.value = (0, 0, 1)
    sleep(10)
    lights.value = (0, 1, 1)
    sleep(1)

    View Slide

  54. Traffic HAT
    from gpiozero import TrafficHat
    th = TrafficHat()
    th.lights.red.on()
    th.lights.amber.on()
    th.button.when_pressed = th.on
    th.button.when_released = th.off

    View Slide

  55. TrafficHat - PWM
    from gpiozero import TrafficHat
    th = TrafficHat(pwm=True)
    th.lights.red.value = 0.2
    th.lights.amber.value = 0.4
    th.lights.green.value = 0.8

    View Slide

  56. Motion sensor
    from gpiozero import LED, MotionSensor
    led = LED(2)
    sensor = MotionSensor(3)
    sensor.when_motion = led.on
    sensor.when_no_motion = led.off

    View Slide

  57. Light sensor
    from gpiozero import LED, LightSensor
    led = LED(2)
    sensor = LightSensor(3)
    while True:
    sensor.wait_for_light()
    print("It's light!")
    sensor.wait_for_dark()
    print("It's dark")

    View Slide

  58. Motor
    from gpiozero import Motor
    from time import sleep
    motor = Motor(forward=17, backward=18)
    while True:
    motor.forward()
    sleep(5)
    motor.backward()
    sleep(5)

    View Slide

  59. Robot
    from gpiozero import Robot
    from time import sleep
    robot = Robot(left=(17, 18), right=(22, 23))
    while True:
    robot.forward()
    sleep(10)
    robot.left()
    sleep(1)

    View Slide

  60. Pre-configured robot interfaces
    robot = RyanteckRobot()
    robot = CamJamKitRobot()

    View Slide

  61. Button controlled Robot
    from gpiozero import RyanteckRobot, Button
    robot = RyanteckRobot()
    left = Button(26)
    right = Button(16)
    fw = Button(21)
    bw = Button(20)
    fw.when_pressed = robot.forward
    fw.when_released = robot.stop
    left.when_pressed = robot.left
    left.when_released = robot.stop
    right.when_pressed = robot.right
    right.when_released = robot.stop
    bw.when_pressed = robot.backward
    bw.when_released = robot.stop

    View Slide

  62. Picamera
    from picamera import PiCamera
    from gpiozero import Button
    from datetime import datetime
    camera = PiCamera()
    left = Button(4)
    right = Button(5)
    def capture():
    dt = datetime.now().isoformat()
    camera.capture('/home/pi/%s.jpg' % dt)
    left.when_pressed = camera.start_preview
    left.when_released = camera.stop_preview
    right.when_pressed = capture

    View Slide

  63. Push button stop motion
    from gpiozero import Button
    from picamera import PiCamera
    camera = PiCamera()
    button = Button(4)
    camera.start_preview()
    frame = 1
    while True:
    button.wait_for_press()
    camera.capture('/home/pi/frame%03d.jpg' % frame)
    frame += 1

    View Slide

  64. Analogue - potentiometers
    from gpiozero import MCP3008
    pot = MCP3008()
    while True:
    print(pot.value)

    View Slide

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

    View Slide

  66. Colour mixing
    from gpiozero import RGBLED, MCP3008
    led = RGBLED(red=2, green=3, blue=4)
    red_pot = MCP3008(channel=0)
    green_pot = MCP3008(channel=1)
    blue_pot = MCP3008(channel=2)
    while True:
    led.red = red_pot.value
    led.green = green_pot.value
    led.blue = blue_pot.value

    View Slide

  67. #76 while True: led.red = pot.value

    View Slide

  68. #76 while True: led.red = pot.value

    View Slide

  69. #76 while True: led.red = pot.value

    View Slide

  70. #76 while True: led.red = pot.value

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  74. Colour mixing
    from gpiozero import RGBLED, MCP3008
    led = RGBLED(red=2, green=3, blue=4)
    red_pot = MCP3008(channel=0)
    green_pot = MCP3008(channel=1)
    blue_pot = MCP3008(channel=2)
    led.red.source = red_pot.values
    led.green.source = green_pot.values
    led.blue.source = blue_pot.values

    View Slide

  75. Colour mixing
    from gpiozero import RGBLED, MCP3008
    led = RGBLED(red=2, green=3, blue=4)
    red_pot = MCP3008(channel=0)
    green_pot = MCP3008(channel=1)
    blue_pot = MCP3008(channel=2)
    led.source = zip(
    red_pot.values,
    green_pot.values,
    blue_pot.values
    )

    View Slide

  76. Source / Values
    Output Device
    .value
    .values
    .source
    Custom generator
    while True:

    yield value

    View Slide

  77. Custom value generators
    blue = PWMLED(16)
    blue.source_delay = 0.1
    blue.source = (i / 10000 for i in range(10000))

    View Slide

  78. Custom value generators
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values
    Custom generator
    e.g. read_slowly

    View Slide

  79. Custom value generators
    def read_slowly(values):
    for i in values:
    yield i
    sleep(0.1)
    blue.source = read_slowly(sensor.values)

    View Slide

  80. Source Tools
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values
    Source tool
    e.g. invert

    View Slide

  81. Source Tools
    from gpiozero import PWMLED, MCP3008
    from gpiozero.tools import inverted
    led = PWMLED(4)
    pot = MCP3008(channel=0)
    led.source = inverted(pot.values)

    View Slide

  82. LEDBoard
    from gpiozero import LEDBoard
    from time import sleep
    leds = LEDBoard(2, 3, 4, 5, 6)
    for led in leds:
    led.on()
    sleep(1)
    leds.off()

    View Slide

  83. LEDBarGraph
    from gpiozero import LEDBoard
    from time import sleep
    leds = LEDBarGraph(2, 3, 4, 5, 6, 7)
    leds.value = 1/2 # (1, 1, 1, 0, 0, 0)
    leds.value = ­1/2 # (0, 0, 0, 1, 1, 1)
    leds.value = 1/4 # (1, 0, 0, 0, 0, 0)

    View Slide

  84. LEDBarGraph - PWM
    from gpiozero import LEDBoard
    from time import sleep
    leds = LEDBarGraph(2, 3, 4, 5, 6, 7, pwm=True)
    leds.value = 1/4 # (1, 0.5, 0, 0, 0, 0)
    leds.value = 3/4 # (1, 1, 1, 1, 0.5, 0)

    View Slide

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

    View Slide

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

    View Slide

  87. GPIO Zero Timeline

    12 Sept – Idea sparked

    14 Sept – Initial commit on GitHub

    15 Sept – Named GPIO Zero, first PR, first alpha released on PyPI

    28 Sept – v0.6 public beta 1

    25 Oct – v0.9 public beta 4

    16 Nov – v1.0 released

    21 Nov – Released in Raspbian Jessie

    8 Feb – v1.1 released

    10 Apr – v1.2 released

    View Slide

  88. v1.0

    200 commits

    2 contributors (+4 minor contributions)

    103 GitHub issues (53 issues, 50 PRs)

    4 alpha releases

    4 beta releases

    68 days between initial commit and major release

    View Slide

  89. v1.3 (a work in progress)

    522 commits

    5 contributors (+8 minor contributions)

    390 GitHub issues (178 issues, 212 PRs)

    View Slide

  90. Future development

    Add more components

    Probably move default from RPi.GPIO to pigpio

    Better remote GPIO support

    Promote use of “gpiozero standard” to allow other modules to
    provide objects which plug-in to gpiozero objects easily (e.g.
    source/values)

    View Slide

  91. Install GPIO Zero

    Pre-installed in Raspbian Jessie since November

    Update with:
    sudo apt­get update

    Install with:
    sudo apt­get install python3­gpiozero
    or:
    sudo apt­get install python­gpiozero

    View Slide

  92. GPIO Zero

    gpiozero.readthedocs.io
    – Installation instructions
    – API Documentation
    – Examples

    GitHub Issues
    – Suggestions
    – Feedback

    Contact me
    [email protected]
    – @ben_nuttall on Twitter

    #gpiozero on Twitter

    View Slide

  93. Zero all the things!

    PyGame Zero

    GPIO Zero

    Network Zero

    BlueZero

    View Slide

  94. How can you get involved?

    View Slide

  95. www.raspberrypi.org

    Daily blog articles – news,
    projects and stories

    OS Downloads

    Help pages

    Documentation

    Community sites

    Forums

    View Slide

  96. Raspberry Pi Learning Resources

    Teach, Learn and Make

    Free

    Open source

    Creative Commons

    Created by Raspberry Pi
    Education Team

    Scratch, Python and more

    View Slide

  97. Raspberry Pi Weekly

    Free weekly email newsletter

    Raspberry Pi news, projects
    and articles

    3 years of issues on the
    website

    raspberrypi.org/weekly

    View Slide

  98. The MagPi

    Community magazine established
    in 2012 (as free PDF download)

    Now the official Raspberry Pi
    magazine

    Paper copies on sale in UK shops
    and online

    Still a free PDF download

    Occasionally comes with a free
    computer

    Book series (also available for free)

    View Slide

  99. Raspberry Jam in Russia

    Start a Raspberry Jam in your
    area

    Create opportunities for kids
    to learn programming

    Share skills and projects

    See www.raspberrypi.org/jam

    Contact me for support

    View Slide

  100. Code Club in Russia

    Translate our projects

    Run a club for 9-11 year olds

    Become a country
    coordinator

    See www.codeclubworld.org

    Contact me or
    [email protected]

    View Slide

  101. How can you get involved?

    View Slide