anicam

Concept


The idea for this  current version is to condense down the functions from the previous versions in to a single board that fits on top of the raspberry pi zero W.

I also wanted to reduce the cost and complexity of the previous versions to increase the reliability and make it easier to use.

 

Hardware


Board

The main board sits between the various components and the pi, and is populated with jst connectors to make fitting it all together easier.

I’ve been running a version of this board that I built with the excellent  protozero board : https://www.modmypi.com/raspberry-pi/prototyping-and-breakout-boards/prototyping-boards-1027/protozero-prototyping-board-for-pi-zero and thought I’d have a go at putting together a circuit diagram on fritzing.

A few clicks later I had the first version of the board ordered from aisler.net – this was a bit of a mess, mainly as I managed to use an odd relay template but after a bit of tinkering I had this version printed.

 

Circuit diagram (click to enlarge):

 

I.C.

The heart of the board is a L293D IC. I chose this as it can reverse the polarity of a signal which is necessary to control the IR filter over the camera lens. Further information here: http://garysshed.co.uk/wp/?p=664

Relay

The L293D has 2 channels and I have used the second channel to control the relay. This is handy as it allows the GPIO pins to control a 5v relay with minimum effort or additional components (just a diode to prevent feedback) and provides an element of protection to the pi.

5v relays are easy to get hold of and cheap, and the minimal components mean that I can include the relay on the main board and not have to rely on per built boards.

Light

The light is a 5v, 10W Infrared IR 840-850nm SMD LED unit.  It draws about 2A current so I’ve connected it to the USB battery supply via the relay to enable it to be switched.

Previously I’ve used a ring of LED’s but this brighter and doesn’t require a 12v supply so the whole assembly is smaller.

It gets very hot, so it may get an upgrade with a heat sink in the future.

Infra Red filter

This filter is designed for use with security cameras and consists of a small plastic element that is pulled in front of the camera lens to prevent infra red light getting through.

When it is deployed it gives a more natural colouration however when it is removed it picks the light reflected by the infra red lamp giving better vision in the dark.

It works by sending a 5v signal for a fraction of a second to the actuator – one way it moves in front of the lens and the other retracts it.

The polarity of the signal is handled by the L293D chip with the following outcomes:

Filter off
Chip pin Pi pin Signal
1A 16 LOW
1B 13 HIGH
1E 7 HIGH
Filter on
Chip pin Pi pin Signal
1A 16 HIGH
1B 13 LOW
1E 7 HIGH
Filter stop
Chip pin Pi pin Signal
1E 7 LOW

Buttons

The buttons are simple and cheap ones from mod-my-pi https://www.modmypi.com/raspberry-pi/sensors-1061/buttons-and-switches-1098/12mm-domed-push-button-6-pack/?search=buttons but they’re decent quality and a normally open (push to make) type. They are current limited with a 220? resistor and the pin is pulled down to negative on the board so it is simple to put any button or even an LDR or similar with a jst soldered to it.

There’s no protection on these and I managed to fry my old test pi by plugging in the 5v supply to the light on the first board (stupid mistake, but it had a broken camera connector anyway).

PIR sensor

I’ve used an inexpensive HC-SR501 PIR (passive infra red) detector  datasheet

5v is supplied to the module and when it detects movement, it sends a 3v signal through the yellow wire which is detected by the pi on pin 15

There are adjustments on the sensor for trigger time, sensitivity etc which can be altered however it is set up in a configuration that seems to work.

Software


Pikrellcam

http://billw2.github.io/pikrellcam/pikrellcam.html

Once again this does all of the heavy work to include the actual motion detection and video conversion. I’ve used the in built ability to add on a button to the main web interface that links to my own webpage which alters button size etc so  it’s a bit easier for kids with touchscreen devices to use.

  1. Status
  2. Current date & time
  3. Main screen
  4. Buttons

 

Detect

A program called “detect” is running which is started via cron at boot. This program is a loop that performs the following actions:

  • Waits for a trigger from the PIR then turns on the light;
  • Reads the pikrellcam pipe to see if the light or filter buttons have been pressed on the web page
  • Reads the 2 x buttons and performs the shutdown / restart wifi commands

Link to code:  HERE

Results


General

I’ve not been able to give it a proper test with animals yet -I don’t have any secure outside space to use it anymore and when I’ve put it out in other gardens nothing has been about at night (as its a bit cold). It works well in low light with early risers and curious children so performs as well as the previous version in terms of triggering.

PCB

  • fritzing is really easy to use and handy for drawing up the circuit diagram.
  • It’s simple to turn the diagram in to a pcb, but you need to be careful with component choice to make sure they are easy / cheap to get.
  • Aisler are quick and  cheap  – 3 identical boards cost approx £20 which is less than buying the prototype boards. It’s also fun to get Emails from Felix keeping you updated on progress.

Lamp

  • The lamp gets REALLY hot! Not so bad when it is on sporadically outside but running for long periods wil require a heat sink.
  • The top line of emitters failed on my first lamp during testing. This could be down to getting hot, defective lamp or mishandling (it was messed about with a lot).
  • the lamp is an 840-850nm version which works well with the pi camera. I’ve since bought 3 x smaller (3W) ones at 740nm, 850nm & 940nm to test out which is best.

Power

I’ve used an easyacc 10400mAh power supply – link which works fantastically in normal operation, giving a good 10 to 12 hours of power for both the pizero and lamp.
It’s got 2 connections – 1A & 2.1A with the lamp requiring all of the 2amp supply to function correctly.
The only issue with the power supply is its size. It’s pretty small for its output however it is now the largest component in the whole setup, so it dictates the overall box required.

Internal access

There is nothing more irritating than the insides of the camera flapping about and disconnecting so I’ve screwed everything down in this version. All worked beautifully until I needed to get to the sd card and had to unscrew and disconnect the board so I’ve bought an SD card extender which should do the trick.

The battery is now easily accessible via a flap in the bottom of the birdbox and the connections, while fiddly to put in, are nice and stable.

Pictures


Old version

The is the original version with the ring of IR LED’s and the main control board with separate ones for the relay and the filter control. Not shown are the 9v battery to power the light (supposed to be 12v but needed the space!),2 buttons  and the main powerpack.

New version

The is the new version with the pi zero attached to the back and all of the elements connected. The usb plug goes to the 2.1A output from the battery with the pi powered by the 1A output (lead not shown). It is a considerable saving in space & complexity over the old version with fewer areas to break.

Comparision

This picture shows the evolution of the camera board:

  1. simple version for pi V1 to break out the gpio pins. Very bulky and needed the wires kept falling off of the pins.
  2. this version is again for the pi V1 but introduced a couple of buttons on the board
  3. the final pi V1 board with pins that allowed the various elements (relays, buttons etc) to be positioned in more convenient places.
  4. Explorer phat version that I just couldn’t get to work well enough. It was excellent for the filter switching but the relay control needed a bit more oomph (or a better programmer!)
  5. The populated and working previous version.
  6. prototype of the new board constructed on an excellent protozero board. Loads of messy wires and my crummy soldering underneath so plenty of points of failure.
  7. PCB version 1 – i managed to mess up the relay type and didn’t want to use expensive ones so moved on to the current version.
  8. Current version, populated and unpopulated.
  9. Current version fully connected.

Housing

Group shot of the cameras, from left to right:

  • Handheld version – as described on previous posts.
  • The current trailam. The lamp is in the top and camera below., the pir is now fitted under the camera (not shown in this pic) and the buttons are inside.
    Stupidly I ended up putting the smallest version of the trailcam in the biggest box I had! In reality it could easily fit in the smallest box with space to spare.
  • normal colour trailcam. This is just pikrellcam and an old camera and pi model A used in daylight.
  • old version – this has got the big light around the camera and the PIR in the little window. The button is cunningly positioned behind the door.

Code


Custom index page

The Anicam is controlled with a custom php page that uses the feed from pikrelcam.

 

<?php
//ini_set('display_errors',1);
//ini_set('display_startup_errors',1);
//error_reporting(-1);

require_once(dirname(__FILE__) . '/config.php');
include_once(dirname(__FILE__) . '/config-user.php');
include_once(dirname(__FILE__) . '/config-defaults.php');

function time_lapse_period()
{
$tl_status = "../../.pikrellcam/timelapse.status";
$f = fopen($tl_status, 'r');
$tl_period = 1;
if ($f)
{
$input = fgets($f);
$input = fgets($f);
sscanf($input, "%d", $tl_period);
fclose($f);
}
return $tl_period;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo TITLE_STRING; ?></title>

<script src="js-css/jquery.min.js"></script>
<link rel="stylesheet" href="js-css/pikrellcam.css" />
<script src="js-css/pikrellcam.js"></script>
<script src="js-css/expandable-panels.js"></script>
<link rel="stylesheet" href="js-css/expandable-panels.css" />
</head>

<?php
if (isset($_GET["hide_audio"]))
{
$show_audio_controls = "no";
config_user_save();
}
if (isset($_GET["show_audio"]))
{
$show_audio_controls = "yes";
config_user_save();
}

echo "<body background=\"$background_image\" onload=\"mjpeg_start();\">";
echo "<div class=\"text-center\">";
echo "<div class='text-shadow-large'>";
echo TITLE_STRING;
echo "</div></div>";
echo "<div class=\"text-center\" style=\"color: $default_text_color; font-size: 1.4em;\">";
echo "<img id=\"mjpeg_image\"
alt=\"No preview jpeg. Is pikrellcam running? Click: System->Start\"
style=\"border:4px groove silver;\"
onclick=\"image_expand_toggle();\"
></div>";

echo "<div class=\"text-center top-margin\">";

?>

<input type="image" src="images/bigstop.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('record off')"
>
<input type="image" src="images/bigpause.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('pause')"
>
<input type="image" src="images/bigrecord.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('record on')"
>
<input type="image" src="images/bigtakestill.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('still')"
>
<input type="image" src="images/bigmotion.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('motion_enable toggle')"
>
<input type="image" src="images/mainmenu.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="location.href='index.php'"
>
<br>
<input type="image" src="images/lampon.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('lampon')"
>
<input type="image" src="images/lampoff.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('lampoff')"
>
<input type="image" src="images/filteron.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('filteron')"
>
<input type="image" src="images/filteroff.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="fifo_command('filteroff')"
>
<input type="image" src="images/videos.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="location.href='media-archive.php?mode=media&type=videos'"
>
<input type="image" src="images/stills.png"
style="vertical-align: bottom; margin-left:9px; margin-right:0px;"
width="100" height="100"
onclick="location.href='media-archive.php?mode=media&type=stills'"
>
</div>

<div id="container" class="top-margin">


<?php
if (file_exists("custom.php"))
{
include 'custom.php';
}
?>
</div>
</body>
</html>

 

This page basically hijacks the pikrellcam pipe and sends its own information through it to the python program.

detect.py

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import os
import sys
 
# Set pins to BOARD
GPIO.setmode(GPIO.BOARD)

# Setup pins for relay
Motor2A = 36
Motor2B = 32
Motor2E = 37
 
GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)
 
 
# Setup pins for filter
Motor1A = 16
Motor1B = 13
Motor1E = 7
 
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
 
  
#PIR setup
#set up PIR pin
PIR_PIN = 15
#set PIR pin as input
GPIO.setup(PIR_PIN, GPIO.IN)

#set up BUTTON A pin
BUTTON_PINA = 31
GPIO.setup(BUTTON_PINA, GPIO.IN)

#set up BUTTON B pin
BUTTON_PINB = 33
GPIO.setup(BUTTON_PINB, GPIO.IN)

#below is the function MOTION which is called by the main program at the bottom
def MOTION(PIR_PIN):
				print "motion detected"
				# double check
#				time.sleep(2)
				while GPIO.input(PIR_PIN)==1:
					print "confirmed"
#					time.sleep(1)
					# switch relay on
					GPIO.output(Motor2A,GPIO.HIGH)
					GPIO.output(Motor2B,GPIO.LOW)
					GPIO.output(Motor2E,GPIO.HIGH)
					print "light on"
					# wait 20 seconds - can change this
					print "waiting"
					time.sleep(10)
					# check the PIR again, if its on wait for another 20 seconds
					while GPIO.input(PIR_PIN)==1:
						print "still waiting"
						time.sleep(5)
					#turn off the relay
					GPIO.output(Motor2E,GPIO.LOW)
					print "motion stopped"
					print "Light off" 
				
def BUTTONA(BUTTON_PINA):
				print "button A detected"
				time.sleep(1)
				while GPIO.input(BUTTON_PINA)==1:
					print "confirmed"
					os.system('sudo poweroff')

def BUTTONB(BUTTON_PINB):
				print "button B detected"
#				os.system("sudo ifup --force wlan0")


	
# ---MAIN PROGRAM STARTS------
# message is prined at startup & gives the PIR sensor 2 seconds to settle down before the program starts.
				
print "PIR Module test press Ctrl+C to exit"
#turn off the relay
GPIO.output(Motor2A,GPIO.HIGH)
GPIO.output(Motor2B,GPIO.LOW)
GPIO.output(Motor2E,GPIO.LOW)
print "Light off"

time.sleep(1) 
 
print "Sending signal"
GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor1E,GPIO.HIGH)
 
time.sleep(0.1)
 
GPIO.output(Motor1E,GPIO.LOW)

print "Filter off"

time.sleep(5)
print "ready"

# this runs all the time, checking to see if the PIR or buttons are triggered
try:
	GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION, bouncetime=300)
	GPIO.add_event_detect(BUTTON_PINA, GPIO.RISING, callback=BUTTONA, bouncetime=300)
	GPIO.add_event_detect(BUTTON_PINB, GPIO.RISING, callback=BUTTONB, bouncetime=300)
	while 1:
		time.sleep(100)
							  
# stop program if ctrl+C is pressed
except KeyboardInterrupt:
				print "quit"
				GPIO.cleanup()

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