Raspberry Pi Haunted House
|With Halloween just a few weeks away, we thought we’d take a look at how to add a little spook with our favourite little computer, the Raspberry Pi.
There are literally hundreds of ways to use a Pi to add a little fright to the night, but we’re going to look at adding some creepy sound effects.
This particular project is going to use Python, a PIR movement sensor and some speakers to add sounds that mysteriously play whenever someone approaches. This could be placed near a front door to get trick-or-treaters, or perhaps in a hallway to scare your family or housemates. You get extra points if the victim has to pass the sensor before they can turn the light on.
Firstly, though, you’ll need to make sure you’ve got python, pygame and RPi.GPIO installed with
sudo apt-get install python python-pygame python-rpi.gpio
Then you’ll need to setup the circuit. We’re going to use a Passive InfraRed (PIR) movement sensor. There are a number of different ones available, and some work in different ways. This tutorial is based on the SEN-08630 from SparkFun (see https://www.sparkfun.com/products/8630).
This sensor has three pins: positive, ground and alarm. On ours, the positive was coloured red, the ground was white and the alarm was black, but there is no guarantee that yours will be the same, so check on the sensor. The positive is marked with a +, the alarm with AL, and the ground is in the middle.
Pull Up Resistor
The alarm is an open collector. This means that when movement is detected, the alarm pin will act like a ground. In order to use this, we need to create a simple circuit with a pull up resistor. This will pull the input up to 3.3 volts when the alarm is off, and drop it to 0 volts when the alarm is triggered (as so acting like a ground). Were you to simply connect the alarm pin to a GPIO pin, then it would be 0v when the alarm is triggered, but simply wouldn’t be in a circuit when it isn’t.
The Raspberry Pi has internal pull up resistors that you can add to pins when you set them up. In the code below, you’ll see the line:
GPIO.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
This tells the Pi to connect a resistor between the pin and 3.3v. Doing this means that when the alarm on the sensor isn’t triggered, the pin will read high, while if the alarm is triggered, it will earth the pin and it’ll read low.
All you need to do to connect the sensor to the Pi is wire up the sensor’s positive pin to 5v (the sensor won’t work properly at 3.3v), the earth pin to 0v and the alarm pin to pin 4 (see http://elinux.org/RPi_Low-level_peripherals for details of what pins do what).
Adding Sound Effects
With this all wired up, all you need is a creepy sound to play when someone activates the motion sensor. You could record your own, but when we need sound effects (or any artwork for computers really) our first port of call is opengameart.org. We’ve gone for a vulture call (because everyone knows that vultures follow zombies to eat the brain-less corpses left behind by the feeding undead).
The only thing left is a tiny bit of Python code to pull it all together. Copy the below as a new python program (you may have to adjust the location of vulture-1.wav depending on where you saved it), and set it running.
We’ve used Pygame’s mixer to play the sounds purely because it’s the module we’re most familiar with.
You’ll need to run it with sudo (i.e. by running sudo python thisfile.py) so that it has access to the GPIO pins.
import RPi.GPIO as GPIO import time import pygame pygame.mixer.init(frequency=22050,size=-16,channels=4) sound = pygame.mixer.Sound('/home/pi/vulture-1.wav') GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_UP) playing = 0 while True: if not GPIO.input(4): if playing == 0: sound.play() playing = 10 if playing > 0: playing = playing - 1 time.sleep(0.5)
Remember to plug the speakers into your RasPi’s speaker jack socket, and turn the volume up nice and high.
Of course, you don’t have to stop with just sound effects. You can use the motion sensor to trigger just about anything. For example, you could plug your Pi into a TV and get it to flicker to life at an opportune moment, or use some servos to move hairy spiders around. There’s still some time to let your imagination get creative before fright night!
Love to give a Pi comment but I don't have one as family, business and dog leave know room for such self absorbing luxuries. I do however have an Android phone and PC and running Mint 14. So I thought of a challenge which is slightly more useful than a poster for an ogg camp event. Would it be a feasible challenge to ask you as a team to write a plug and play style application so that we are able to connect an Android phone to Linux via USB and browse the device through Nemo, Nautilus or Dolphin etc. I have never coded a thing in my life (a simple RM or CP command is a s close as I get, although I did by Paul Hudson's Coding Academy a while back, so you never know), but I have no idea if it is an easy quick task or an involved lengthy one. If possible it would make life so much easier for users like myself that love to use Linux but not know how it really works. Android is the worlds most popular mobile OS it just seems a little bonkers that Linux cannot talk to Linux in a plug and play type environment, there must be thousands of people who would benefit from this. If you can give it a go and call it something cheesey 'LL Cool Way' for example. Cheers guys.
The RaspberryPi blog has had a similar idea. They have a link to some nice public domain scary noises at http://opensource.about.com/od/hardware/a/10-Haunting-Open-Source-Melodies.htm
I've thought of a better idea. Requires a few extra components, namely motors.
So once they get in range, the gate slams behind them (and locks or holds shut if possible).
A door is released and the baying hounds are released. I'd be quite happy to provide some of the components of these. Err well I can provide a Rottweiler and an German Shepherd Dog.