In this project you will use a magnetic door switch to trigger pre-recorded sound clips when the door is opened. A useful project to alert shopkeepers when customers enter or leave the premises or a way to play a royal trumpet fanfare when guests arrive, its principles are the same used in security systems, so there’s a lot of potential to extend this project.
You can convert your sounds with Audacity, a free, open source, cross-platform audio software. You can install it on your Pi directly from Terminal by typing the following command and pressing return:
sudo apt install audacity
Once installed you can open your sound files and export them in 16 bit Wave format by going to File > Export > Export as WAV format
ln -sf name_of_music_clip1.wav sound1.wav
You should now have 3 aliases for your sound clips located in your home directory.
Before starting to build the circuit make sure you know how the wires are connected to the Reed Switch. The one used in this example had 4 wires, Red and Yellow wires were connected to the switch, the Black and Blue wires were not used. The Reed Switch’s datasheet will contain the details or use a multi-meter to test for continuity.
The circuit has 3 main components, the button A which activates the circuit, the LED B which indicates if it’s active and the reed switch C which detects when the door opens.
Always build your circuit with the Pi switched off and check it carefully before powering on. Never connect any of the GPIO pins directly to the +5V rail as this will damage the Pi
Check the datasheet for your transistor to see if it uses the same pinouts as the one shown in the diagram. If not, make sure to connect the collector, base and emitter pins as described.
The project uses the Python 3 Gpiozero library and Pygame to play the music.
#!/usr/bin/env python3 """Play a random sound clip activated by a switch. The system is activated / deactivated by pressing a button""" from gpiozero import Button, LED from signal import pause import pygame.mixer from pygame.mixer import Sound import random
If you get any errors check that the indentation of your code is correct as Python relies on correct indentation
Understanding the code
Line 1 is a standard header and makes the script executable in Python 3. Line 2 to 3 is the Doc string and describes the code. It’s good practice to add these.
Lines 5 to 7 set up the Python libraries being used. The GPIO inputs and outputs are managed by gpiozero and pygame.mixer handles playing the music.
Lines 11 to 13 name the LED, Button and Reed Switch and create software objects that represent them. The Reed Switch acts like a button that is normally closed.
Line 15 is a flag that holds the system status which is checked by the activate() and on_open() functions. It is Global in scope so that both these functions can read its value and update it.
Line 17 initialises a pygame mixer object to handle playing the music clips.
Lines 20 to 31 define the activate() function. It gets called each time the Button is pressed. It turns the LED on when the Button is pressed and off when it is pressed again. It also prints out messages to show the system status which is useful for debugging.
Lines 34 to 41 define the on_open() function. This gets called when the Reed Switch is opened and it triggers pygame to play the music clip. The playback is asynchronous so if you close the switch while the music is still playing, you will see the status change, even though the clip has not finished. This style of programming makes the system very reactive so that no opening / closing events are missed.
Lines 44 to 46 define the on_close() function which gets called when the Reed Switch closes. This just updates the status to illustrate how this is achieved.
Lines 49 to 51 are doing the work to capture the button events. They setup call back references to the above functions and this is what triggers them.
Finally line 53 stops the script from ending, so that it continues to run until the program is stopped.
When everything has been tested and is working as expected, the last step is to make the code run each time the Pi boots up.
[Unit] Description=Sound Switch After=multi-user.target [Service] ExecStart=/usr/bin/python3 -u room_music.py WorkingDirectory=/home/pi Restart=always User=pi [Install] WantedBy=multi-user.target [room-music.service – systemd service file]
sudo cp room-music.service /etc/system/system
sudo systemctl enable room-music.service
You should now be greeted with music each time you open the door.
The switch circuit and Python code illustrate how to use sensor inputs to trigger outputs like the LED and the music player. With some basic components and a few lines of Python code it’s possible to create a fairly sophisticated project.
All of these techniques can be taken and used in other more advanced systems.
By extending the code, rather than play music, the system could raise an alarm and even be connected to the cloud to send alerts as an IoT enabled security system.
You can replace the reed switch with an ultrasonic sensor or an RFID tag to have different ways to detect activity. There are lots of possibilities…