Category Archives: pi ir camera

Handheld Raspberry Pi Infra Red camera


Following the construction of my trailcam, I’ve created a new version that is also hand held.  The intention is to use it as a normal trailcam as before (leaving it in position and triggering the video / light via sensor), as well as using the IR lamp & camera to view wildlife in the dark via the screen.

Prototype , built in the modmypi packaging!

The main construction comprises of a 12v IR lamp from a security camera along with a pi noir camera and adafruit screen all controlled by a raspberry pi (model 2B). I’ve added a couple of buttons to control the light, video record etc for use in handheld mode with the pi controlling the functions in automatic / trailcam mode.

Front of camera

Raspberry pi

I’ve used a model B raspberry pi 2 for this project. Originally I’d hoped to use a zero with a pimoroni explorer pHAT however I had trouble getting the screen to work with the zero and the explorer so reverted back to my test system – the pi2. My main concern was battery life but it doesn’t seem to be too much of an issue.


Rear of camera

The screen is an adafruit 3.5″ pitft plus bought from pimoroni. This has a restive touch screen incorporated which can be used to operate some of the functions – I use a spare pointer from a Nintendo DS which is fairly accurate.


The buttons are connected to a board which is in turn connected to the pi gpio pins via the 26way ribbon cable at the back of the screen as well as from the pads on the screen.

The rear of the touchscreen

I’ve since moved the red wire in the pic above as it sat on, & made connection to the usb port on the pi B.

Board 1

There are quite a few gpio controlled functions so I’ve used 2 boards all linked together with with JST connectors. It is possible to shrink most of the connections down to a single, more compact board however I wanted it to be modular and by construction skills weren’t up to it.

Board 2


The lamp needs to be as bright as possible so I’ve had to use 12v. Rather than having 2 power sources as before (1 for the light and one for the pi) I’ve gone for a single source and used a step down power supply for the pi. I’ve used a batt borg power converter bought from modmypi and it is excellent, working flawlessly.

The power itself is supplied via 10 x 1.2v rechargeable batteries which last for quite some time. I’ve also fitted a socket for a power supply to be attached, for when I’m testing it and if it is deployed near a socket. A simple switch selects if it runs from batteries of power supply.


The light is a super cheap replacement board from a security camera. I’ve disabled the LDR using blutak so it could be used if necessary. There’s also a connection of the filter on the board – presumably it auto selects if the filter is required – I’ve not used this as I wanted the filter to be controlled by the pi.

light with filter inside

The light is controlled via a simple switch on the body of the camera as well as via a relay for use in trailcam mode. I bought a relay from an ebay seller as it was cheaper and easier than making my own. The supplier said that he’d imported some for himself but had a few spare to sell so  I don’t know if he’s got any left. Excellent seller, though and really quickly delivered and good item.

(I had experimented with making my own board but managed to kill a pizero in the process so went for the safer option)


The filter is pretty much the same as used previously and controlled by a motor controller chip on board 2.

I’ve described it in more detail on my previous post here: Trailcam 5 I.R. Filter.

Software & Code

I’ve once again opted for the excellent pikrellcam with its built in motion detection. This has worked very well on my previous trailcam and it works perfectly when used in manual record mode.

The pi boots up in kiosk mode and displays a modified version of the pikrellcam web page – I’ve altered it to change the size of the image and add the touch buttons at the side. If the main image is clicked it reverts back to the normal pikrellcam page giving access to the additional functionality and media storage.

The simplified web page

It is worth noting that the web page will be deleted if pikrelcam updates, so I’ve had to live without updates for the time being. If it should need updating I’ll copy back the modified page after the update.

EDIT: Billw has provided a solution for the issue here

Finally, a python script runs in the background which provides the functionality for the buttons, this is started in the same way as the kiosk mode and calls a separate piece of code to run the motion detection when the button is pressed – described here

I’ve put the code on a separate page here. I’m no programmer however it seems to work.

Start / stop python script from python


We took it out for a walk in the dark a week or so ago and the results were better than expected:

First test:

These were taken when it was in the cardboard box and held together with elastic bands. The light has been improved since then as well.

A montage of several situations:

It’s got a range of about 40m showing movement with 20m and less giving reasonable detail.

The danger of looking at a screen rather than concentrating on where you’re going:

I’ll post up some more videos when I’ve experimented a bit more – I’m desperate to see if I can video a hare in the nearby fields.

Main youtube:


There is some glare on the lens from the lamp. Currently I’ve got an old toilet roll holder inside the light, this needs to be made more permanent and increased in length.

It could do with a new box that’s a bit more suitable for leaving outside.

I’ve got some new buttons to replace the current ones. They’re waterproof so I can use them in conjunction with a new, more robust housing for prolonged trailcam usage.

Being left handed, I’ve built it to be easier to use with the left hand in terms of both button position and weight distribution. Right handed people seem to find this hugely difficult to use so perhaps I could modify it to be ambidextrous.

I’d like to further tweak the python coding as there are a couple of areas that I’m not too happy with. Ultimately it needs a good few workouts in both handheld and trailcam modes to see what needs work.


element manufacturer supplier link
Screen adafruit Pimoroni×320-3-5-tft-touchscreen-for-raspberry-pi-pi-2-and-model-a-b
Proto boards adafruit modmypi
IR filter ebay  electronics_lee
IR lamp ebay xrst_511
Lamp relay ebay puretekuk
Power convertor – batt borg modmypi
JST connectors modmypi
buttons maplin maplin



Handheld Infra Red Camera code

Motion detect code:

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

# Set pins to BOARD

#PIR setup
#set up PIR pin
PIR_PIN = 15
#set PIR pin as input

#relay setup
# Set relay pin
# Set relay pin as output

				GPIO.output (GPIO_LIGHT_ON,1)
				print "light on"
				while GPIO.input(PIR_PIN)==1:
					GPIO.output (GPIO_LIGHT_ON,0) 

print "PIR Module test press Ctrl+C to exit"
print "ready"

	GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
	while 1:
except KeyboardInterrupt:
				print "quit"

Main button control code:

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import os
import sys
from time import sleep
import subprocess as sp

# Set pins to BOARD

#set up BUTTON 1 - RED
#set PIR pin as input

#set up BUTTON 2 - GREEN
#set PIR pin as input

#set up BUTTON 3 - YELLOW
#set PIR pin as input

#set up BUTTON 4 - BLUE
#set PIR pin as input

#set up BUTTON 5 - BLACK
#set PIR pin as input

#set up pin 12 for PWM (screen control)
GPIO.setup(12, GPIO.OUT)
#GPIO.PWM(12, 1000)
pwm = GPIO.PWM(12, 1000)

# set record states
rkd_state = 0
rkd_state_blue = 0
rkd_state_black = 0
rkd_state_green = 0

# set filter pins
Motor1A = 16
Motor1B = 13
Motor1E = 7

# set up the variables for running motion detect
extProc = 1

				os.system('sudo poweroff')

				global rkd_state_green
				global extProc
				if rkd_state_green == 0:
					print "Green on"
					os.system('echo "motion_enable on" > /home/pi/pikrellcam/www/FIFO')
					extProc = sp.Popen(['python','']) # runs 
					status = sp.Popen.poll(extProc) # status should be 'None'
					rkd_state_green = 1
				elif rkd_state_green == 1:
					print "Green off"
					sp.Popen.terminate(extProc) # closes the process
					status = sp.Popen.poll(extProc) # status should now be something other than 'None' ('1' in my testing)
					os.system('echo "motion_enable off" > /home/pi/pikrellcam/www/FIFO')
					rkd_state_green = 0	
				global rkd_state
				if rkd_state == 0:
					os.system('echo "record on" > /home/pi/pikrellcam/www/FIFO')
					rkd_state = 1
				elif rkd_state == 1:
					os.system('echo "record off" > /home/pi/pikrellcam/www/FIFO')
					rkd_state = 0
				global rkd_state_blue
				if rkd_state_blue == 0:
					rkd_state_blue = 1
				elif rkd_state_blue == 1:
					rkd_state_blue = 0

				global rkd_state_black
				if rkd_state_black == 0:
					rkd_state_black = 1
				elif rkd_state_black == 1:
					rkd_state_black = 0		
				GPIO.add_event_detect(BUTTON_RED_PIN, GPIO.RISING, callback=BUTTON_RED, bouncetime=300)
				GPIO.add_event_detect(BUTTON_GREEN_PIN, GPIO.RISING, callback=BUTTON_GREEN, bouncetime=300)
				GPIO.add_event_detect(BUTTON_YELLOW_PIN, GPIO.RISING, callback=BUTTON_YELLOW, bouncetime=300)
				GPIO.add_event_detect(BUTTON_BLUE_PIN, GPIO.RISING, callback=BUTTON_BLUE, bouncetime=300)
				GPIO.add_event_detect(BUTTON_BLACK_PIN, GPIO.RISING, callback=BUTTON_BLACK, bouncetime=300)
				while 1:
except KeyboardInterrupt:
				print "quit"