Overview

This guide is for people that wish they had a soundtrack to their life. Using a PIR sensor and magnetic door switch with a Raspberry Pi, you'll be able to play a sound file every time you open a door. More specifically, it will play a pre-recorded sound file based on whether you are entering (applause!) or leaving (booooo!).

This project was conceived and executed along with George Michael Brower.
Temporarily unable to load embedded content. Please try again later.

Getting Started

Items used:
Before we get into the fun stuff, make sure to have Adafruit's Occidentalis OS installed, as well as our WebIDE. Follow the links for how to's.

Wire it

PIR sensors and the Pi 3 sometimes don't get along - if you're having false trigger reports, make sure the PIR Sensor is far away from the Pi 3

Connections on Prototyping Pi Plate:

PIR Sensor
  • Red -> 5V
  • Yellow Data -> input 24
  • Black -> Ground
Door Switch
  • One wire to ground, the other to input 23, with a 10k pull-up resistor to 3v3
Indicator LEDs
  • Green connected to output 18 through a 220ohm resistor, will correspond to motion detection
  • Red connected to output 25 through a 220ohm resistor, will correspond to an open magnetic door switch
And of course, some sort of stereo or portable speaker in the 1/8in jack so you can hear it!

That's it! Now on to the code...
Log in to your Raspberry Pi using the Adafruit WebIDE and create a new file. Mine's called SitcomSFX.py

Upload two audio files to that directory, one named "enter.wav" the other "exit.wav"

You can find pretty decent sound clips here & here, and more professional ones here.

MP3'S WILL NOT WORK WITH PYGAME!
If you are using the 1/8inch audio jack, you'll need to ssh into your Pi and run this command to route audio to that output
$ sudo amixer cset numid=3 1
#!/usr/bin/env python

from time import sleep

import pygame.mixer
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

door_switch_pin = 23
motion_sensor_pin = 24

#Set up inputs
GPIO.setup(door_switch_pin, GPIO.IN)
GPIO.setup(motion_sensor_pin, GPIO.IN)

motion_LED_pin = 18
door_LED_pin = 25

#Set up output LED indicators
GPIO.setup(door_LED_pin, GPIO.OUT)
GPIO.setup(motion_LED_pin, GPIO.OUT)

pygame.mixer.init(44100, -16, 2, 1024)

prev_door = False

#sound files expect to be in the same directory as script
enter = pygame.mixer.Sound("./enter.wav")
exit = pygame.mixer.Sound("./exit.wav")

while True:
	#Update sensor and LED states each loop
	door = GPIO.input(door_switch_pin)
	motion = GPIO.input(motion_sensor_pin)

	GPIO.output(motion_LED_pin, motion)
	GPIO.output(door_LED_pin, door)
	
#When the door is opened, if there is movement outside, it means that someone is entering. If not, someone is exiting		
	if door and not prev_door:		
		if motion:
			enter.play()
		else:
			exit.play()
			
	prev_door = door
	
	sleep(0.01)
Run the code on your Pi and make sure it works. If the green LED is lit (motion detected) it should play enter.wav when the door switch is opened, and exit.wav if there is no motion detected. 

The PIR sensor has two physically adjustable potentiometers on it, I've found it's best to set it to max sensitivity and minimum retrigger time. For more info, check out the PIR tutorial

Now lets mount it!

Mount it

Pi, Pi plate, and speaker all stacked together on the VESA mount above the door.
Magnetic door switch contacts have sticky foam, or screws for a more permanent install.
Mounted PIR sensor with some Sugru

Test it

Now its testing time. The Pi looks for the two sensor inputs: the first one is the magnetic door switch, when that opens it knows that someone is entering or leaving. The PIR sensor is used to tell what side of the door they're on - that's what determines which WAV to play.

Here's a little video of my roommate trying out both entering and leaving: