Animal motion detection : an Arduino project for photography

Hungry (angry?) pigeon
A real angry bird.

I'm going to talk to you about a simple, yet fun project I did last year : an animal detection device that could control my DSLR and take pictures.

I thought that it would be a fairly simple thing to do with Arduino and a motion sensor. I chose to use an IR sensor because they are very cheap and efficient, and bought the PIR sensor from parallax. It just has a single bit output, so it's super simple to use with an Arduino. It is very sensitive and fits well this application. The only bad thing about it is that it has an incorporated LED that turns red every time a motion is detected, which is probably useful for most use cases, but not in my situation ๐Ÿ˜€ I just cut the circuit track leading to the LED so it wouldn't turn red anymore.

Motion detection on Arduino
The motion sensor and its 3 pins : The one bit output, VCC, and GND.

Once we have the information about whether ย a living thing is in front of the sensor or not, we need to control the camera to trigger it. This part really depends on what kind of camera you own, if it can accept a remote control or not. If it doesn't, one way to trigger it is to use a servo motor to press the shutter button (with the Arduino Servo library), but the problem might be the noise it generates when moving, which could scare the animals. My DSLR ( a ย Canon 550D) has a jack input for remote control which makes it really easy to command. What you will need is a standard 2.5mm stereo male jack, some wires and that's it!

Canon Remote Controller Wiring (2.5mm mini-plug and N3 plug)

You just need to solder the 3 wires to the jack connector. One for the ground, one for the focus command, and one for the trigger command. To focus, just put the focus contact to the ground; and it works the same way to take a picture.

So how to control the camera with an Arduino?

We will use two digital ports of the Arduino to control trigger and focus of the camera. When these outputs will be set LOW, they will fire the action they are supposed to create (focus or trigger). When set HIGH, nothing will happen. To protect the camera, you should put a resistor between the outputs and the camera (I used 2.2K resistor) just to make sure no current goes into the DSLR.

The final schematics look like this:

The very simple schematics of the breadboard circuit. The LED is simply there to control if everything is working (on when something is detected). You can click for the full size image.
The very simple schematics of the breadboard circuit. The LED is simply there to control if everything is working (on when something is detected). You can click for the full size image.
Motion detection on Arduino
The real life schematics ๐Ÿ™‚

The program will be pretty simple too : if something is detected, take a picture and turn on the control LED ๐Ÿ™‚

Here it is:


#include <Camera.h> // The Camera library makes it easier to control a DSLR

/********PINS*********/

int PIR_Pin = 3; //the digital pin connected to the PIR sensor's output
int LED_Pin = 2;
int focusPin=6;
int shutterPin=7;

/********VARIABLES****/

int idletime =0; // The time since last picture
int lastshot=0; // The millis() when the last picture was taken
int burstInterval=5000; //The time between pictures when motion is on
int calibrationTime = 30; // The sensor calibration time (so we don't get false positives when we start the Arduino up)
long unsigned int lowIn;//the time when the sensor outputs a low impulse
long unsigned int pause = 2000;// The time necessary for the motion to be gone after the sensor has gone to a LOW state
boolean lockLow = true; // goes to false when a motion is detected
boolean takeLowTime;
boolean burst=false;// burst mode indicator
Camera* eos; // a pointer to our DSLR

void setup()
{
 eos =new Camera(focusPin,shutterPin);
 Serial.begin(9600);
 pinMode(PIR_Pin, INPUT);
 pinMode(LED_Pin, OUTPUT);
 digitalWrite(PIR_Pin, LOW);

 //Sensor calibration
 Serial.print("Calibrating sensor ");
 for(int i = 0; i < calibrationTime; i++){
 Serial.print(".");
 delay(1000);
 }
 Serial.println("SENSOR READY");
 delay(50);
}

void loop()
{

 if(digitalRead(PIR_Pin) == HIGH){ //If a motion is detected
 if(idletime>30000){ // If the camera is in sleep mode
 (*eos).TriggerFocus(); // wake up the camera
 idletime=0;
 }

 digitalWrite(LED_Pin, HIGH); //signal that a motion is detected
 if (burst){ //Once the motion has been detected and a picture taken, we go into this mode to keep taking pictures every 5s until the motion ends
 delay (burstInterval);
 (*eos).TriggerShutter();
 lastshot=millis();
 idletime=0;
 }
 if(lockLow){
 lockLow = false;// We enter in "motion" mode
 (*eos).TriggerShutter();// We take a picture right away
 delay(1000);
 (*eos).TriggerShutter();// We take a second picture 1s later
 delay(2000);
 lastshot=millis();
 idletime=0;
 burst=true; //Now we go in burst mode,ie picture will be taken every 5S
 }
 takeLowTime = true;
 }
 if(digitalRead(PIR_Pin) == LOW){ //If mothing is detected
 digitalWrite(LED_Pin, LOW); //Turn off the LED
 if(takeLowTime){
 lowIn = millis(); //save the time of the transition from high to LOW
 takeLowTime = false; //make sure this is only done at the start of a LOW phase
 }
 idletime=millis()-lastshot;
 burst=false;

 if(!lockLow && millis() - lowIn > pause){ // If there has been more than 2000ms inactivity, we exit the motion mode
 lockLow = true;
 }
 }

}

The Camera library is included in the project. It is a very simple library that avoids you to code the boring stuff (like setting LOW the trigger output, then HIGH again etc ๐Ÿ™‚ )

To host the electronics and the camera, I built a wooden box big enough to put everything in easily, with a lid so It could be weather resistant. Here are the pictures

Motion detection on Arduino
You can see the PIR sensor, and a layer of tape around it. The goal is to make its FOV a bit narrower, so it only fires when the animal is in the camera frame.
Motion detection on Arduino
I used an old remote control from a broken RC helicopter (RIP ๐Ÿ™ ) to power the Arduino. You can obviously put anything you want in here ( staying in the 7-12V range is recommended )
Motion detection on Arduino
Top view of this masterpiece of engineering.
Motion detection on Arduino
The camera is held here with its tripod mounting hole. You can also see the stereo jack.
Motion detection on Arduino
The lid. The foam layer is there to protect from the rain.
Motion detection on Arduino
The whole thing, with the 550D and the Sigma 10-20mm. The bungee cords can be used to set the device in a tree.

This is obviously far from being perfect, the DSLR shutter noise scaring most of the animals away after the first picture, and the size and weight of this thing making it hard to place anywhere you'd want ๐Ÿ˜€

On the other hand, it does work pretty well and can take some fun pictures.

Cat eating a pizza

Hungry birds.

Hope this can give little help to those of you looking for ways to photograph animals ๐Ÿ™‚