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

Python Picamera with GPIO Zero - Picademy USA #2

Python Picamera with GPIO Zero - Picademy USA #2

Using Python Picamera and GPIO Zero

Ben Nuttall

April 30, 2016
Tweet

More Decks by Ben Nuttall

Other Decks in Education

Transcript

  1. Raspberry Pi camera module - 5Mpx - Full HD -

    Photo & video - Command line - Python module - Infra-red camera
  2. 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
  3. 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
  4. Open a new file • File > New File •

    File > Save • Save as camera.py
  5. 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
  6. 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
  7. 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
  8. 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)
  9. 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
  10. 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()