Trailcam – 3. PIR sensor

Other sections

  1. Introduction
  2. Basic setup & Power
  3. PIR sensor
  4. IR lamp
  5. IR filter
  6. Buttons
  7. Completed pics, improvements and parts list
  8. Updates

 

Description

The PIR sensor is a pretty standard piece of kit – I got mine from Tandy however they’re really common and stocked by the usual suspects (adafruit, pimoroni, Modmypi,maplin)

In the first version I had the PIR sensor triggering the recording however for this version I’ve altered it so that it just turns the IR lamp on leaving the motion detection in the software to do its thing..

Install

Camera in window cill mode
PIR at bottom of trailcam

I had hoped that the PIR would “see” through the plastic on the box however it wasn’t happening so I ended up removing the defractor / lens, drilling a hole through the box to push the sensor through then stick the lens on the outside. It does increase the chance of letting water through however there isn’t really much choice and a big chunk of gaffer tape should sort it out.

PIR
Dismantled PIR sensor (main). I cut a hole and pushed the sensor section through it, then fixed the diffuser on the outside (insert)

Control programme

I modified the control program from raspberry pi spy to run the previous version of the trail cam however this version only needs to turn the light on an off, hence why it is a bit of a mess.

EDIT: Revised version here

#!/usr/bin/python
# Original
# By : Raspberry Pi Spy
# Author : Matt Hawkins
# Date : 21/01/2013
# Modification
# By : G
# Date : 24/04/2016


# Import required Python libraries
import RPi.GPIO as GPIO
import time
import subprocess

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define PIR GPIO to use on Pi
GPIO_PIR = 22

print "PIR Running (CTRL-C to exit)"

# Set PIR pin as input
GPIO.setup(GPIO_PIR,GPIO.IN) # Echo

Current_State = 0
Previous_State = 0

# Define GPIO for lamp control
GPIO_LIGHT_ON = 17

# Set lamp on GPIO as output
GPIO.setup(GPIO_LIGHT_ON,GPIO.OUT) # Echo

try:

 print "Waiting for PIR to settle ..."

 # Loop until PIR output is 0
 while GPIO.input(GPIO_PIR)==1:
 Current_State = 0 

 print " Ready" 
 
 # Loop until users quits with CTRL-C
 while True :
 
 # Read PIR state
 Current_State = GPIO.input(GPIO_PIR)
 if Current_State==1 and Previous_State==0:
 # PIR is triggered
 print " Motion detected!"
 GPIO.output (GPIO_LIGHT_ON,1)
 print " Light on"
 # Capture a 5 second video
 # print " Record start"
 time.sleep(20)
 # Record previous state
 Previous_State=1
 elif Current_State==0 and Previous_State==1:
 # PIR has returned to ready state
 GPIO.output (GPIO_LIGHT_ON,0) 
 print " Light off" 
 print " Ready"
 Previous_State=0
 
 # Wait for 10 milliseconds
 time.sleep(0.01) 
 
except KeyboardInterrupt:
 print " Quit" 
 GPIO.output (GPIO_LIGHT_ON,0)
 # Reset GPIO settings
 GPIO.cleanup()

2 thoughts on “Trailcam – 3. PIR sensor”

Leave a Reply

Your email address will not be published. Required fields are marked *