Jamie's Blog

Lessons from a life of startups, coding, countryside, and kids

How to Build a Raspberry Pi Camera Trap

05 20140814123021 01

I started with a low-tech footprint tunnel to get proof of our local hedgehogs but that was never going to be enough. I had a Raspberry Pi sitting in a drawer that wasn’t getting much love so, after a little googling a Raspberry Pi Camera Trap, aka a ‘PiTrap’, was born.

The idea is to put a Raspberry Pi in a waterproof enclosure, use the NoIR picam (which let’s in infra-red light) and use a cheap IR lamp to light the scene. Then we can use the motion application to take photos / videos only when there’s movement in the scene

04-20140705200059-00

Materials

The Drybox was probably the most expensive part of this setup but you can probably hack together a cheaper alternative. It’s pretty neat though because I can put all the electronics safely in there and the picam sits neatly under the lid sheltered from any rain. Having said that, I don’t use it if I know we’re expecting heavy rain.

IR Lamp I just grabbed a cheap IR lamp from eBay for about €6. It doesn’t come with a power source though so you’ll need to find a suitable one. I used an old power supply from a battery charger that had the requisite 12V/1W and appropriate connector.

Put all the ingredients in the box and stir.

I found that the flat camera cable fitted perfectly through the Drybox slot and I just mounted the picam under the lid with some Blu-tack. If you have a longer cable you could mount the camera in a different position. Then you can position the box near some food or water. I’ve even started mounting it on a fence.

IMG_6313

IMG_6315

IMG_6317

IMG_6320

Software setup

Next, we need to make the hardware do something. I’ll assume you have Raspian already installed (if not, do it now)

First, we need to install motion but the default motion package isn’t compatible with the Raspberry Pi camera. Install the dependancies:

sudo apt-get install -y libjpeg62 libjpeg62-dev libavformat53 libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev

Then grab the patched motion app from here

wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz
tar zxvf motion-mmal.tar.gz

You can run it like this by supplying a configuration file

./motion -n -c motion-mmalcam.conf

The configuration file that I use is here.

Here’s a couple of things that I changed:

  • threshold 10000. I made it less sensitive (previous it would capture flies and slugs moving in the distance!). You can try adjusting this number though
  • ffmpeg_output_movies on. I turned on video recording
  • ffmpeg_video_codec msmpeg4. I changed the video encoding by trial & error and this one seemed to work for me when playing back in VLC

Next we need to make motion auto-start when the pi is turned on.

In Operation

If everything is working correctly, operation is as simple as plugging it in and walking away. Getting the images off is as simple as using rsync or scp. The IR camera gives a distinct ‘pinkish’ hue in daylight so it’s probably best just to convert them to Black & White.

IMG_6333

IMG_5208

Hedgie!

First Hedgie

And there it is again

05-20140814123021-01

Even birds!

04-20140706094530-00

Debugging

Sometimes it can be difficult to figure out if the motion capture is really working because it can be very sensitive. I found it helpful to turn on locate_motion_mode and set locate_motion_style redbox so that a line will be drawn around the area of movement.

Debugging motion

Enhancements

  • Live view. You can get a live view from motion at http://:8081 which would be incredibly useful for framing shots! Unfortunately I don’t get wifi in the corner of the garden and I’ve tried using a powerline adapter but I couldn’t get it working. I might try a Wifi extender at some point or even a little 3G Mifi modem.
  • Reconnect networking. The Pi won’t try to reconnect to a network if it doesn’t detect one at first. So when I bring it back inside it doesn’t automatically connect to the wifi. The interim solution is just to pull the wifi dongle and plug it back in but it’d be nice not to need to open up the drybox.
  • Keeping time. The Pi doesn’t have a clock battery so it requires a network connection to get the right time. Since I don’t get a network connection my clock is usually out of sync and it’d be nice to know exactly when Hedgie is calling
  • Focusing. The PiCam is a fixed focus camera that’s good from about 60cm from the subject. This can be difficult with small animals and small light source. Apparently a good solution is simply to use a lens from some cheap reading glasses
  • Flushing images to disk. The images don’t flush straight to disk so if you simply pull the plug on your Pi you’ll find lots of zero-byte images. Ideally I’d have a power switch for gracefully shutting down the Pi. Or have a network connection for issuing sudo poweroff
  • Turn off red camera LED. I know it’s possible although it really hasn’t bothered our hedgehogs
  • Frame rate. The video framerate is abysmal and I’m pretty sure it’s just due to low level of IR light