Hedgehog Cam

Figure 1 : Hedgehog

Whats that in the garden, is it a rat, is it a cat, no its a hedgehog :) (Videos). Looks like i have a hedgehog, so decided to put together a night vision Raspberry Pi system to see what it was doing in the garden whist i was asleep. For this project went with a Pi camera rather than a web camera, mainly because you can get an all in one solution for about £18: a infra-red camera + infra-red illuminators, as shown in figure 2. An added bonus is that these cameras (those using the Pi camera interface) tend to have a better resolution and frame rate than your average USB web camera. To avoid having to watch many hours of video also needed to implement some sort of motion triggered capture. A few different alternatives, but decided to go with the OpenCV library and python (LINK), this allowed a little more flexibility and an opportunity to play around with some image processing stuff.

Figure 2 : Infra-red camera module

Table of Contents

Version 1: inside camera
OpenCV: install and code
Version 2: outside camera

Version 1: inside camera

In a previous job i worked with underwater electronics and the one thing i learnt whilst i was there was that water always has a way of getting into equipment, either through condensation, or those tiny cracks and gaps in the sealing you missed. Therefore, to avoid these joys initially i went for an inside solution that looked through a window onto the garden. As always went for the standard 3D printed box (LINK). Camera housing was based on this design (LINK), slightly modified to include an infra-red illuminator ON/OFF switch. The camera module does have an on-board LDRs (one per module) to turn off the LEDs during day light hours, circled in red shown in figure 4, but i decided to add an extra manual switch just to make sure, reason, these LEDs do take a bit of current which i thought may result in boot issues i.e. brown-outs, especially for the lower current rated power supply modules. Also added an LCD display as this camera will operate headless (without a keyboard and display), therefore, needed to know its IP address to allow me to SSH in. The LCD housing was based on this design (LINK). The assembled system is shown in figure 3.

Figure 3 : inside Infra-red camera system

Figure 4 : LDR sensors

The original camera shown in figure 2 powers the two side LED modules via the two mounting bolts i.e. clamps together the two PCB pads. I separated the LED pcb from the camera pcb using fibre washers (red washers shown in figure 5). The 0V connection is made via the metal bolts, perhaps not the best conductor, but worked. The +5V connections are insulated using a plastic bolts, i then soldered on small wire links (made from ribbon cable) to the +5V and LED module pads, these wires are then soldered to a toggle switch mounted on the back of the camera, shown in figure 5.

Figure 5 : Infra-red LED ON/OFF switch

To test if the camera was working, first enable the camera interface by running sudo raspi-config, selecting Interface options => P1 Camera => Yes. Then run raspivid -t 100, if all is well you will see a short video stream from the camera. Originally i thought of using VNC/remote desktop, unfortunately the camera software writes directly to the Pi display memory/driver so it does not appear on the remote desktop. When operated in a small-ish room the infra-red illuminators performed perfectly, however, when i tried to use these through a window i found that i got a large amount of reflection / glare, effectively making the window a mirror i.e. captured a video of the camera recording the camera, rather than the outside, as shown in figure 6. This problem was helped a little by ensuring that the surfaces of the window were perfectly clean, to minimise the amount of light being scatters back (a little tricky with double glazing). Also angling the camera such that it was not perpendicular to the glass also helped i.e. reflected light was reflected away from the camera. However, after a lot of testing it became clear that for this application having the illumination source near the camera was not going to work.

Figure 6 : reflected light problem

To overcome this problem bought an infra-red illuminator and a small tripod from Amazon, as shown in figure 7, also added a 3D printed mount for a USB web camera. Note, had to put some black insulation tape over the USB camera's power LED, as again this reflected light was picked up by the camera. The IR illuminators are powered from an external 12V supply. Again, these module have an LDR sensor to automatically turn off the LED array during daylight hours, but to allow the Pi to control this power added the relay board shown in figure 8. This board is powered from +5V from the Pi, giving you four independent high current switches.

Figure 7 : IR illuminators

Figure 8 : Relay board

As the Pi will be running headless most of the time i included a 16x2 LCD displays, the same module used in the PiTime project (LINK), again to save GPIO pins used a I2C adaptor board, the red board in figure 9. Also to allow a safe shutdown added a push button, using the same software as the PiCase project (LINK), a little messy but also added some CR de-bounce, using the internal pull-up on the Pi GPIO and a 4.7uF cap and 100 ohm resistor (to limit discharge current), circuit diagram in figure 10.

Figure 9 : LCD display and shutdown button.

Figure 10 : de-bounce circuit

Connections to the GPIO header is via a 10 way IDC ribbon cable connector, as shown in figure 11, the two edges have been filed flag then super-glued together to allow it to fit in-between the other pins. The assigned function for each pin is as follows:

Figure 11 : GPIO pins

Figure 12 : wiring

OpenCV: install and code

Unlike the sunflower camera built in a previous project (LINK) which took a picture every 2 minutes, this camera will be motion triggered, so need some software to detect movement. After looking around went for the OpenCV library (LINK). At the time of this project the apt-get repository version of the openCV library was a little out of date. As the online documentation was aimed at version 3.3+ decided to install the latest version from source, i based my install on these web pages: (LINK),(LINK), missed out a few bits. An additional problem is really need a 16G card, 8G will just about due, but you need to clear some space:

sudo apt-get purge wolfram-engine
sudo apt-get purge libreoffice*
sudo apt-get clean
sudo apt-get autoremove

This frees up a good 1GB+, Next, update and install base software, compiler and make tools:

sudo apt-get update
#sudo apt-get install build-essential (note, should be installed)
#sudo apt-get install python-dev (note, should be installed)
sudo apt-get install cmake cmake-qt-gui
sudo apt-get install libgtk2.0-dev

Then download/clone the OpenCV source code from: (LINK).

git clone https://github.com/opencv/opencv/

To compile, install and test ...

cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make install

Alternatively you can use the graphical interface cmake-gui. You can compile using multiple cores, but, this locks the Pi up a bit, i just left it running over night, its always going to take a little while on a Pi (around 7 hours).


Figure 8 : Wifi module + external USB disk

Figure 8 : final system

Version 2: outside camera

Creative Commons Licence

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Contact details: email - mike.freeman@york.ac.uk, telephone - 01904 32(5473)