Thursday, December 27, 2012

Motion Detector Disco Ball


For Xmas this year, I wanted to make something interesting for my niece and nephew.  They are twins, 2 1/2 years old and very much aware of their surroundings.  I watched them interact with a fountain in Bethany Beach that squirted water randomly, and it gave me the idea that a system where they could interact with their surroundings and cause a change might be cool for them, even at their age.  Having just ordered a couple of the Sparkfun PIR sensors, I thought it would be easy and quick to interface this sensor with a relay to turn on/off a 120V supply.  I had done this earlier with the temperature controller, and this time around I focused on a clean, quick box.  I already had a small board with a 120V/5V relay installed, so this project was really just a matter of making an elegant box with places to mount the PIR, an electric socket, an arduino, and my relay board.  The programming also took some work and practice, because the PIR had a tendency to trip itself if a delay wasn't added and a little signal processing done.

The disco ball was ordered from Amazon.  It actually does the job pretty well, but it didn't work when it arrived and I had to open up the bottom to find that one of the wires connected to the power adaptor socket had come lose during transit and needed to be resoldered on.

Click here for the disco ball from Amazon.

I used a simple outlet from Lowes, which conveniently had two 6-32 tapped holes around the socket.  The one I used was also tamper resistant which is good for toddlers.

This is the socket I used.
One final twist to this project was that I wanted the box to  be functional in some way beyond just being a box.  My sister had asked for picture frames on her Xmas list, and I figured it would be very easy to add in space for a 5"x9" print under a pane of acrylic on the top of the box.  I  used SolidWorks to quickly lay out dimensions for a box that would house all the components and have space on top for the picture.

Here's the box, assembled with the components on the inside.  The PIR sensor was mounted on the right hand side using some M2 screws and bolts.  The Arduino is mounted to the base of the box using two M3 screws and nuts.  I cut off the flanges on the outlet, leaving just the 6-32 holes and affixed this on another side of the box.  That same side has an opening for a two-prong power cord.  The hot wire on the powercord  is interrupted by the relay, then runs to the outlet.  The neutral wire runs directly to the outlet.

I was really pleased with the box I made for this project.  It fits all the components elegantly and secures them in place, virtually eliminating any type of mechanical error.  You can download the DXF files below.

CLICK FOR DXF FILE:  Cut these parts from 1/8" wood, 1/4" wood, and thin acrylic.







The finished box has the pane of acrylic on top to house a photo.  The top of the box is easily removed by removing the two screws which go through the side panels into two small pieces of wood attached to the top panel.

The pictures below show the clean installation of the outlet, the Arduino, and the PIR sensor.  For the sake of safety, I coated all the exposed 120V wires with a healthy layer of silicone caulk (non-acid type) to insulate them from the many metal screws exposed on the exterior of the box.












Getting the PIR sensor to do what I wanted took a little bit of work.  I have an ad-hoc solution which is probably not the most elegant, but it works pretty well.  Initially I simply tied the relay directly to the PIR sensor, so when it dropped low (indicating IR motion), the relay turned on.  This led to crazy oscillations of the relay and was pretty useless.  Next I tried using an array to take 20 sequential readings.  Rather than use an average, I just added up all the readings.  If the PIR was really tripped, then most of the readings in a set of 20 would be zero, and so the sum would be close to zero and far from 20.  I played with a threshold value, and ended up comparing the sum of the twenty readings to two.  If the sum was less than two, the relay was tripped, otherwise it was off.  This smoothed out the relay trips, but it still wasn't reliable.

I realized that the behavior I wanted was when the threshold was reached, the relay should just stay on for some period of time, then reset.  So rather than tie the relay directly to the sensor, I have it setup so that the sensor trip resets a timestamp.  If the difference between the timestamp and the current time is less than seven seconds, the relay is on.  After seven seconds, the relay is turned off.  Finally, I had to provide a two second buffer so that the relay change couldn't retrip the PIR (I was finding for whatever reason that every time the relay turned off, the PIR would drop low again turning the relay back on).  So the timestamp cannot be reset until nine seconds after the relay is tripped.

This is the final version of the code.

Here's the motion detector box in action.


And another video to show that it actually detects motion!!!



Finally, here's the schematic showing how everything is attached.  A warning for the PIR sensor wiring... the color coding on the wires from the PIR sensor is not the normal system... make sure to read the Sparkfun page.