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.

Items Used:

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

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.

$ sudo apt-get install python3-pip

Install adafruit-blinka

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

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 . 

  1. enter.wav
    • freesound 462093
  2. exit.wav 
    • 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.

$ wget
$ wget
$ wget

Select the Audio Port

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

The Code

# 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
        door_led.value = False

    # toggle motion LED based on motion (PIR) sensor
    if motion.value:
        motion_led.value = True
        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:

    prev_door = door.value

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. 

$ sudo python3 ./

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!

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

This guide was first published on Dec 20, 2012. It was last updated on Jul 16, 2024.