This simple project builds a motion activated sound player. Each time motion is sensed, an MP3 file is played. I built this originally to play a random sound for my B1 Battle Droid (from the awesome Sean Fields at Project842...look him up on Facebook!), but it can be used in pretty much any project that requires motion detection.
The following components are needed (links are my Amazon affiliate links ;) ):
- Arduino Uno, Nano or Mini.
- PIR (Passive Infrared) sensor such as HC-SR501 or HC-SR505
- DFPlayer Mini (for MP3) and Micro SD card
- Amp board or circuit (such as a PAM8403) - this is optional, but will give you much more volume.
- Speaker
The DFPlayer has a built-in amp, but in a crowded room it might not be loud enough. Fortunately, it also has two (2) DAC outputs that you can run to an amplifier.
Follow the wiring diagram to build the sensing circuit. Then edit, compile and load the Arduino sketch. You'll need to install the DFPlayer libary in your Arduino IDE (you can also use the Library Manager in the Arduino IDE and search for 'DFPlayer.')
You'll need to edit a couple settings in the sketch to match your setup:
byte maxRnd = 20;
This is the maximum number of files your have loaded on your SD card. For example, with this setting, your MP3 files should be named sequentially from 0001.mp3 to 0020.mp3.
byte volume = 30;
This is the volume for the DFPlayer. It affects BOTH the speaker lines out AND the DAC lines. You could also add a potentiometer and modify the sketch to read that and set the volume, but if you add an amplifier board with a volume control, just set this to 30 (max) and use the volume control on the amplifier.
The DFPlayer is a great mult-purpose MP3 player and is easy to use, but it DOES have some quirks:
-
Files must be named with leading zeros. For example: 0001.mp3 or 0020.mp3
-
Despite the name of the file, DFPlayer will index the files according to the timestamp on the file, so you have to copy the files to the SD card in order from 0001 to ?? so that they are indexed the same as their file name.
-
If you copy the files using a Mac, it will also copy some hidden files that you'll need to clean by running:
dot_clean /Volumes/{name of your SD card}
By default, the sketch uses the maxRnd
variable to calculate a range (from 1 to maxRnd) and plays a random sound each time the sensor is triggered, being careful not to play the last sound again.
Alternatively, you can comment out this portion of the code and uncomment the line:
DFPlayer.next();
to just keep looping through the sounds in order each time the sensor is triggered.
Depending upon the sensor you use, some things to keep in mind:
- For the smaller HC-SR505 sensor, you have a much smaller sensor area, but a shorter warmup time.
- For the larger HC-SR501, you have a much larger sensor area and a longer warmup time (1 to 3 minutes depending on the brand, etc.) and can adjust both the sensitivity and the delay time until the sensor resets. You should keep the jumper set to single trigger.
For both sensors, once it is triggered, it will report that it's being triggered for several seconds, then go into a standby mode, then reset after a set number of seconds.
The sketch takes this into account so that only the very first report of a trigger is used to kick off the sound play, and then once the sound is finished, a flag is reset to let the the sketch know it's ready to play another file when the sensor is tripped. If the sensor is tripped or is still reporting being tripped while a sound is playing, it will be ignored. This stops a bunch of sounds from being played at once due to the repeated reports of the sensor being triggered.
Below is a diagram of the circuit wiring. I would suggest putting all of this on a prototyping board to make it easier to install in your setup.
Alternate diagram if no external amplifier is used: