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

A1995c9abe48450ce2f82d93ca0b863f?s=128

Ben Nuttall

January 10, 2017
Tweet

Transcript

  1. Physical Computing with Python and Raspberry Pi Ben Nuttall Raspberry

    Pi Foundation UK Charity 1129409
  2. Ben Nuttall • Raspberry Pi Community Manager • Creator of

    gpiozero python library • github.com/bennuttall • @ben_nuttall on Twitter
  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
  4. Raspbian Jessie with PIXEL

  5. GPIO Pins – General Purpose Input/Output

  6. Physical computing • Flashy lights • Motors & robots •

    Photo & video • Sensors • Internet of Things • Engaging and empowering
  7. GPIO • 3V3, 5V • GPIO, SPI, I2C, UART •

    GPIO = variable 3V3
  8. GPIO components

  9. Add-on boards / HATs

  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)
  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)
  12. Python - GPIO Zero from gpiozero import LED led =

    LED(17) led.blink()
  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()
  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
  15. Multi-paradigm: declarative from gpiozero import LED, Button led = LED(17)

    button = Button(4) led.source = button.values
  16. GPIO Zero supports...

  17. GPIO Zero Device Hierarchy!

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

    = PWMLED(2) pot = MCP3008() while True: led.value = pot.value
  19. Source / Values Output Device .value .values .source Input Device

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

    .value .values
  21. Dial up the brightness! from gpiozero import PWMLED, MCP3008 led

    = PWMLED(2) pot = MCP3008() led.source = pot.values
  22. Source / Values Output Device .value .values .source Custom generator

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

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

    .value .values Custom generator
  25. Source Tools Output Device .value .values .source Input Device .value

    .values Source tool
  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)
  27. Supporting multiple back-ends 1) RPi.GPIO 2) RPIO 3) pigpio 4)

    Native
  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()
  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()
  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
  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()
  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()
  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()
  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()
  35. Picamera image effects

  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
  37. Sense HAT

  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
  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)
  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)
  41. Joystick def foo(ev): if ev.action == 'pressed': sense.show_message(str(sense.temperature)) sense.stick.direction_up =

    foo
  42. Astro Pi

  43. Documentation • gpiozero.readthedocs.io • picamera.readthedocs.io • pythonhosted.org/sense-hat