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

Physical computing with Scratch and Python

Physical computing with Scratch and Python

Physical computing with Raspberry Pi using Scratch and Python with GPIO Zero

Ben Nuttall

March 21, 2016
Tweet

More Decks by Ben Nuttall

Other Decks in Education

Transcript

  1. Physical Computing
    with Scratch and Python

    View Slide

  2. View Slide

  3. Boot the Pi and open Scratch

    View Slide

  4. GPIO Server

    GPIO Server On

    View Slide

  5. Broadcast

    configXout

    gpioXon

    gpioXoff

    wait X secs

    forever
    Component GPIO pin
    Red LED 25
    Amber LED 8
    Green LED 7
    Buzzer 15

    View Slide

  6. Close Scratch and open Python 3

    View Slide

  7. Open a new file

    File > New File

    File > Save

    Save as camjam.py

    View Slide

  8. GPIO Zero LED
    from gpiozero import LED
    led = LED(25)
    while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)
    Save and run:
    Ctrl + S
    F5

    View Slide

  9. GPIO Zero LED
    from gpiozero import LED
    led = LED(25)
    led.blink()

    View Slide

  10. GPIO Zero Button + LED
    from gpiozero import Button, LED
    button = Button(21)
    led = LED(25)
    while True:
    button.wait_for_press()
    led.on()
    button.wait_for_release()
    led.off()

    View Slide

  11. GPIO Zero Button + LED
    from gpiozero import Button, LED
    button = Button(21)
    led = LED(25)
    while True:
    led.on()
    button.wait_for_press()
    led.off()
    button.wait_for_release()

    View Slide

  12. GPIO Zero Button + LED
    from gpiozero import Button, LED
    button = Button(21)
    led = LED(25)
    while True:
    led.blink()
    button.wait_for_press()
    led.off()
    button.wait_for_release()

    View Slide

  13. Traffic Lights
    from gpiozero import Button, TrafficLights
    button = Button(21)
    lights = TrafficLights(25, 8, 7)
    while True:
    button.wait_for_press()
    lights.on()
    button.wait_for_release()
    lights.off()

    View Slide

  14. Traffic Lights
    from gpiozero import Button, TrafficLights
    button = Button(21)
    lights = TrafficLights(25, 8, 7)
    while True:
    lights.blink()
    button.wait_for_press()
    lights.on()
    button.wait_for_release()

    View Slide

  15. Traffic Lights
    from gpiozero import Button, TrafficLights, Buzzer
    from time import sleep
    button = Button(21)
    lights = TrafficLights(25, 8, 7)
    buzzer = Buzzer(15)
    while True:
    lights.on()
    buzzer.off()
    button.wait_for_press()
    lights.off()
    buzzer.on()
    button.wait_for_release()

    View Slide

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

    View Slide

  17. Traffic Lights
    while True:
    button.wait_for_press()
    lights.green.on()
    sleep(1)
    lights.amber.on()
    sleep(1)
    lights.red.on()
    sleep(1)
    lights.off()

    View Slide

  18. Traffic Lights Sequence

    Can you make a full traffic lights sequence?

    Use the button for a pedestrian crossing

    Use buzzer.beep() to indicate safe crossing

    View Slide

  19. Documentation and help guides

    raspberrypi.org/documentation/usage/scratch/gpio

    gpiozero.readthedocs.org

    raspberrypi.org/resources

    raspberrypi.org/education/downloads

    View Slide

  20. Physical Computing
    with Scratch and Python

    View Slide