I'm trying to write a program in python 3.4 for my Raspberry Pi that will take some photos when motion is detected from an IR sensor ( A bird table in this case).

The camera and IR sensor are working fine but I'm trying to figure out how to just take a few pictures when a bird triggers the sensor and then put the program to sleep for a bit and then take some more photos later when another bird arrives. The problem with my program is that it just continually takes photos non-stop when any motion is detected. Any ideas. Thanks.

from gpiozero import MotionSensor
import time
from datetime import datetime
from picamera import PiCamera
from time import sleep


pir = MotionSensor(4)
while True:
    if pir.motion_detected:
        filename = datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
        camera.capture('/home/pi/Desktop/images/'+filename +'.jpg')
        print('picture taken')
  • you need something that is going to stop the camera taking pictures if motion is not detected.
    – Darth Vader
    Commented Jun 26, 2016 at 11:03
  • A better approach would be to setup an event listener to take a photo when the sensor is triggered. There are many tutorials on the web regarding gpio event listeners.
    – Andy Lamb
    Commented Jun 26, 2016 at 12:36

1 Answer 1



# Code here
# Sleep
  • 1
    I don't think you need to sleep after wait_for_no_motion. You can go back to wait_for_motion directly. Commented Mar 30, 2017 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.