Physical computing with GPIO Zero

A1995c9abe48450ce2f82d93ca0b863f?s=47 Ben Nuttall
March 06, 2016
250

Physical computing with GPIO Zero

Talk on physical computing with Raspberry Pi using GPIO Zero, a new friendly Python API.

A1995c9abe48450ce2f82d93ca0b863f?s=128

Ben Nuttall

March 06, 2016
Tweet

Transcript

  1. 2.

    Ben Nuttall • Education Developer Advocate at the Raspberry Pi

    Foundation – Software & project development – Learning resources & teacher training – Community outreach • @ben_nuttall on Twitter
  2. 3.

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

    Python – GPIO Zero from gpiozero import LED from time

    import sleep led = LED(17) while True: led.on() sleep(1) led.off() sleep(1)
  4. 6.

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

    LED + Button from gpiozero import LED, Button led =

    LED(17) button = Button(3) button.when_pressed = led.on button.when_released = led.off
  6. 8.

    Source + Values from gpiozero import LED, Button led =

    LED(17) button = Button(3) led.source = button.values
  7. 9.

    Button + Picamera from gpiozero import Button From picamera import

    PiCamera button = Button(3) camera = PiCamera() button.when_pressed = camera.start_preview button.when_released = camera.stop_preview
  8. 10.

    LED - PWM from gpiozero import PWMLED from time import

    sleep led = PWMLED(17) led.on() # on led.off() # off led.value = 0.5 # half brightness led.pulse() # coming in v1.2
  9. 11.

    Motion sensor from gpiozero import LED, MotionSensor led = LED(2)

    sensor = MotionSensor(3) sensor.when_motion = led.on sensor.when_no_motion = led.off
  10. 12.

    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")
  11. 13.

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

    Traffic Lights sequence lights.green.on() lights.amber.off() lights.red.off() while True: 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()
  13. 15.
  14. 16.

    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
  15. 17.

    Push button stop motion from gpiozero import Button from picamera

    import PiCamera button = Button(4) with PiCamera() as camera: camera.start_preview() frame = 1 while True: button.wait_for_press() camera.capture('/home/pi/frame%03d.jpg' % frame) frame += 1
  16. 18.

    Full colour LED from gpiozero import RGBLED led = RGBLED(2,

    3, 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)
  17. 19.

    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)
  18. 20.

    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)
  19. 21.

    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
  20. 23.

    Dial up the brightness! from gpiozero import PWMLED, MCP3008 led

    = PWMLED(2) pot = MCP3008() while True: led.value = pot.value
  21. 24.

    Dial up the brightness! from gpiozero import PWMLED, MCP3008 led

    = PWMLED(2) pot = MCP3008() led.source = pot.values
  22. 25.

    Analogue - potentiometers 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
  23. 26.

    Analogue - potentiometers 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
  24. 27.

    Analogue - potentiometers 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)
  25. 28.

    GPIO Music Box from gpiozero import Button import pygame.mixer from

    pygame.mixer import Sound pygame.mixer.init() sound_pins = { 2: Sound("samples/drum_tom_mid_hard.wav"), 3: Sound("samples/drum_cymbal_open.wav"), } buttons = [Button(pin) for pin in sound_pins] for button in buttons: sound = sound_pins[button.pin] button.when_pressed = sound.play
  26. 29.

    GPIO Zero • gpiozero.readthedocs.org – Installation instructions – Documentation –

    Examples • GitHub Issues – Suggestions – Feedback • Contact me – ben@raspberrypi.org – @ben_nuttall on Twitter • #gpiozero on Twitter
  27. 30.

    CamJam EduKit • £5 starter kit (kit 1) • £7

    sensors kit (kit 2) • £17 robotics kit (kit 3) • Free worksheets (soon to be GPIO Zero) • Very reusable
  28. 31.

    GPIO Zero • Pre-installed in Raspbian Jessie • Available with

    apt: – sudo apt-get update – sudo apt-get install python3-gpiozero – sudo apt-get install python-gpiozero
  29. 32.
  30. 33.