Thursday, October 25, 2012

Motion Detection via Flood Light

In our project, the Arduino homebrew security system, we've tried a few variations of motion video capture, Linux Motion, and ZoneMinder.  After a few months of trial and error, I've learned a few things :
  1. Neither package can keep up with recording video at a high enough frame rate (ended up being about 3 frames per second, a far cry from what the camera does with it's 1280x1024 at 15 frames per second, or a standard 30 frames per second at a lower resolution).
  2. Both ZoneMinder and Motion worked the motion detection a little bit differently, and neither one would work for what was needed - a breeze and a tree (or tree shadow) would trigger motion, and relentlessly alert me.  Being desensitized to the motion alerts had the wrong effect, causing it to be worthless for what was intended.
With those two issues, I couldn't use either Motion or ZoneMinder.  I needed something better.  Then I remembered the "anti-cat" video :


In the video, he uses a motion sensor that has an LED that activates when motion is detected.  I'd suggest the PIR sensors - you will have fewer false alarms, and most modern ones are at least IR, so most will do.  Just make sure they have the LED that is active.

I opted for a full flood light.  The problem with these are most have daylight sensors now, and you don't want to the use 120 volt relays on that, because then your motion would only be detected at night.  This is why you need one with an LED.  The LED's are usually active even during daylight hours.

The first step is taking apart the sensor itself, and locating where the LED connects to the circuit board.  Once you have this, you might need to see what kind of voltage is running across those two lines when motion is detected just to be safe.  You will connect those two lines to a relay (why you may need to see the kind of voltage), and that is simply adding a "switch".  You can then wire this into the arduino wiring you used for the doors.

This would give you the following events on the arduino (and the alarm server we coded in http://www.silverhawk.net/2012/04/arduino-assembly-and-testing.html) :
  • MOTION DETECTED
    This event should trigger a motion capture from any video sources you want tied to this camera.
  • ALL CLEAR
    This event should stop the motion capture.  Other possibilities are to cause a temporary sleep (e.g. you want to capture an extra 30 seconds of video before you stop the capture).
Since we had coded the alarm server to be able to call an external command, we can easily interface a tool that immediately starts recording video on a simple command.  The stop is easily done, too, by issuing a command that sleeps and then kills the previous command.

However, there is a problem with that.  If you have multiple sensors tied to a single camera, and one clears (but the other continues to sense motion), your recording would have stopped when the first sensor goes to an all-clear.  The trick here is to use a count of sensors using a camera.  For example, when we start recording, we should open up a count file.  If it contains a 0, then we start the recording.  No matter what it contains, we should increment the count and write it back out.  When we stop recording, we should open the file and decrement the count.  If it's 0 when we're done, kill the recording session.

If you want to get really fancy, you can automatically start transcoding the video and uploading it to an offsite location (very handy, in case someone steals your video server from the house).  Still, I needed a simple hack to get motion detection operational as quickly as I could, and the camera I had included motion detection capabilities using the on-camera motion detection.  That turned out to be the easiest method to use, was the quietest (fewer false positives after tweaking), and had the best quality of video automagically uploaded to the video server.