It’s halloween soon so I thought I’d try and create a really simple halloween project that anyone with a raspberry pi and a couple of simple components can implement, obviously this can be expanded upon massively but it’s a good starter and possibly the simplest script I’ve written.
Hardware
For this project we need:
- Raspberry Pi
- Cobbler (or equivalent to connect to GPIO pins)
- PIR sensor (I used these ones from Amazon)
- speakers and cable
Circuit
As mentioned above this is possibly the simplest circuit I have ever used in any projects.
The audio out of the Raspberry Pi should connected to speakers.
Code
I am assuming that the python and GPIO libraries have been installed so will not cover this.
The intention of the code will be to monitor the pi and then play a sound when the PIR is triggered, as it is halloween I have chosen a “witches cackle” but anything could be used, you could also use this to trigger an output via HDMI to play some images.
Configure the audio drivers
I will me using the mpg321 drivers for the audio, a detailed summary of the drivers and options can be found on Jeff Skinners Box. For this project you will simply need to run the following commands in the command prompt\bash window.
sudo apt-get install alsa-utils sudo apt-get install mpg321
Now we can create our python script, open a new file
sudo nano pir.py
Now add the code below, this sets the GPIO pin 18 to read from the PIR sensor, when there is a reading (i.e. the sensor has been triggered) it plays a sound.
#!/usr/bin/env python import os import time import RPi.GPIO as io io.setmode(io.BCM) pir_pin = 18 io.setup(pir_pin, io.IN) # activate input while True: if io.input(pir_pin): os.system('mpg321 WitchCackle.mp3 &') time.sleep(2)
Save the script using Ctrl+x and then Chmod the file so that it is an executable.
sudo chmod -x pir.py
The current script also uses an mp3 file, this should be transferred to your raspberry pi and saved in the same directory as the script.
Additions
Now we have the sound or images triggered the next step for me is to add some LEDs to glow, which we’ll use as the eyes in bats or masks that we hang around.