Category Archives: all

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.

Screen

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.

Buttons

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

Power

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.

Light

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)

Filter

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.

Links:
kiosk
https://github.com/elalemanyo/raspberry-pi-kiosk-screen
Pikrellcam
http://billw2.github.io/pikrellcam/pikrellcam.html
Start / stop python script from python
http://stackoverflow.com/questions/3054740/terminate-a-python-script-from-another-python-script

Results

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:  https://www.youtube.com/channel/UCBhztnk1gHhY_zLZJRJL_Rw

Improvements

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.

Supplies

element manufacturer supplier link
Screen adafruit Pimoroni https://shop.pimoroni.com/products/pitft-plus-480×320-3-5-tft-touchscreen-for-raspberry-pi-pi-2-and-model-a-b
Proto boards adafruit modmypi https://www.modmypi.com/raspberry-pi/breakout-boards/adafruit/adafruit-perma-proto-half-sized-breadboard-pcb-3-pack/?limit=100
IR filter ebay  electronics_lee http://www.ebay.co.uk/usr/electronics_lee?_trksid=p2060353.m2749.l2754
IR lamp ebay xrst_511 http://www.ebay.co.uk/usr/xrst_511?_trksid=p2060353.m2749.l2754
Lamp relay ebay puretekuk http://www.ebay.co.uk/itm/272272819712?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
Power convertor – batt borg https://www.piborg.org/ modmypi https://www.modmypi.com/raspberry-pi/breakout-boards/piborg/piborg-battborg/?search=batt%20borg
JST connectors modmypi https://www.modmypi.com/search/?search=jst
buttons maplin maplin http://www.maplin.co.uk/p/250v-15a-square-push-to-make-switch-red-ff98g

 

Gallery

Handheld Infra Red Camera code

Motion detect code:

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

# Set pins to BOARD
GPIO.setmode(GPIO.BOARD)

#PIR setup
#set up PIR pin
PIR_PIN = 15
#set PIR pin as input
GPIO.setup(PIR_PIN, GPIO.IN)

#relay setup
# Set relay pin
GPIO_LIGHT_ON = 11
# Set relay pin as output
GPIO.setup(GPIO_LIGHT_ON,GPIO.OUT)

def MOTION(PIR_PIN):
				GPIO.output (GPIO_LIGHT_ON,1)
				print "light on"
				time.sleep(10)
				while GPIO.input(PIR_PIN)==1:
					time.sleep(2)
					GPIO.output (GPIO_LIGHT_ON,0) 

print "PIR Module test press Ctrl+C to exit"
time.sleep(2)
print "ready"

try:
	GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
	while 1:
		time.sleep(100)
except KeyboardInterrupt:
				print "quit"
				GPIO.cleanup()

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
GPIO.setmode(GPIO.BOARD)

#set up BUTTON 1 - RED
BUTTON_RED_PIN = 37
#set PIR pin as input
GPIO.setup(BUTTON_RED_PIN, GPIO.IN)

#set up BUTTON 2 - GREEN
BUTTON_GREEN_PIN = 31
#set PIR pin as input
GPIO.setup(BUTTON_GREEN_PIN, GPIO.IN)

#set up BUTTON 3 - YELLOW
BUTTON_YELLOW_PIN = 33
#set PIR pin as input
GPIO.setup(BUTTON_YELLOW_PIN, GPIO.IN)

#set up BUTTON 4 - BLUE
BUTTON_BLUE_PIN = 32
#set PIR pin as input
GPIO.setup(BUTTON_BLUE_PIN, GPIO.IN)

#set up BUTTON 5 - BLACK
BUTTON_BLACK_PIN = 36
#set PIR pin as input
GPIO.setup(BUTTON_BLACK_PIN, GPIO.IN)

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

# 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
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)	

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

def BUTTON_RED(BUTTON_RED_PIN):
				os.system('sudo poweroff')

def BUTTON_GREEN(BUTTON_GREEN_PIN):
				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','motion.py']) # runs motion.py 
					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	
				
def BUTTON_YELLOW(BUTTON_YELLOW_PIN):
				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
					
def BUTTON_BLUE(BUTTON_BLUE_PIN):
				global rkd_state_blue
				if rkd_state_blue == 0:
					pwm.ChangeDutyCycle(0)
					rkd_state_blue = 1
				elif rkd_state_blue == 1:
					pwm.ChangeDutyCycle(100)
					rkd_state_blue = 0

def BUTTON_BLACK(BUTTON_BLACK_PIN):
				global rkd_state_black
				if rkd_state_black == 0:
					GPIO.output(Motor1A,GPIO.LOW)
					GPIO.output(Motor1B,GPIO.HIGH)
					GPIO.output(Motor1E,GPIO.HIGH)
					sleep(0.1)
					GPIO.output(Motor1E,GPIO.LOW)
					rkd_state_black = 1
				elif rkd_state_black == 1:
					GPIO.output(Motor1A,GPIO.HIGH)
					GPIO.output(Motor1B,GPIO.LOW)
					GPIO.output(Motor1E,GPIO.HIGH)
					sleep(0.1)
					GPIO.output(Motor1E,GPIO.LOW)
					rkd_state_black = 0		
try:
				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:
					time.sleep(100)
except KeyboardInterrupt:
				print "quit"
				GPIO.cleanup()

Elite dangerous controller

Whilst ordering some new arts for a couple of projects, I came across the zero delay arcade by Reyann.

board

It is intended for raspberry pi etc. controls for creating arcade consoles and appears to be USB plug and play so I thought I’d give it a go and make some additional controls for elite:dangerous.

dscn4619

This prototype has been lovingly crafted for the cardboard box that my delivery arrived in. No setup required and it just plugged in and worked.

The kit comes with the wiring loom and usb cable so it’s just a case of slipping the buttons on to the wires and off you go. It connects perfectly with the arcade buttons but not so well with the smaller tactile ones which required some structural blu-tak – a couple of mins soldering would fix it but these buttons are only temporary.

dscn4610

Windows sees it as a generic controller and E:D responds in a similar way, so there is no issue in assigning the buttons to the massive list of controls available (copy and save your key binds though. They’re very fickle).

The arcade buttons themselves are made by Sanwa and are spot on arcade quality – you can roll your finger on them like a real arcade machine and they feel like they could go on forever.

The joystick too is a Sanwa item and just oozes quality.

The larger buttons are (I think) from Adafruit. They are a bit more clicky than the arcade ones but very tactile. The 100mm HAL special in the middle is impossible not to touch and brings out the father dougal in everyone.

lit

They also light up, I’ve not tried this yet but it looks to be pretty straightforward and some of the online pictures make them look very effective (e.g. the HAL 9000 replica on adafruit )

dscn4616

For a prototype it works perfectly and the next step is to work on the case and assemble the required buttons. This was pretty much thrown together to see if the board worked however having played for a bit today the HAL9000 / pacman mashup has really grown on me so I think I’ll keep it!

dscn4617

In summary:

Controller board

By Reyann. mod my pi link: https://www.modmypi.com/raspberry-pi/arcade/arcade-controllers/zero-delay-arcade-usb-encoder-and-wire-set/?search=arcade

Arcade buttons

 By Sanwa – mod my pi link : https://www.modmypi.com/raspberry-pi/arcade/arcade-buttons/official-sanwa-arcade-button-black/?search=sanwa%20arcade%20buttons

Arcade stick

By Sanwa Mod my i link : https://www.modmypi.com/raspberry-pi/arcade/arcade-joysticks/official-sanwa-8-way-arcade-joystick-jlf-tp-8yt/?search=sanwa

Illuminated buttons.

By Adafruit. Mod my pi link : https://www.modmypi.com/electronics/buttons-and-switches/massive-arcade-button-100mm-red/?search=arcade%20buttons