Category Archives: featured

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()

Trailcam – 4. I.R. Lamp

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 IR lamp is a kit from Maplin. I’d priced up getting the LED’s individually and putting them on to a bit of veroboard and the kit worked out much cheaper.

inside front of camera
from top to bottom: I.R. lamp, camera and I.R. filter and P.I.R. lamp, as seen from the inside of the front section of the trailcam.

It is designed to be 12v but it works satisfactorily with a 9v source. I have bought another of the kits when they were on special offer so I may end up swapping out the resistors to make it truly 9v.

light
12v I.R. lamp kit from Maplin (My spare one)

Powering the lamp

I wanted the lamp to be powered separately from the PI, initially because it is much easier to do but also because it’ll let me be a bit more flexible if I choose to change the lamp or power source (It also helps to reduce the load on the battery).  I settled for a relay kit from Ciseco – as with the lamp it proved cheaper than buying the bits.

relay
Ciesco 3v3 relay kit

I’ve soldered this board so many times the tack has come off, so I’ve had to find alternative places to put the wires! This has become a major weak point of the build.

For debug purposes I’ve produced 2 python programs – one to turn it on and the other to turn it off.

On
#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import subprocess

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

# Define GPIO for light
GPIO_LIGHT_ON = 17

# Set light on GPIO as output
GPIO.setup(GPIO_LIGHT_ON,GPIO.OUT)
GPIO.output (GPIO_LIGHT_ON,1)
print " Light on"
GPIO.cleanup()


Off
#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import subprocess

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

# Define GPIO for light
GPIO_LIGHT_ON = 17

# Set light on GPIO as output
GPIO.setup(GPIO_LIGHT_ON,GPIO.OUT)
GPIO.output (GPIO_LIGHT_ON,0)
print " Light off"
GPIO.cleanup()

	

Trailcam – 5. I.R. Filter

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.

I bought the filter from an EBay seller in China. It took a while to arrive but was extremely cheap and pretty good quality. It comprises of a main body with an electrical connection coming out of it and works by sliding a tiny IR filter in front of the camera, replacing the missing filter on the pi noIR camera to give a better colour reproduction under natural light.

To get it to work the wires need to receive a brief current, when this is reversed (i/e the previous +ve connection becomes –ve) it goes the other way. I needed to find something that could reverse the polarity on a pair of wires and came across this description about a motor controller. http://computers.tutsplus.com/tutorials/controlling-dc-motors-using-python-with-a-raspberry-pi–cms-20051

I built this one using the diagram provided by Envatotuts+ on to a lovely adafruit permaproto board.

filter board

IC

Mount to cam

The IR filter is clearly from some sort of CCTV camera and so comes with a couple of mounting holes. These almost line up with the mounting holes on the camera board and pimoroni camera holder so I’ve bolted them together with some nylon nuts and bolts.

I tried to fit the camera unit behind the clear plastic of the box but the result was disappointing with a very noticeable blue / grey tinge so I ended up cutting a hole through it and pushing the camera assembly through. I’m not too happy about the camera position in relation to the filter housing and intend to jiggle it about a bit in the future.

In order to prevent water getting in I’ve put a clear lens over the outside. This is from a go-pro and was a very cheap pattern part. A bit of tape and some blu-tak and it’s firmly in position.

Automatic switching.

In order to use the camera as both a night time trail cam and a daytime birdfeeder camera I needed to get the IR filter to switch automatically. This will also help to give a better picture on the trailcam in the summer when full darkness is brief.

I’ve looked at putting in an LDR (and may still do in the future) however for the time being I’ve set up cron to run the on / off program at specific times. Pikrellcam also has the facility to run scripts and I will investigate this in the future also.

Python script (taken from the Envatotuts+ blog)

filter off

import RPi.GPIO as GPIO
from time import sleep
 
GPIO.setmode(GPIO.BOARD)
 
Motor1A = 16
Motor1B = 18
Motor1E = 22
 
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
 
print "Sending signal"
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)
 
sleep(0.1)
 
print "Filter off"
GPIO.output(Motor1E,GPIO.LOW)
 
GPIO.cleanup()

filter on

import RPi.GPIO as GPIO
from time import sleep
 
GPIO.setmode(GPIO.BOARD)
 
Motor1A = 16
Motor1B = 18
Motor1E = 22
 
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
 
print "Sending signal"
GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor1E,GPIO.HIGH)
 
sleep(0.1)
 
print "Filter on"
GPIO.output(Motor1E,GPIO.LOW)
 
GPIO.cleanup()

Addition to the cron

00 16 * * * /usr/bin/python /home/pi/filteroff.py