Have you ever wanted to play a sound effect when someone enters the door? Using a Raspberry Pi, some speakers, and some ingenuity you can make your life closer to one of my favourite shows - Seinfeld. This Raspberry Pi project is great for beginners and introduces you to the concept of using the GPIO (general purpose input-output) pins.
If you’re not a Seinfeld fan, you can always replace the audio with another sound of your choosing. Here’s how to build a Raspberry Pi machine that plays a custom sound effect when a door is opened.
What You’ll Need For This Project
- Raspberry Pi 4 or Raspberry Pi 3 with power adapter
- 8 GB (or larger) microSD card with Raspberry Pi OS. See our list of best microSD cards for Raspberry Pi.
- Desktop speakers or a megaphone with a 3.5mm input and 3.55mm cable
- Speaker wire, any gauge, at least a few feet in length
- Aluminum foil
- Electrical tape
- Male to female jumper cables (2)
- Wire strippers or scissors
- Monitor or Projector with HDMI and power cables. (Optional)
How to Turn Raspberry Pi into an Electronic Door Chime
Before you get started, get your Raspberry Pi set up. If you haven’t done this before see our article on how to set up a Raspberry Pi for the first time or how to do a headless Raspberry Pi install (without the keyboard and screen). For this project, we recommend a headless Raspberry Pi install.
1. Install the lower-level dependencies we need to play audio files and make our code run by entering the following commands in a terminal window.
sudo apt-get update sudo apt-get -y install mpg321 git python3-dev python3-rpi.gpio
2. Set the 3.5mm audio output to be the default audio output in raspi-config. You do this by launching raspi-config (entering sudo raspi-config at the command prompt) and navigating to System Options -> Audio -> Headphones 1. The pi allows for audio to be outputted to either the 3.5mm analog output, or over the HDMI port. If you do not have a monitor attached, Headphones may be the only option.
3. Enable auto-login in raspi-config by navigating to System Options > Boot / Auto Login > Console Autologin. When the raspberry pi restarts, it will automatically log in (which we’ll need to run our scripts).
4. Click finish to exit the raspi-config menu, and reboot your raspberry pi (either through the prompt, or the console).
5. From your home directory, clone the sample code using git.
git clone https://github.com/rydercalmdown/doorbell
6. Plug your speakers into your Raspberry Pi, and test them by playing your audio file. You should hear sound from your speakers.
7. Adjust your pi’s volume with the alsamixer command, or if your external speakers have a volume knob, on there too. Use the arrow keys to adjust the volume, and Ctrl + C to exit.
8. Test the script. Connect one jumper wire to board pin 12 (GPIO pin 18), and a second to ground. Then run the python command below. Once it’s running, touch (short) the two jumper wires together for a second or two, then release them. If all goes successfully, the console will output “playing audio_0.mp3” and you should hear the sound over your speakers.
9. Download or copy a few short mp3 files you’re looking to play. In my case, I took a few recordings from YouTube and copied them over to the Raspberry Pi into the audio directory using SCP. However, I could have also used Chromium on the Pi to download or copied files over using VNC or FTP. Any mp3 files placed in /home/pi/doorbell/audio directory will be randomly selected when the door opens or closes.
10. Tape a small, rectangular piece of aluminum foil to the door.
11. Cut a piece of speaker wire long enough to stretch from your Raspberry Pi to the door.
12. Strip the wire on both ends
13. Tape one end to the door frame so that when closed, the piece of aluminum foil closes the circuit. It is also possible to use a reed switch instead of aluminum foil.
14. Connect the other ends of the speaker wire to the jumper cables using solder or electrical tape.
15. Add the script to /etc/rc.local so it runs automatically when the Raspberry Pi restarts.
sudo nano /etc/rc.local # Add the following line before the last line (exit 0) python3 /home/pi/doorbell/app.py &
16. Restart the Raspberry Pi
17. Test your project by opening or closing the door.
If all goes well, you should hear a random sound from the audio directory.