I've built a trail camera using a RPi Zero, the camera module and a PIR sensor to detect motion. I use a python program set to run on boot that records the footage and saves it to an attached USB device. The whole thing is powered by an USB power bank. Straight forward enough.

Recently, I've been noticing that after the first or second video has been recorded, subsequent video files are empty (0kb).

I've attached the full program below. I wondered if this might be something to do with how the PIR is being polled and if it is not getting enough of a break in order to save the file properly.

from gpiozero import MotionSensor
import logging
from datetime import datetime
from subprocess import call
import picamera
import time
import os

logfile ="/home/pi/trailcam_log/trailcam_log"+str(datetime.now().strftime("%Y%m%d%H%M"))+".csv"
logging.basicConfig(filename=logfile, level=logging.DEBUG,
format='%(asctime)s %(message)s',
datefmt='%Y-%m-%d, %H:%M:%S,')

pir = MotionSensor(17)


# Wait an initial duration

while True:
    logging.info('Motion detected')
    print('Motion detected')
    while pir.motion_detected:
        print('Taking photo')
        ts = '{:%Y%m%d-%H%M%S}'.format(datetime.now())
        logging.info('Taking photo: '+ str(ts)+'.jpg')
        with picamera.PiCamera() as cam:
            cam.annotate_background = picamera.Color('black')

            start = datetime.now()
            while (datetime.now() - start).seconds < 30:
                cam.annotate_text = "Peak Nature "+datetime.now().strftime('%d-%m-%y %H:%M:%S')
        timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')
        input_video = "/home/pi/video.h264"

        logging.info('Attempting to save image')

        if os.path.isdir('mnt/usb1/videos'):
            logging.info('Saving to /mnt/usb1/videos/')
            output_video = "/mnt/usb1/videos/{}.mp4".format(timestamp)
        elif os.path.isdir('mnt/usb2/videos'):
            logging.info('Saving to /mnt/usb2/videos/')
            output_video = "/mnt/usb2/videos/{}.mp4".format(timestamp)
            logging.info('Saving to /home/pi/videos/')
            output_video = "/home/pi/videos/{}.mp4".format(timestamp)

        call(["MP4Box", "-add", input_video, output_video])
    print('Motion Ended')
    logging.info('Motion Ended')

Thanks for any advice.


  • Have you tried increasing your sleep timers to allow the sensor more time?
    – goobering
    Commented Apr 12, 2017 at 14:52
  • Hi @goobering, yes. See my answer for details.
    – Chris J
    Commented Apr 13, 2017 at 9:17

1 Answer 1


It looks as if reducing the sensitivity and the delay potentiometers on the PIR itself has done the trick. I had them set too high.

I tested the camera over about 12 hours and all the resulting video files were properly created (40-50Mbs).

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.