Save 37% off PRO during our Black Friday Sale! »

Python Picamera with GPIO Zero - Picademy USA #2

Python Picamera with GPIO Zero - Picademy USA #2

Using Python Picamera and GPIO Zero

A1995c9abe48450ce2f82d93ca0b863f?s=128

Ben Nuttall

April 30, 2016
Tweet

Transcript

  1. Python Picamera featuring GPIO Zero Introducing the camera board and

    Python module
  2. Raspberry Pi camera module - 5Mpx - Full HD -

    Photo & video - Command line - Python module - Infra-red camera
  3. What you will learn • How to connect the camera

    module • How to use Python to take pictures • How to add physical components to your your project • How to use loops to repeat commands
  4. Connect the camera

  5. Add a GPIO Button

  6. Boot the Pi and open Python 3

  7. IDLE Python Shell >>> from picamera import PiCamera >>> from

    gpiozero import Button >>> camera = PiCamera() >>> button = Button(17) >>> button.when_pressed = camera.start_preview >>> button.when_released = camera.stop_preview
  8. Open a new file • File > New File •

    File > Save • Save as camera.py
  9. Take a selfie from picamera import PiCamera from time import

    sleep camera = PiCamera() camera.start_preview() sleep(3) camera.capture("/home/pi/image.jpg") camera.stop_preview() Save and run: Ctrl + S F5
  10. Add GPIO Button code from picamera import PiCamera from gpiozero

    import Button from time import sleep camera = PiCamera() button = Button(17) camera.start_preview() button.wait_for_press() sleep(3) camera.capture("/home/pi/button.jpg") camera.stop_preview() Save as button.py
  11. Add a loop camera.start_preview() for i in range(5): button.wait_for_press() sleep(3)

    camera.capture("/home/pi/button%s.jpg" % i) camera.stop_preview() Save as loop.py
  12. What's the difference? for i in range(5): button.wait_for_press() sleep(3) camera.capture("/home/pi/button%s.jpg"

    % i) button.wait_for_press() for i in range(5): sleep(3) camera.capture("/home/pi/button%s.jpg" % i)
  13. Connect a PIR Motion Sensor

  14. Button Motion Sensor? → from picamera import PiCamera from gpiozero

    import Button from time import sleep camera = PiCamera() button = Button(17) camera.start_preview() button.wait_for_press() sleep(3) camera.capture("/home/pi/button.jpg") camera.stop_preview() Open button.py Save as pir.py
  15. Button Motion Sensor → from picamera import PiCamera from gpiozero

    import Button from time import sleep camera = PiCamera() button = Button(17) camera.start_preview() button.wait_for_press() sleep(3) camera.capture("/home/pi/button.jpg") camera.stop_preview() from picamera import PiCamera from gpiozero import MotionSensor from time import sleep camera = PiCamera() sensor = MotionSensor(4) camera.start_preview() sensor.wait_for_motion() sleep(3) camera.capture("/home/pi/pir.jpg") camera.stop_preview()
  16. Picamera effects

  17. Picamera effects camera.start_preview() button.wait_for_press() camera.image_effect = 'negative' sleep(5) camera.capture("/home/pi/negative.jpg") camera.stop_preview()

    Try more effects: - negative - colorswap - sketch - emboss Save as effect.py
  18. Documentation and help guides • picamera.readthedocs.io • gpiozero.readthedocs.io • raspberrypi.org/resources

    • raspberrypi.org/education/downloads
  19. What next? • Time-lapse • Stop motion animation • Wildlife

    camera
  20. What next? • Sensor trigger • Send to social media

    • Robotics
  21. Python Picamera featuring GPIO Zero Introducing the camera board and

    Python module