It was 2:00pm the afternoon before Halloween and I badly wanted to add motion triggered sound effects to the trick-or-treat experience. Not wanting to brave the crowds making a last minute dash for Halloween supplies, I headed to my project box and was able to find all the components I needed to make a sound effect unit using a passive infra red sensor, a tablet, bluetooth speaker and an electric imp WiFi microcontroller. It is certainly cheaper to go and buy a motion detecting sound effect generator from a store, but where's the fun in that?! While the cost and technologies used for this project are firmly in overkill territory, the actual cost to me was $0 since I had everything on-hand. Your costs will definitely vary!
One of the biggest benefits of using a tablet, PC, Laptop or smartphone for playing the sound effects, is that it is easy to find public domain sound effects on the web in MP3 format. The code allowed me to randomize a small library of sounds as well as to overlap sounds which resulted in some unintended effects. The net result is that kids left laughing.... mostly nervous laughter as the sound caught them by surprise!
It was a mad scramble to get it all done in one afternoon. The code was hacked together from bits and pieces I had used on other projects. The final painting and decoration of the box happened after Halloween, as well as a cleanup of the code. It was worth the effort... and unintended/unexpected stress of working to an immovable deadline!
Step 1: How It Works
The PIR sensor generates a 3.3V signal when it senses motion. This status change triggers a function on the electric imp firmware which transfers the sensor state to an imp agent that resides in the cloud. An HTML page running on the tablet (or PC or Laptop) periodically queries the agent for sensor status. When the HTML page receives a sensor status of "1", this means that motion was detected and a sound should be played. The HTML Page generates a random number between 0 and 9 and uses this as an index into a pre-loaded list of 10 sound effects (MP3) and plays the sound. If the HTML page queries the status and the sensor status is "0", then the current playing sound is allowed to run to completion. If the status is still "1" another random sound is played. This results in multiple overlapping sounds which adds to the scare factor! For example, the chainsaw might let rip in the middle of an evil laughter sound effect.
The sound library can be as big as you want. There are many sounds available on the web in the public domain. You can increase the library by editing the supplied HTML file sound table.
Step 2: Stuff I Used
- A tablet, PC or Laptop. Any computing device with a WiFi connection as well as the ability to connect to a bluetooth speaker
- A bluetooth speaker. Any bluetooth speaker will work here. A small speaker is easier to hide inside one of your decorations like the halloween pumpkin you laboriously carved. If you do hide the speaker inside a pumpkin use aluminum foil or plastic wrap on the surface that touches the pumpkin to avoid getting pumpkin goo all over your speaker! The speaker I used is available here: http://www.walmart.com/ip/808-CANZ-Bluetooth-Wireless-Speaker-Silver/32468583
- An electric imp card and April development board. This is available from AdaFruit and Sparkfun. Combined cost is around $42 making this a very expensive solution! https://www.sparkfun.com/products/11400 , https://www.sparkfun.com/products/11395 and these will be handy https://www.sparkfun.com/products/11417
- A passive infrared sensor. The one I used is the HC-SR501 Adjust IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module. There are many variants of this device on ebay - search for HC-SR501. Expect to pay around $1.80 each. A while back I ordered pack of 10 for another project for $10 and free shipping. I had some left overs, one of which I used for this project.
- A quad AA battery holder. Radioshack stocks these. http://www.radioshack.com/radioshack-4-aa-battery-holder/2700383.html. I had multiple dual battery holders so I used 2 of them in series to generate 6V DC. The minimum voltage for the detector is 5V. I had it work at 3V but it's always better to follow the spec!
- A project box to house the electric imp. I used a cardboard box I had on-hand. You don't need anything special - just something to enclose the electronics. I used a box with dimensions 5" x 4" x 2" will work well. A cardboard box is easy to cut but for a more robust solution, these boxes from Radioshack will work nicely: http://www.radioshack.com/project-enclosure-6x3x2-/2701805.html
- A cardboard or opaque plastic tube tube. Inner diameter will need to be 1.75" to ensure the PIR sensor fits inside of it. The smallest practical tube is 1" which will fit the dome of the PIR sensor. The tube from a spent paper towel roll works well. You just need something to reduce the detection angle of the PIR sensor so that you can target the direction you want the triggers to come from.
- Male-to-Female Dupont wires to connect the Imp to the PIR Sensor
- 4 x AA batteries for the motion sensor
- Double Sided Tape - used to secure the electronics and battery holder within the enclosure.
- Soldering Iron and Solder to solder the battery holder wires to the Imp development board.
- Xacto knife or sharp utility knife to cut the cardboard tube to length and cut a hole in you enclosure box. If you project box is plastic, you will need a Dremel or similar tool to make a hole large enough for the cardboard/plastic tube you choose to use.
- Duct tape, masking tape, clear tape - any tape that can stick the cardboard tube to your electronics project box.
- A PC to program the imp.
Step 3: Building the Motion Detector
Connecting the Imp to the PIR sensor is straight forward. There are only 3 wires to connect. The Vin pin on the Imp is the battery voltage and is an output and is connected to the PIR Sensor.
Referring to Image #10
- Electric Imp Vin pin connects to PIR Sensor +ve power (see image 10)
- Electric Imp Gnd pin connects to PIR Sensor GND
- Electric Imp Pin 1 connects to PIR Output which is the center pin
Ensure that the jumper on the Imp Board is set for Battery input and not USB input. You could use USB but the idea is to eliminate all wires so that the sensor is easy to hide.
Since I only had dual battery holders, I connected 2 of them together in series to create a 6V battery source. All this requires is that you connect the red wire from the first battery holder to the black wire of the second battery holder. The 2 battery holders are stuck together using double sided tape. If you have a quad battery holder, you are already ahead of the game!
Use the cardboard tube as a template and cut a hole for it in the side of you enclosure box. Tabbify the edge of the tube and use your favorite tape to secure the tube to the box. The tube is used to cut down the detection angle of the sensor. By default it will detect a very broad cone of about 100 degrees. The tube will knock that down so that the system is only triggered when the tick-or-treaters walk directly past it.
The sensor may require adjustment to get the triggering just right. I adjusted the time delay to about 7 seconds and the sensitivity to minimum. The image shown above has the potentiometers set as I needed them. This will reduce random triggers. With a 7 second time delay, you will hear multiple sounds each time the device is triggered due to the Tablet polling interval being about 2 seconds.
Now is a good time to sign up for a developer account at ElectricImp.com. Signup is free and provides access to the IDE for programming the imp. In order to connect your imp with your electric imp account, you need to go through a blink-up process. The entire process is described in superb detail here: http://electricimp.com/docs/gettingstarted/
It makes no sense for me to repeat it and I would do a worse job! I recommend you follow through the entire getting started process to familiarize yourself with the Imp, blink-up and the IDE http://electricimp.com/docs/gettingstarted/ide/ since these skills will be necessary to complete the sounds effects project.
When you have successfully completed blink-up and run the simple IDE example, you can use the double sided tape to secure the imp, battery holder and PIR sensor within the enclosure as shown in the images. You can of course do your own thing here. There is nothing super critical - just make sure the dome part of the sensor is pointing toward the far end of the tube.
Step 4: Programming the Imp and Agent
The program for the imp is attached to this step. There are 2 separate but related pieces of code - the Imp Code and the Agent Code. The Agent lives on the Imp Cloud Servers and provides the API for the HTML page running on the tablet to query the status of the motion sensor.
The Imp Code is downloaded form the cloud to the Imp hardware. This code implements a callback function for Pin 1 of the imp which is triggered whenever the Pin line level changes state. This is an efficient way to monitor the status of the motion sensor without polling the pin. Every time the motion sensor is triggered, it generates a logic level "1" at the Imp pin. The Imp code reads the pin level and transfers this to its agent through a background messaging mechanism between the Imp and it's Agent. The Agent stores the pin status, and returns it to the HTML page upon request.
The easiest way to load the code is to create new model for the Imp by clicking on the gear icon next to the Imps Device Name. In my case I left the default name which is the mac address pre-pended by an Imp specific code. Click on the Associated Model combo box and type a new Model name: HalloweenSound. Click Save Changes to create an empty model. Now open the attached HalloweenAgent.Nut file and past the contents into the left Pane of the IDE, Open the HalloweenDevice.Nut file and past it's contents into the Right Pane of the IDE. Click the "Build and Run" button to start the agent and the Imp. If you copied everything correctly you should see no errors!
If you do see errors, you have made a copy and paste error so delete everything from the two IDE panes and repeat the process.
You are now ready for the HTML code.
Step 5: HTML Code for the Tablet
'https://agent.electricimp.com/' + halloweenSettings.URLKey
The APIKey is a basic security mechanism to ensure that if your URL is compromised, you can still prevent unauthorized access to your API. The Agent code running in the Imp Cloud checks all incoming requests to ensure that the key matches. The key can be any random alphanumeric string - you just need to ensure that the string is the same in the Agent code and the HTML code.(API Key idea from http://forums.electricimp.com/discussion/comment/...
The polling cycle formats a JSON request for the Imp Agent. The URL includes a timestamp to prevent the browser from caching the outgoing request.
The response from the Imp Agent is the status of the PIR sensor. If the sensor status is "1" then a random number is generated to index into the sound table.
The PlaySound function plays the sound and shows the current sound on the screen. This is useful for debugging and also gives you something to look at when you get bored!
If you have a web server, you can simply copy the files provided with this step to the web server, point your tablet/PC/Smartphone to the URL and you are done. If you don't have a web server and you are using a PC or laptop, you can copy the files directly to a folder and open the i.html file from the folder you used.
If you are not using a web server, you can copy the files directly to the Tablet. This is a little tricky because the folder path on the tablet is not obvious. Using an Android tablet connected to a PC USB port, you will be able to see the tablet's folder structure. Add a folder named Halloween and copy all the files to that folder. Using Google Chrome on the tablet, use the file:/// protocol to open the i.html file.
To find the path on the Android tablet, download any of the free File Explorer Apps from the Android App Store and navigate to your Halloween folder. The File Explorer app will reveal the path to the folder. For example, file:///storage/emulated/0/halloween/i.html
Step 6: Putting It All Together
Black spray paint and a sticker to cover the ugly label on the top of the box served as box decoration.
Now all that's left to do is to pair your Bluetooth speaker to the tablet and you are ready to scare the trick-or-treaters. With everything wireless, it is easy to hide the WiFi PIR module, tablet and speaker as long as you keep everything within wireless range. In my case, the WiFi PIR module was aimed to trigger as a person reached the bottom of the stairs to the front door, and the sound would start a short while later from the speaker hidden at the top of the stairs.
That wraps it up. Thanks for reading!
Third Prize in the
Halloween Decor Contest