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

Physical Computing with Python and Raspberry Pi...

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. Ben Nuttall • Raspberry Pi Community Manager • Creator of

    gpiozero python library • github.com/bennuttall • @ben_nuttall on Twitter
  2. 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
  3. Physical computing • Flashy lights • Motors & robots •

    Photo & video • Sensors • Internet of Things • Engaging and empowering
  4. 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)
  5. Python - GPIO Zero from gpiozero import LED from time

    import sleep led = LED(17) while True: led.on() sleep(1) led.off() sleep(1)
  6. 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()
  7. 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
  8. Dial up the brightness! from gpiozero import PWMLED, MCP3008 led

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

    = PWMLED(2) pot = MCP3008() led.source = pot.values
  10. 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)
  11. 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()
  12. 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()
  13. Picamera • 8 megapixels (v2) – v1 was 5mpx •

    Visible light & infra-red versions available • 1080p30, 720p60 and VGA90 video • Command line interface and Python library
  14. 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()
  15. 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()
  16. 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()
  17. 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()
  18. 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
  19. Sense HAT >>> from sense_hat import SenseHat >>> sense =

    SenseHat() >>> sense.show_message(“Hello world”) >>> sense.temperature >>> sense.humidity >>> sense.accelerometer >>> sense.gyroscope >>> sense.orientation
  20. 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)
  21. ...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)