# Sitcom SFX Door Trigger

## Overview

http://vimeo.com/55041635

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&nbsp;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 guide works with all 20-pin and 40-pin versions of the Raspberry Pi.&nbsp;  
  
This project was conceived and executed along with [George Michael Brower](http://www.staggeringbeauty.com).

![](https://cdn-learn.adafruit.com/assets/assets/000/003/148/medium800/raspberry_pi_image_2-1.jpeg?1396792279)

# Sitcom SFX Door Trigger

## Getting Started

# Items Used:
![](https://cdn-learn.adafruit.com/assets/assets/000/003/144/medium800/raspberry_pi_image_6.jpeg?1396792211)

- [Raspberry Pi](https://www.adafruit.com/product/3775)&nbsp;- All 20-pin and 40-pin models are supported (A, B, A+, B+, Zero, Pi 2, Pi 3)
- [Pi Plate](http://adafruit.com/products/801)
- [SD Card](http://adafruit.com/products/102)
- [Magnetic Door Switch](http://adafruit.com/products/375)
- [PIR Motion Sensor](http://adafruit.com/products/189)
- [USB Powered Speakers](https://www.adafruit.com/product/1363)
- (1) Red & (1) Green LED
- (2) 220ohm & (1) 10k ohm resistors
- [Raspberry Pi VESA Mount](http://adafruit.com/products/986) - meant for mounting to the back of HDTV's, but mounts to walls just as well :)

# Update Your Pi to the Latest Raspbian
Your Pi will need to be running the latest version of Raspbian. This tutorial was written using Raspbian Stretch (Nov. 2018). Checkout our guide for&nbsp;[Preparing an SD Card for your Raspberry Pi](https://learn.adafruit.com/adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi)&nbsp;if you have not done so already. After the installation is complete be sure and run the following commands to make sure your installation packages are up to date.&nbsp;

```auto
$ sudo apt-get update -y
$ sudo apt-get upgrade -y
```

# Install pip3
pip3 is already installed with a full Raspbian installation, but the Raspbian Lite does not include pip3 so it needs to be installed as shown below.

```auto
$ sudo apt-get install python3-pip
```

# Install adafruit-blinka
We are&nbsp;using the CircuitPython Libraries that are part of adafruit-blinka. If you have any trouble with the following command see&nbsp;[CircuitPython Libraries on Raspberry Pi&nbsp;](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)to get a fresh Raspberry Pi setup.

```auto
$ sudo pip3 install adafruit-blinka
```

# Sitcom SFX Door Trigger

## Wire it

![](https://cdn-learn.adafruit.com/assets/assets/000/003/142/medium800/raspberry_pi_Screen_Shot_2012-12-19_at_7.45.35_PM.png?1396792169)

Warning: 

## **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&nbsp;to an open magnetic door switch

And of course, some sort of stereo or&nbsp;portable speaker in the 1/8in jack&nbsp;so you can hear it!  
  
That's it! Now on to the code...  
# Sitcom SFX Door Trigger

## Code

# Audio Files
Be sure to download the following MP3 files so that you can hear the samples playing. These sounds we are using came from MATTIX at freesound.org .&nbsp;

1. [enter.wav](https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/master/Sitcom_SFX_Door_Trigger/enter.wav)
  - freesound 462093

2. [exit.wav](https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/master/Sitcom_SFX_Door_Trigger/exit.wav)&nbsp;
  - freesound 454638

# Pulling Down the Files on the Pi
We can use wget to pull down the audio files and code directly to the Pi we are working on. ssh into your pi and run the following command to pull down the two audio files and example python code.

```auto
$ wget https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Sitcom_SFX_Door_Trigger/enter.wav
$ wget https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Sitcom_SFX_Door_Trigger/exit.wav
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Sitcom_SFX_Door_Trigger/Sitcom_SFX_Door_Trigger.py
```

# Select the Audio Port
Danger: 

```auto
$ sudo amixer cset numid=3 1
```

# The Code
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Sitcom_SFX_Door_Trigger/Sitcom_SFX_Door_Trigger.py

# Run the Code
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.&nbsp;

```auto
$ sudo python3 ./Sitcom_SFX_Door_Trigger.py
```

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&nbsp;time. For more info, check out the [PIR tutorial](https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/overview)  
  
Now lets mount it!

# Sitcom SFX Door Trigger

## Mount it

![](https://cdn-learn.adafruit.com/assets/assets/000/003/147/medium800/raspberry_pi_image_2.jpeg?1396792260)

Pi, Pi plate, and speaker all stacked together on&nbsp;the VESA mount above the door.

![](https://cdn-learn.adafruit.com/assets/assets/000/003/146/medium800/raspberry_pi_image_8.jpeg?1396792243)

Magnetic door switch contacts have sticky foam, or screws for a more permanent install.

![](https://cdn-learn.adafruit.com/assets/assets/000/003/145/medium800/raspberry_pi_image_12.jpeg?1396792226)

Mounted PIR sensor with some [Sugru](http://www.adafruit.com/products/436)

# Sitcom SFX Door Trigger

## 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:

https://vimeo.com/56001329


## Related Guides

- [PIR Motion Sensor](https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor.md)
- [Christmas Soundboard with NeoTrellis M4](https://learn.adafruit.com/xmas-sound-board.md)
- [Using ItsaSNAP for HomeKit PIR Motion Detection](https://learn.adafruit.com/itsasnap-homekit-pir-motion-detection.md)
- [Talking Musical NeoPixel Clock with Infrared, BLE and Touch Controls](https://learn.adafruit.com/talking-musical-neo-pixel-clock-with-infrared-ble-and-touch-controls.md)
- [Adafruit BrainCraft HAT - Easy Machine Learning for Raspberry Pi](https://learn.adafruit.com/adafruit-braincraft-hat-easy-machine-learning-for-raspberry-pi.md)
- [Adafruit Metro M4 Express AirLift (WiFi)](https://learn.adafruit.com/adafruit-metro-m4-express-airlift-wifi.md)
- [Analog Knobs on Raspberry Pi 400 with CYBERDECK Hat](https://learn.adafruit.com/analog-knobs-on-raspberrypi-400-with-cyberdeck-hat.md)
- [Feather Freezer Door Alarm](https://learn.adafruit.com/feather-door-alarm.md)
- [Using Adafruit IO Actions to Make an IoT Door Detector](https://learn.adafruit.com/using-adafruit-io-actions-to-make-an-iot-door-detector.md)
- [How to Make Games in MakeCode Arcade](https://learn.adafruit.com/how-to-make-games-on-makecode-arcade.md)
- [Adafruit IO Basics: Analog Output](https://learn.adafruit.com/adafruit-io-basics-analog-output.md)
- [Make It Talk](https://learn.adafruit.com/make-it-talk.md)
- [Single Channel LoRaWAN Gateway for Raspberry Pi](https://learn.adafruit.com/raspberry-pi-single-channel-lorawan-gateway.md)
- [Larsio Paint Music](https://learn.adafruit.com/larsio-paint-music.md)
- [Adafruit IO Basics: Digital Output](https://learn.adafruit.com/adafruit-io-basics-digital-output.md)
- [ABC Soundboards with NeoTrellis](https://learn.adafruit.com/abc-soundboards-with-neotrellis.md)
