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 guide works with all 20-pin and 40-pin versions of the Raspberry Pi.
This project was conceived and executed along with George Michael Brower.
Page last edited March 08, 2024
Getting Started
- Raspberry Pi - All 20-pin and 40-pin models are supported (A, B, A+, B+, Zero, Pi 2, Pi 3)
- Pi Plate
- SD Card
- Magnetic Door Switch
- PIR Motion Sensor
- USB Powered Speakers
- (1) Red & (1) Green LED
- (2) 220ohm & (1) 10k ohm resistors
- Raspberry Pi VESA Mount - meant for mounting to the back of HDTV's, but mounts to walls just as well :)
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 Preparing an SD Card for your Raspberry Pi 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.
$ sudo apt-get update -y $ sudo apt-get upgrade -y
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.
$ sudo apt-get install python3-pip
We are using the CircuitPython Libraries that are part of adafruit-blinka. If you have any trouble with the following command see CircuitPython Libraries on Raspberry Pi to get a fresh Raspberry Pi setup.
$ sudo pip3 install adafruit-blinka
Page last edited March 08, 2024
Wire it
Connections on Prototyping Pi Plate:
PIR SensorAnd of course, some sort of stereo or portable speaker in the 1/8in jack so you can hear it!Door Switch
- Red -> 5V
- Yellow Data -> input 24
- Black -> Ground
Indicator LEDs
- One wire to ground, the other to input 23, with a 10k pull-up resistor to 3v3
- 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
That's it! Now on to the code...
Page last edited March 08, 2024
Code
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 .
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.
$ 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
$ sudo amixer cset numid=3 1
# SPDX-FileCopyrightText: 2019 Mikey Sklar for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import board
from digitalio import DigitalInOut, Direction
import pygame.mixer
# setup inputs
door_switch_pin = board.D23
motion_sensor_pin = board.D24
door = DigitalInOut(door_switch_pin)
door.direction = Direction.INPUT
motion = DigitalInOut(motion_sensor_pin)
motion.direction = Direction.INPUT
# setup output LED indicators
motion_led_pin = board.D18
door_led_pin = board.D25
door_led = DigitalInOut(door_led_pin)
door_led.direction = Direction.OUTPUT
door_led.value = False
motion_led = DigitalInOut(motion_led_pin)
motion_led.direction = Direction.OUTPUT
motion_led.value = False
prev_door = False
# audio settings
pygame.mixer.init(44100, -16, 2, 1024)
# sound files expect to be in the same directory as script
enter_sound = pygame.mixer.Sound("./enter.wav")
exit_sound = pygame.mixer.Sound("./exit.wav")
while True:
# toggle door LED based on door sensor
if door.value:
door_led.value = True
else:
door_led.value = False
# toggle motion LED based on motion (PIR) sensor
if motion.value:
motion_led.value = True
else:
motion_led.value = False
# When the door is opened, if there is movement outside,
# It means that someone is entering.
# If not, someone is exiting.
if door.value and not prev_door:
if motion.value:
enter_sound.play()
else:
exit_sound.play()
prev_door = door.value
time.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.
$ 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 time. For more info, check out the PIR tutorial
Now lets mount it!
Page last edited March 08, 2024
Mount it
Page last edited March 08, 2024
Test it
Here's a little video of my roommate trying out both entering and leaving:
Page last edited March 08, 2024