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.
CLICK FOR DXF FILE: Cut these parts from 1/8" wood, 1/4" wood, and thin acrylic.
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.