It’s the end of the year and time to get a bit festive, so pour yourself some mulled wine, pop on your favourite Christmas jumper and gather around the yule log for some Rocca Christmas fun.
After a successful move into our new base of operations, we thought that now would be a great opportunity take the next logical step in introducing ourselves to our new neighbours, give them something to talk about and bring about some festive cheer as 2015 draws to a close.
Be that as it may, already half-way through the month, we had to act fast if we were to going to do something worthwhile that people might get a chance to actually see. With all the hype around the power of the credit-card sized Raspberry Pi computer and ease of the python programming language, it seemed a no brainer to snap one up for ourselves to experiment with.
So with a Raspberry Pi, a few electronic components and the will to create something festive, we quickly threw together a solution that would detect motion in the hallway outside the office and play various sound clips when people walk past.
We had so much fun with this little project that we thought that it would be a crime to keep it to ourselves, and so:
Here’s everything you’ll need to make one for yourself in just a few minutes:
You’ll need to be able connect your Raspberry Pi to the internet to pull down some packages to make use of the 3.5mm audio jack in the python script. You can do all of this task on the Pi itself or via SSH if you have enabled it within the raspi-config settings.
Firstly, If not connected via SSH, you’ll need to open a terminal window so we can get started. On the Pi itself, this can be found either in Menu > Accessories > Terminal or by clicking the little icon at the top of the screen.
Once you’ve got a terminal window open, you can make a real start.
You’ll need to download the ALSA drivers and mpg123 player to allow our script to play the sound files when triggered, enter these lines into the terminal
sudo apt-get update sudo apt-get install alsa-utils mpg123
If prompted as to whether you would like to install the files, you should select Yes.
Once that’s installed, you will need to reboot your Raspberry Pi:
Now we need a python script to interact with the sensor that we’ll connect later. You can download the script on the Pi using the wget command:
wget h ttps://raw.githubusercontent.com/aetharr/christmaspi2015/master/roccachristmas.py
At this point, you’re almost done. You just need to connect your HC-SR501 PIR sensor to the Pi.
This sensor requires a 5v input, a ground connection and a third connection to report when the sensor has been triggered. All of that seems simple enough to set up, if you connect the wires as illustrated in the image below (Ignore the wire colours, they are connected to the appropriate pins on both devices) then we will be able to interpret if someone walks in front of the sensor.
And with that done, all you need to do is find some sound files for your Pi to play to the unsuspecting people who pass by your office. Just make sure you get at least two files in mp3 format and put them in the same directory as the script. It’s very important to name them in a specific structure however.
This is very important because it tells the python script how long to wait before attempting to play more sounds. There is also a wait timer within the script that is added to this time, but this allows you to have sound clips with varying lengths.
Then finally, you just need to hook it up to some speakers, position it appropriately and run the script. To do this, you must first run a couple of lines to configure the sound drivers, and then the script itself. For our solution, we did this over SSH with a shell script so the Pi didn’t need to be connected to a monitor the whole time.
To make use of the GPIO slots, the python script needed to be run as an administrator, but that is simple enough at least with the sudo prefix.
sudo modprobe snd_bcm2835 sudo amixer cset numid=3 1 sudo python roccachristmas.py
When you run the script, you should see a little indicator in the terminal window, telling you how to terminate the script (Ctrl+C) as well as show you the status of the sensor, such as if it is playing a sound, or waiting for motion.
For a little bit more fun, we also implemented the ability to snap videos when the sensor gets triggered if you’d like to capture the unsuspecting people’s reactions. This requires for you to have a Raspberry Pi camera, and have enabled it in the raspi-config settings. You also need to make a small change to the python script.
Open the script either using your favourite text editor or perhaps nano
change this line:
shouldUseCamera = False
shouldUseCamera = True
Then save the file, and make sure there is a folder called ‘videos’ wherever the script is located, it’s easy to quickly make the folder by running this line:
And there you have it, a quick project that anybody and their dog can do to play some sounds to unsuspecting bystanders. The best part is you don’t have to just use it at Christmas, why not trigger some spooky sounds at Halloween? Or even as a security device to play a scary sound at unsuspecting, suspicious people at night?
The code itself that powers this project is relatively simple, and commented within the python script, should you wish to expand on it yourself. Feel free to have a look, experiment for yourself and share your creations with us.
Finally, these links were useful in getting this little project off the ground.
Both of these websites have stores to purchase various Raspberry Pi and Arduino related equipment, as well as a range of tutorials to help you get started with the Pi.
While a link to the script was included earlier with the wget command, it can also be accessed via this github repository. Feel free to fork it and improve upon it as you will, we look forward to seeing what you make.