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).

rpi-haunting

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!

3 Comments

Add a Comment

Your email address will not be published. Required fields are marked *