Got this idea after making my first autonomous robot with arduino and ultrasonic sensor. With Halloween coming up and my fascination with raspberry pi I put my new found knowledge of the distance sensor to good use. I had all parts already except the pumpkin so i'll do my best to list all parts and provide links where possible.
Entered in the Halloween decoration contest please vote me up I want a shirt lol.
Step 1: Parts and Tools
- Raspberry Pi(I used 2B, I'm sure the 3 or others would work as well.)
- HC-SR04 Ultrasonic Sensor(I bought the 5 pack for past projects and had a couple left over)
- Resistors - (330Ω and 470Ω), jumper wires, breadboard
- Bluetooth Transmitter - (Optional. First iteration I used a cable but didn't like the limitations)
- Bluetooth Speaker - (Again the Bluetooth part is optional)
- Micro USB cable
- Pumpkin (plastic) or whatever you want to put it in
- Box cutter (something to cut open the pumpkin)
Step 2: Lets Start With Hardware
The set up is pretty simple I got the diagram above from tutorials-raspberrypi.com so you can read over their explanation for the need for resistors. As shown find the correct GPIO pins you want to use and the appropriate ones for your pi model. Make all connections as shown and that's it for connecting pi to sensor. Plug in the Bluetooth transmitter (audio cable) into the pi's audio out and set aside for testing (I always test things out before putting in the case (pumpkin)). I also powered the Bluetooth transmitter from the pi its self.
Step 3: Software
You should have all necessary programs if you install the latest version of RASPBIAN JESSIE for pi. Just in case run these commands to ensure. I used python 3 to run the app its just a matter of syntax change mostly but found this version to run the code better in its current state.
sudo apt-get update && sudo apt-get upgrade sudo apt-get install python3 sudo apt-get install python3-rpi.gpio sudo apt-get install mpg321
Files and directories:
To keep all files together I made a folder to hold the mp3 files and the .py code. Name your file what you want and keep all mp3 files in the same folder as your code.
From the "/home/pi" directory
mkdir halloween touch halloween.py
The python file is attached to this instructable. Most of the code isn't mine thank you raspberrypi-spy,I simply changed the GPIO numbers to my own and wrote the loop that will play the sound clips when a certain distance is detected. The code is very well commented so I won't go over it all but here are some of the high points and parts you may need to change.
Import the necessary libraries.
import time import os import random import RPi.GPIO as GPIO
Assign the correct pins that you connected the Trigg and Echo from the sensor to:
# Use BCM GPIO references instead of physical pin numbers
# Define GPIO to use on Pi Make sure to change this to your own pins if they are different
GPIO_TRIGGER = 18 GPIO_ECHO = 24
Here I edited the loop to play a random sound clip whenever the sensor picks up a distance shorter than the constant (discussed in later step).
while True: distance = measure_average() # In ordger to play a random clip each # time this variable will change with every # loop iteration could also be placed in the if block randomNumber = random.randint(1,12) print ("Distance : %.1f" % distance) # Choose a distance that works for you the 0 accounts # for an echo that was never returned this could be # constantly 0 if not placed in front of an object/wall if(distance == 0 or distance < 124): #play mp3 if(randomNumber ==1): os.system('mpg321 /home/pi/halloween/candy.mp3') if(randomNumber ==2): os.system('mpg321 /home/pi/halloween/gameover.mp3') # Time between loop iterations time.sleep(.25)
Step 4: Sound Clips
I found clips at numerous sites just make sure they're mp3 if not you can convert them. I also made a couple of my own with a voice changing app you could find some in either app store and you can make custom creepy greetings.
Some sites with sound clips:
This site has compiled a bunch of sites for you as well: https://www.thebalance.com/best-places-to-find-fre...
I personally wanted to use movie sound clips but my wife preferred some generic ghost/howling/creepy laugh noises. I did manage to sneak in the jaws theme and some other classics that got her approval.
Step 5: Starting Script Remotely
So there's multiple options here but I have two preferred methods.
- XRDP into the pi from laptop/computer on the network. This is nice if you prefer a gui,but when all you need to do is get in and start a script its not necessary. once deployed this method may be preferred for adding, placing and renaming the mp3 files.
- Serverauditor - ssh client (android app) from tablet I like this app. I run it from my tablet and kick off the script from here. It was convenient when testing outside.
You could also set the script to run at boot up and it'll run automatically what ever your preference.
To launch your code enter the command:
cd /home/pi/halloween sudo python3 halloween.py
Step 6: Finding the Right Distance
After launching the code for the first time watch the output. The code provided will constantly print out the distance the sensor is receiving. This is good for testing purposes you can remove that portion once deployed but I left it for monitoring purposes. Place the sensor in the desired location (I have it hitting a wall at my entry way which is nice to find a consistent distance). Once you have that control distance use that to determine at what distance you want to trigger the sound. I have it set at anything shorter than the constant by 2 (cm I believe) will trigger a sound clip.
So if my constant is 125 then I set my 'if' statement to (distance == 0 or distance < 123) to account for any inconsistencies and no one is sneaking past within that 2 cm gap. If its not set correctly your sounds may be triggered constantly and ruin the whole point of a sensor.
Step 7: Putting It Together
I placed the pi and sensor in the small pumpkin by cutting a large flap in the back ( I taped shut afterwards as it'll be dark and wasn't to concerned for looks on this part) and a smaller whole to thread cords through. I also kept the bluetooth transmitter on the outside (taped) in order to turn it on or off and in case I need to reconnect to a different speaker. The speaker is housed in the large Skull placed by the door so the sound plays just as they're about to knock.
Step 8: And You're Done
Place it at your door step and scare all the kids that stop by. Please send feedback or ask any questions I'll try my best to answer. First instructable so I probably forgot something or could enhance the tutorial a little more.