How to Make an OwlBot: The Bird Intimidator – Part 2: Making Sounds
Project Name: | OwlBot |
Project Description: | Part 2 build of the OwlBot, where we add an MP3 player to the OwlBot prototype to give it the ability to make sound when motion is detected by the PIR sensor that was installed in Part 1 of the build. |
Project Difficulty: | Easy |
Project Note: | For this second part of the project, we’ll focus on the OwlBot’s sound capabilities using an MP3 player module. |
Introduction
In this project we’re going to be performing the second step of what will eventually be the OwlBot. The OwlBot will be a device that can be used as a bird intimidation tool to scare away pesky birds in the yard, around the house or barn, at restaurants, or in trees, bushes, and gardens. Hence, the phrase, “The Bird Intimidator”.
The OwlBot (a.k.a. “The Bird Intimidator”) we’ll be making will be done in several parts. The OwlBot is an ambitious project. When complete, the OwlBot will sense motion. When motion is detected, the OwlBot should make owl sounds, perform varying movements, and flash its red eyes to intimidate and scare away pests. To put together all these tasks in one page would be too long and cumbersome, so we’ve decided to split each process we want the OwlBot to do into easy to accomplish chunks or parts as we continue on our goal to complete the OwlBot.
This is Part 2
This is part two. This second part of the OwlBot project will involve setting up its ability to make owl sounds once motion has been detected via the passive infrared sensor (PIR) we set up with our Arduino Uno in Part 1 of this OwlBot project series.
Our goal at the end of this part of the project will be to have the MP3 player, the PIR sensor, and the Arduino Uno communicating with each other so that when motion is detected the MP3 player will play owl sounds over a pair of speakers. Again, we’ll use the serial port on the Arduino IDE to print out whether or not motion is detected.
Goal of Part 2 of the OwlBot Project
A motionless, plastic great horned owl deterrent with a staring gaze might not do much over time. For the OwlBot to be effective enough to deter birds from nesting in an area throughout the year requires lifelike actions, like sound and movement. Our goal for Part 1 of the OwlBot project was to first get it to detect motion:
- Motion Sensing: When the OwlBot is complete, we want it to be able to sense motion of a bird or other creatures, and when it does to start making owl hooting sounds. We also want it to perform various movements, as well as flash its red eyes as an added effect to help deter pesky animals in the surrounding area. We’ll end up having all these actions done in sync when motion is detected, and we’ll use an HC-SR501 PIR sensor to get the OwlBot to detect motion.
Our goal for Part 2 of the OwlBot project is to get it to play owl sounds when motion is detected by the PIR sensor, and then we’ll move on to the other stuff later, in the proceeding parts of this build series:
- Sound: We want the OwlBot to make owl sounds to notify any birds around that there’s an owl in the vicinity and that they should get the heck outta dodge. This purpose is to deter the birds from nesting close by. We’ll be using the DFRobot DFPlayer Pro Mini MP3 Player to store an audio file of owl sounds to be played. We don’t want the OwlBot to be hooting non-stop all day and night though, so we’ll use the PIR sensor to help the OwlBot sense when a bird has come near it. When a bird comes within the OwlBot’s field-of-view, then it can start making its owl calls. Of course, the OwlBot will not quite be smart enough to distinguish between a human passing by or a bird, but it’ll work fine, so we’ll just ignore those details, for now.
DFPlayer Pro Mini MP3 Player
The DFRobot DFPlayer Pro Mini MP3 Player (DFR0768) is an MP3 player module that allows you to import MP3 files (and other file types) onto its onboard memory. The DFPlayer Pro supports four controlling modes: Arduino, AT command, on-board buttons, and ADKEY. We’ll be using the Arduino Uno for this project.
We’ll be using the DFPlayer Pro to store an MP3 file that contains owl sounds for the OwlBot. So, when motion is detected, this is where the sounds are coming from. The DFPlayer Pro has 128MB of storage, plenty of space for the MP3 file we’ll be using.
The DFPlayer Pro also provides dual-channel capability, which will allow us to use a couple of small speakers to spread the sound output that our OwlBot will produce.
The DFPlayer Pro has a very compact footprint, about an inch-square. It’s super easy to download a file to it; I just plugged a USB into it and into my computer, opened its file, dragged an MP3 file into its folder, and then I was done. It’s that simple!
To get the details on the PIR sensor and the Arduino Uno we’re using in this project, you can go to Part 1 of the OwlBot project here.
Alternative MP3 Player
As an alternative to the DFRobot DFPlayer Pro Mini MP3 Player (DFR0768), you can use an older version of this family of MP3 players by DFRobot, and that’s the DFPlayer Mini MP3 Player (DFR0299). The DFPlayer Mini is a little cheaper than the DFPlayer Pro, but its configurations and hookups are a little different. I’ve provided links below under “Programming the Arduino to Make Sounds” of people who have given excellent tutorials on how to use the DFPlayer Mini that’ll help you get setup for this project perfectly, if you choose to use the DFPlayer Mini.
Let’s Be Methodical
I like to be very methodical about things, so we’ll go step-by-step — in detail — on the entire build of this project: the parts we’ll need, why we’re using them, how to connect them, how they work, and more! First, we’ll start with the following parts list:
Parts List
(This parts list is a continuation of the previous parts list here from Part 1 of the OwlBot project.)
Item | Quantity | Description |
DFRobot DFPlayer Pro Mini MP3 Player (DFR0768) | 1 | We’ll use this MP3 player to store the audio file we will be using in order to play it later. |
3 Watt – 8 Ohm Mini Speakers | 2 | We’ll use two of the speakers to hear the owl sounds from the DFPlayer Pro. |
1 kΩ Resistor | 1 | This resistor connects between pin 2 of the Arduino and the RX pin of the DFPlayer Pro. |
USB Type-C to USB Type-A Data Transfer Cable | 1 | We need this type of cable to transfer an MP3 file from our computer to the DFPlayer Pro. |
Male-to-Male Jumper Wires | Various Colors and Sizes | We’ll use these to make solderless connections to our components on the breadboard. |
Tools Used in Project
Item | Description |
The Ultimate DIY 3220-Point Breadboard (Optional) | If you haven’t built your own, go to our page here to do so! Otherwise, you can use another type of 3220-point breadboard or 830-point breadboard. |
Soldering Iron | We need the soldering iron to solder components for our project later. |
Solder | I used the MG Chemicals rosin core leaded solder with a diameter of 0.025” — Sn63/Pb37 |
Soldering Iron Tip Cleaner | I use an Aoyue soldering iron tip cleaner with brass wire sponge, but in the past I just used a regular damp dish sponge to clean my solder tips. |
Mini Side Cutters | We’ll be using the cutters to cut things like excess header pins and wire. |
Prototyping the Circuit
Previously, for Part 1 of the OwlBot prototype we successfully made all the connections needed for the Arduino Uno and HC-SR501 PIR sensor and created the code to detect motion. Now we need to add to this prototype the DFRobot DFPlayer Pro Mini MP3 player and speakers. We also need to update the code and add to it the functionality we need to get the MP3 player to play an MP3 file of owl sounds to the speakers when motion is detected by the PIR sensor.
Again, like I mentioned in Part 1 of this build, I will be using The Ultimate DIY 3220-Point Breadboard that was made from a separate project. I may refer to The Ultimate DIY 3220-Point Breadboard as “TUDIY” throughout this project to save me from having to say it every time.
DON’T WORRY! Even though I’ll be discussing how to make connections on TUDIY throughout the following prototyping steps, I’ll provide images of how to hook up items on a generic breadboard, so you can still follow along!
The Ultimate DIY 3220-Point Breadboard (TUDIY)
Soldering the Header Pins to the DFPlayer Pro
It’s possible that when you ordered your DFPlayer Pro Mini MP3 player that it came with its header pins not already soldered to the module board, like mine. So, we’ll have to solder the header pins to the board of the DFPlayer Pro before we can make any connections to it. To accomplish this, we’ll use the help of a breadboard to hold our header pins and DFPlayer Pro’s board in place, so that we can solder the pins to the board. We’re going to need the following:
- The DFPlayer Pro board and its provided header pins.
- A solderless breadboard.
- A soldering iron, soldering iron tip cleaner, and solder.
I’ve provided a video below that walks you through each of the following steps.
Step 1 – Trimming the Header Pins:
First, take the header pins and place the shorter pin ends through the through holes from the bottom side of the DFPlayer Pro’s board. If you need to trim the header pins that were provided to you, you may do so using cutters. Just make sure that you’re trimming the header pins to match the amount of pins required to fit through all the through holes on your DFPlayer Pro board. In this case, I needed to trim the header pins down to six pins for each side of the DFPlayer Pro board.
Step 2 – Trick to Soldering Header Pins to the DFPlayer Pro:
While holding the header pins in place in the through holes of the DFPlayer Pro board, press the longer pin ends that are facing down from the bottom side of the board into the points or holes of the breadboard. It doesn’t matter where you place the header pins into the holes on the breadboard, just make sure that your DFPlayer Pro board is positioned properly onto the header pins and that the top of the DFPlayer Pro board is facing up.
Step 3 – Soldering the Header Pins to the DFPlayer Pro:
Now all there’s left to do is to solder the pins to the board. Make sure you’re careful not to keep the tip of your solder iron onto the board for too long. You could damage the board’s copper contacts if too much heat is applied to them. Also, avoid making contact with anything else on the board with your solder iron, like the IC chips. Too much heat could damage them too.
Once you have the header pins soldered to the DFPlayer Pro board, it should be ready to accept files to be downloaded to it. We’ll do this in the proceeding steps. Below is a short video to walk you through the same steps listed above for soldering the header pins to the DFPlayer Pro:
Connecting the DFPlayer Pro for Sound
In the next steps we’ll seat the DFPlayer Pro onto the breadboard, make the proper connections, load an MP3 file onto it, and connect the speakers.
Step 4 – Seating the DFPlayer Pro Onto the Breadboard:
In Part 1, we connected our PIR sensor to the breadboard and its power rail, and to the Arduino. Now that we have the DFPlayer Pro ready, we can now place or seat the DFPlayer Pro Mini MP3 Player onto the breadboard and make connections to it using jumper wires. I’ve placed the DFPlayer Pro onto points b6 through b11 and points h6 through h11 onto TUDIY.
- Seat the DFPlayer Pro Mini MP3 Player onto points b6 – b11 and points h6 – h11 on the breadboard.
Step 5 – Connecting the DFPlayer Pro to Power:
Now that we’ve seated the DFPlayer Pro Mini MP3 Player to the breadboard, we now need to connect the MP3 player to the power rails. I’ll be using a red and black jumper wire to make the connections for power.
- Take a red jumper wire and place one end of the jumper wire in a point connecting it to the VIN pin of the DFPlayer Pro. Next, connect the other end of the red jumper wire to a point on the positive (+) power rail from the Arduino’s 5V supply of the breadboard we set up in Step 3 in Part 1 of the OwlBot project.
- Take a black jumper wire and place one end of the jumper wire in a point connecting it to the GND pin of the DFPlayer Pro. Next, connect the other end of the black jumper wire to a point on the negative (-) power rail from the Arduino’s ground supply of the breadboard we set up in Step 3 in Part 1 of the OwlBot project.
Step 6 – Connecting the DFPlayer Pro’s RX and TX Pins:
Next, let’s go ahead and get the DFPlayer Pro’s RX and TX pins connected to the Arduino Uno. The TX or transmission pin of the DFPlayer Pro transmits data from the MP3 player to the Arduino’s receiver pin. The RX or receiver pin of the DFPlayer Pro receives data from the Arduino’s transmission pin.
Potential Issue:
You could connect the DFPlayer Pro’s TX and RX pins to the Arduino’s RX and TX pins directly to send and receive data, but in doing so we run into an issue. The Arduino Uno only has one serial port; those are the RX and TX pins of the Arduino. This kinda presents a problem. The Arduino’s USB port and the serial port are tied together. This means that when you’re trying to compile code and upload it to the Arduino from its USB port, and you have the Arduino’s RX and TX pins (it’s serial port) connected to something else, you can run into issues when uploading your code. We’d have to remove the wires to the serial port each time we’d need to load code to the Arduino, then replace them again. Not fun.
Solution to the Issue:
To get around this issue, we’ll connect the jumper wires coming from the RX and TX pins of the DFPlayer Pro to pins 2 and 3 of the Arduino, respectively, then put into our code for those pins to be able to handle transmitting and receiving data, later when we get to that step. Much better.
I’ll be using male-to-male blue and orange jumper wires to make the connections from the Arduino to the DFPlayer Pro.
- Connect the male end of the blue wire to point a8, inline with the RX pin of the DFPlayer Pro Mini MP3 Player. Next connect the other male end of the same blue jumper wire to pin 2 of the Arduino.
- Connect the male end of the orange wire to pin 3 of the Arduino. Next, connect the other male end of the same orange jumper wire to the TX pin of the DFPlayer Pro Mini MP3 Player. In my case, point a9 is inline with the TX pin of the DFPlayer Pro on the breadboard.
Step 7 – Loading an MP3 File Onto the DFPlayer Pro:
Now that we have the DFPlayer Pro set on the breadboard and hooked up, let’s download an MP3 file onto it that contains some owl sounds. You can search the internet for all sorts of free owl sounds. I chose my owl sound off the website freesound.org, where I found this MP3 file for the owl sound that I ended up loading onto my DFPlayer Pro.
Source | File | File Type | Creator | License |
freesound.org | 92640__cgeffex__hoot-owl2.mp3 | MP3 | CGEffex | CC by 3.0 |
Once you’ve downloaded the MP3 file onto a dedicated folder onto your computer, loading the MP3 file onto the DFPlayer Pro Mini MP3 player is very easy. Let’s go through all the steps on what to do to get your DFPlayer Pro setup for the owl sounds from freesound.org:
- Create an account at freesound.org, if you don’t already have one.
- Go to the location of the owl MP3 file here.
- Download the MP3 file and save it to a dedicated folder on your computer. I made a folder called “Owl” on my desktop and saved the MP3 file there. So, my folder location looks something like the following:
- C:\Users\My_Computer\OneDrive\Desktop\Owl
- Get a USB Type-C to USB Type-A data transfer cable.
- Connect the USB Type-C end of the cable to your DFPlayer Pro Mini MP3 Player.
- Connect the USB Type-A end of the cable to your computer.
- Open the folder that has the downloaded MP3 file of the owl sounds you downloaded from freesound.org. In my case, I saved my MP3 file in a folder similar to the following file location:
- C:\Users\My_Computer\OneDrive\Desktop\Owl
- Open a second folder up on your computer of the DFPlayer Pro Mini MP3 Player’s drive location. The location of my DFPlayer Pro was under the USB Drive (F:). Yours may be different.
Changing MP3 File Name
I ended up changing the MP3 file name from “92640__cgeffex__hoot-owl2” to “001__hoot-owl”, although this is not shown in the .gif image below. I did this in anticipation of possibly adding more files to my DFPlayer Pro later. You can have the DFPlayer Pro cycle through files when playing them and I figured that having a number system at the beginning of the file may help in that process. This is not necessary for this project though, so you can keep the name of the file or change it to whatever name you want it to be.
- Now all you need to do is drag-and-drop the MP3 file from the folder you downloaded it to, to the location of the DFPlayer Pro’s file. In my case, I moved my MP3 file from the Owl folder I created on the desktop to the F: drive where the DFPlayer Pro is located:
- FROM: C:\Users\My_Computer\OneDrive\Desktop\Owl
- TO: F:\
- Your MP3 file with the owl sound is now loaded to the DFPlayer Pro Mini MP3 Player and is ready to go! That’s it!
- You can now unplug the DFPlayer Pro from your computer.
Below is the video companion of this step to help those of whom are visual learners.
Step 8 – Connecting the Speakers to the DFPlayer Pro:
Now we’re ready to connect the speakers to the DFPlayer Pro Mini MP3 Player. Let’s grab some male-to-male jumper wires and two 3W – 8Ω speakers to start making connections to the speakers and to the Arduino. First, let’s connect to the speakers. I’m using whatever color jumper wires I have available, so I’ll be using a yellow jumper wire and an orange jumper wire to make connections from the DFPlayer Pro to a speaker, for each speaker.
If your speaker wires are capable of connecting directly to the breadboard, then you don’t need to use jumper wires. You can just skip to the second bullet-point below to connect the red and black wires of the speakers directly to the breadboard. My speakers came equipped with a type of JST connector on the end of their wires, so I’m using jumper wires to make my connections from the speakers to the breadboard.
- Taking a speaker and your jumper wires, connect a yellow jumper wire to the black wire of the speaker and connect an orange jumper wire to the red wire of the speaker. Do this for both speakers.
- Take one speaker and connect the orange jumper wire that’s connected to the speaker’s red wire and connect it to the R+ pin of the DFPlayer Pro. This happens to be at point i9 on my breadboard. Next, connect the yellow jumper wire that’s connected to the speaker’s black wire and connect it to the R- pin of the DFPlayer Pro. This is at point i8 on my breadboard.
- Take your other speaker and connect the orange jumper wire that’s connected to the speaker’s red wire and connect it to the L+ pin of the DFPlayer Pro. This happens to be at point i11 on my breadboard. Next, connect the yellow jumper wire that’s connected to the speaker’s black wire and connect it to the L- pin of the DFPlayer Pro. This is at point i10 on my breadboard.
Programming the Arduino to Make Sounds
Now that we’ve made our connections to the DFPlayer Pro, we’re set up for making sounds for the OwlBot. It’s time to add more to the code from Part 1 of the OwlBot prototype build, and give the ability for the Arduino to send a signal to the MP3 player to play its MP3 file to be heard on the speakers we added when motion is detected by the PIR sensor.
In keeping full transparency, I will be using the help of some fantastic people who have offered their knowledge of Arduino and of the DFPlayer MP3 player modules freely on the internet, and will be referencing those sources as we continue with this project. As the program evolves, any sources used will be added into the Arduino code comments. It is encouraged that you go check out any person mentioned for yourself.
DFPlayer Pro References
DFPlayer Pro Reference 1: Toms Trains and Things (YouTube): “How I Setup & Program DFPlayer Pro With Arduino Uno”
DFPlayer Pro Reference 2: ModelSceneryTutorials (YouTube): “DFPlayer Pro Voice Prompt Removal: A Comprehensive Tutorial To Unlocking the Full Potential”
DFPlayer Mini References
DFPlayer Mini Reference 1: Rachel De Barros (YouTube): “Motion-activated Sound Effects with Arduino, PIR Sensor & MP3 Player“
DFPlayer Mini Reference 2: The Last Outpost Workshop (YouTube): “Sound problems with the MP3-TF-16p DFPlayer Mini?”
The following is the code we’ll write for the Arduino Uno to accomplish the tasks needed for our OwlBot prototype, so far:
/* ------------------------------------------------------------------------------
* Project: OwlBot: The Bird Intimidator - Part 2: Making Sounds
* Written by: Dustin Hodges (Motbots)
* Date Created: 10/11/2024
* Date Last Modified: 12/23/2024
* Description: This is part two of the OwlBot project and its prototype process. Here, we
* are adding to our code from Part 1 of the build the functionality to use a DFPlayer Pro
* MP3 Player with our OwlBot prototype as it is constructed so far. The idea behind this
* update in the code is to get the DFPlayer Pro to play owl sounds to speakers from a saved
* MP3 file we uploaded to it when motion is detected from the HC-SR501 PIR sensor we used
* from Part 1 of this build.
*
* Microcontroller Board: Arduino Uno R3
* IDE Version: Arduino IDE 2.3.4
* Modules Used: HC-SR501 PIR sensor, DFRobot DFPlayer Pro Mini MP3 Player
*
* ------------------------------- REFERENCES -----------------------------------
* DFPlayer Pro Reference 1: Toms Trains and Things (YouTube): “How I Setup & Program DFPlayer
* Pro With Arduino Uno”
*
* DFPlayer Pro Reference 2: ModelSceneryTutorials (YouTube): “DFPlayer Pro Voice Prompt Removal:
* A Comprehensive Tutorial To Unlocking the Full Potential”
*
* DFPlayer Mini Reference 1: Rachel De Barros (YouTube): "Motion-activated Sound Effects with
* Arduino, PIR Sensor & MP3 Player"
*
* DFPlayer Mini Reference 2: The Last Outpost Workshop (YouTube): “Sound problems with the
* MP3-TF-16p DFPlayer Mini?”
* ------------------------------------------------------------------------------
*/
#include <DFRobot_DF1201S.h> // The DFRobot_DF1201S library for the DFPlayer Pro Mini MP3 Player
#include <SoftwareSerial.h> // The SoftwareSerial library allows serial communication on
// other digital pins of an Arduino board.
/* The follwing is the initial setup of variables for the HC-SR051 PIR sensor. */
const int PIR = 12; // Signal pin from PIR sensor to pin 12 on Arduino
const int RX = 3; // This is what we'll designate as the RX pin on the Arduino (not the DF0768's RX pin)
const int TX = 2; // This is what we'll designate as the TX pin on the Arduino (not the DF0768's TX pin)
int motionStatus = LOW; // Current motion status or signal pin reading (0 = LOW, 1 = HIGH).
// Initially set to 0 or LOW.
int pirState = LOW; // The state of the PIR signal pin (HIGH/LOW). Initially set to LOW.
SoftwareSerial soundFxSerial(RX, TX); // Creating an object for our sound effects serial, saying which pins to use
// for receiving (RX) and transmission (TX).
// The SoftwareSerial is used to communicate with the DFPlayer Pro module.
DFRobot_DF1201S soundFxPlayer; // We'll use this object when referring to the sound effects player (MP3 player).
/* The setup function does all the initial setting up of everything we need. When code is
compiled and ran, the setup function is only entered into during processing once. */
void setup() {
// Pin modes
pinMode(PIR, INPUT); // Pin 12 on Arduino (signal line from PIR) is set as an input pin.
pinMode(RX, INPUT); // Our receiving pin for the Arduino needs to be an input.
pinMode(TX, OUTPUT); // Our transmission pin for the Arduino needs to be an output.
// Serial setup
Serial.begin(115200); // Want to be able to use Serial Monitor for debugging purposes.
soundFxSerial.begin(115200); // Setting up the serial for the sound effects serial.
soundFxPlayer.begin(soundFxSerial); // Telling the sound effects player to use the sound effects serial.
// Player setup
soundFxPlayer.setVol(30); // Set volume for the sound effects player.
soundFxPlayer.switchFunction(soundFxPlayer.MUSIC); // Enter music mode.
soundFxPlayer.setPrompt(false); // Silence voice prompt at start.
delay(2000); // Delay for two seconds to allow everything to calibrate
// before proceeding.
soundFxPlayer.setPlayMode(soundFxPlayer.ALLCYCLE); // Set playback mode to "repeat all"
}
/* The loop function is an infinite loop, meaning once the code is compiled and ran, the
processes of the code will continually run what's within the loop, until the processes are
stopped, either within the code, or by the user, or by some other act of God. */
void loop() {
motionStatus = digitalRead(PIR); // Get the motion status from pin 12 (Is the signal HIGH or LOW?)
/* If there's motion detected by the PIR sensor, do what's inside the loop. */
if(motionStatus == HIGH) {
// If the pirState is LOW (it's initially set to LOW)
if(pirState == LOW) {
Serial.println("Motion has been detected!"); // Print message to serial monitor.
/* Going to need to change the pirState to HIGH, since motion has been detected. */
pirState = HIGH;
if(!soundFxPlayer.isPlaying()) { // If the owl FX not playing...
playOwl(); // Play the owl MP3 audio.
}
}
}
/* Else, if no motion has been detected by the PIR sensor, do what's inside here: */
else {
// If the pirState is HIGH
if(pirState == HIGH) {
Serial.println("No motion detected."); // Print message to serial monitor.
/* Going to need to change the pirState to LOW, since no motion has been detected. */
pirState = LOW;
if(soundFxPlayer.isPlaying()) { // If the owl FX is still playing...
pauseOwl(); // Make absolute sure we pause the owl.
}
}
}
}
/* The playOwl() function is called when we want to play the owl MP3 file to make owl sounds. */
void playOwl() {
uint16_t fileNum = soundFxPlayer.getCurFileNumber(); // Getting the current file number in our MP3
// player and setting it to the variable 'fileNum'.
soundFxPlayer.playFileNum(1); // Play file #1, the numbers are arranged according to the sequence
// of the files copied into the U-disk.
soundFxPlayer.setPlayTime(0); // Starts the file 0 seconds in (or at the very beginning).
soundFxPlayer.start(); // Start playing sound FX.
// The following for loop allows the owl sounds to play for 10 seconds.
for(int i = 10; i > 0; i--) {
Serial.println(i);
delay(1000); // Wait one second.
}
pauseOwl(); // Pause the owl's FX as soon as we're done playing the FX.
}
/* The pauseOwl() function is called when we need to pause the audio playing from the MP3 file. */
void pauseOwl() {
soundFxPlayer.pause(); // Pause the sound FX player.
}
The Code Explained
For this part, I will only go over the code we’ve added here for Part 2 of the OwlBot prototype build. If you need an explanation of the other parts of the code which we did in Part 1 of the prototyping process, please visit our page How to Make an OwlBot: The Bird Intimidator – Part 1: Motion Sensing.
Referring to the code above, let’s walk through some key lines to understand what’s going on:
Importing the DFRobot Library
#include <DFRobot_DF1201S.h> // The DFRobot_DF1201S library for the DFPlayer Pro Mini MP3 Player
We need to import the DFRobot_DF1201S library in order to use the prebuilt functions designed to use for the DFPlayer Pro Mini MP3 Player. You can read up on this library here.
Creating and Initializing the Variables
const int RX = 3; // This is what we'll designate as the RX pin on the Arduino (not the DF0768's RX pin)
The RX variable is what we’ll designate as the RX pin on the Arduino, pin 3 in this case, not the DFPlayer Pro’s RX pin.
const int TX = 2; // This is what we'll designate as the TX pin on the Arduino (not the DF0768's TX pin)
The TX variable is what we’ll designate as the TX pin on the Arduino, pin 2 in this case, not the DFPlayer Pro’s TX pin.
Creating the Serial Objects
SoftwareSerial soundFxSerial(RX, TX); // Creating an object for our sound effects serial, saying which pins to use
// for receiving (RX) and transmission (TX).
// The SoftwareSerial is used to communicate with the DFPlayer Pro module.
Here, we’re creating an object for our sound effects serial, saying which pins to use for receiving (RX) and transmission (TX). The SoftwareSerial is used to communicate with the DFPlayer Pro module.
DFRobot_DF1201S soundFxPlayer; // We'll use this object when referring to the sound effects player (MP3 player).
We’ll use the soundFxPlayer object when referring to the sound effects player (the MP3 player).
Pin Modes
In the setup, we’ll initialize the pin modes for each of the pins we’re using on the Arduino Uno for the MP3 player module:
pinMode(RX, INPUT); // Our receiving pin for the Arduino needs to be an input.
The RX variable is our receiving pin (pin 3) for the Arduino and it needs to be set as an input.
pinMode(TX, OUTPUT); // Our transmission pin for the Arduino needs to be an output.
The TX variable is our transmission pin (pin 2) for the Arduino and it needs to be set as an output.
Begin Serial Setup
The following is for our Serial setup:
Serial.begin(115200); // Want to be able to use Serial Monitor for debugging purposes.
We want to be able to use Serial Monitor for debugging purposes. Here, we’ve changed the baud rate to be 115200. This baud rate was chosen instead of the typical 9600 baud rate because during the prototype and testing stage of this part of the build gave unintelligible output readings to the serial monitor using a baud rate of 9600. This had something to do with the use of the DFPlayer Pro Mini MP3 Player. After referring to DFRobot’s website and looking over the DFPlayer Pro’s coding samples, I found that they were using a baud rate of 115200. Having a baud rate of 115200 fixed this issue and I was able to read the outputs printed clearly onto the serial monitor.
soundFxSerial.begin(115200); // Setting up the serial for the sound effects serial.
Here, we’re setting up the serial for the soundFxSerial object. Again, the baud rate value was found at DFRobot’s website where they gave coding samples for the DFPlayer Pro.
soundFxPlayer.begin(soundFxSerial); // Telling the sound effects player to use the sound effects serial.
Here, we’re telling the sound effects player to use the sound effects serial. Remember, the soundFxSerial said which pins to use for transmission (TX) and receiving (RX) for the Arduino. The soundFxPlayer refers to the MP3 player. So, we’re saying we want the MP3 player to listen for signals from the Arduino’s serial port we made from pins 2 (TX) and 3 (RX).
Initial Setup for the DFPlayer Pro Mini MP3 Player
For the following functions used, you may refer to DFRobot’s website for the DFPlayer Pro’s documentation here. Now, we want to establish the initial setup for the MP3 player:
soundFxPlayer.setVol(30); // Set volume for the sound effects player.
Here, we want to set volume for the sound effects player (MP3 player). The volume range is from 0 to 30. I initially set the volume to its highest volume setting. You can choose whatever value you prefer.
soundFxPlayer.switchFunction(soundFxPlayer.MUSIC); // Enter music mode.
We are entering in music mode here.
soundFxPlayer.setPrompt(false); // Silence voice prompt at start.
When the MP3 player initially powers up, a voice prompt is automatically played. Here, we pass the false argument to the setPrompt() function call, silencing the voice prompt at the start. We don’t want a talking voice coming from our OwlBot every time it turns on.
delay(2000); // Delay for two seconds to allow everything to calibrate
Here, we’re just setting a delay for two seconds to allow everything to calibrate before proceeding.
soundFxPlayer.setPlayMode(soundFxPlayer.ALLCYCLE); // Set playback mode to "repeat all"
Here, we want to set the playback mode to “repeat all” (ALLCYCLE).
Inside The Loop
motionStatus = digitalRead(PIR); // Get the motion status from pin 12 (Is the signal HIGH or LOW?)
Get the motion status from pin 12. Is the signal HIGH or LOW?
/* If there's motion detected by the PIR sensor, do what's inside the loop. */
if(motionStatus == HIGH) {
// If the pirState is LOW (it's initially set to LOW)
if(pirState == LOW) {
Serial.println("Motion has been detected!"); // Print message to serial monitor.
/* Going to need to change the pirState to HIGH, since motion has been detected. */
pirState = HIGH;
if(!soundFxPlayer.isPlaying()) { // If the owl FX not playing...
playOwl(); // Play the owl MP3 audio.
}
}
}
If the motionStatus is HIGH, meaning the sensor detects motion, and the pirState is LOW, remember we initialized the pirState to be LOW, then print to the serial monitor that motion was detected. If motion is detected, and the sound from the MP3 player is not playing, then play the owl sounds from the MP3 file on the MP3 player by callying the playOwl() function.
/* Else, if no motion has been detected by the PIR sensor, do what's inside here: */
else {
// If the pirState is HIGH
if(pirState == HIGH) {
Serial.println("No motion detected."); // Print message to serial monitor.
/* Going to need to change the pirState to LOW, since no motion has been detected. */
pirState = LOW;
if(soundFxPlayer.isPlaying()) { // If the owl FX is still playing...
pauseOwl(); // Make absolute sure we pause the owl.
}
}
}
Else, if the motionStatus is LOW or not HIGH, and the pirState is HIGH, then print to the serial monitor that motion is not detected, then set the pirState to LOW. While we’re at it, if the MP3 is playing owl sounds right now, then pause those sounds for now.
The playOwl() Function
The playOwl() function is a function we create ourselves to play the sounds loaded onto the MP3 player.
uint16_t fileNum = soundFxPlayer.getCurFileNumber(); // Getting the current file number in our MP3
// player and setting it to the variable 'fileNum'.
Here, we’re getting the current file number within our MP3 player and setting it to the variable fileNum. Each file on the MP3 player will have its own value. For this project, we’re only using one file, the owl sound file we loaded onto the MP3 player from before. You could have other files that play other sounds on your MP3 player module.
soundFxPlayer.playFileNum(1); // Play file #1, the numbers are arranged according to the sequence
// of the files copied into the U-disk.
Play the first file. The numbers are arranged according to the sequence of the files copied into the U-disk on the MP3 player module.
soundFxPlayer.setPlayTime(0); // Starts the file 0 seconds in (or at the very beginning).
The setPlayTime function starts the file 0 seconds in or at the very beginning. You could change this value if you feel it necessary.
soundFxPlayer.start(); // Start playing sound FX.
The start function starts playing sounds on the MP3 player.
// The following for loop allows the owl sounds to play for 10 seconds.
for(int i = 10; i > 0; i--) {
Serial.println(i);
delay(1000); // Wait one second.
}
A simple way to allow the MP3 player to play the owl sound for 10 seconds is by using a for loop. At this point in the code, the for loop will cycle through ten times, starting from 10 going down to 1, each time printing out the value for i to the serial monitor, having a delay for 1 second (1000 milliseconds) for each iteration within the loop.
pauseOwl(); // Pause the owl's FX as soon as we're done playing the FX.
The pauseOwl() function is a function we create ourselves. It is used to pause the owl sounds as soon as we’re done playing the sound effects.
The pauseOwl() Function
soundFxPlayer.pause(); // Pause the sound FX player.
The only thing we’re doing here is using the pause function in the DFRobot library for the DFPlayer Pro, to pause the audio coming from the MP3 player.
Compiling and Uploading the Code to Arduino
Now, that we’ve gone over the code we’re using to get the DFPlayer Pro MP3 Player, the PIR sensor and Arduino to talk to each other, you can either tediously write the code yourself, or just copy-and-paste it into your file on the Arduino IDE. I’ve named my file owlbot_part-2.ino. You can name yours whatever you want.
If you need or want to see the process of copying and pasting the code into the Arduino IDE, connecting the Arduino to a computer, choosing the board and port under tools in the IDE, verifying code, and fixing code errors, then check out Part 1 of this OwlBot project here, because these processes are similar for Part 2.
Testing What We’ve Done
At this point, you have your prototype for step two of this project complete and you have completed writing your code for this step of the prototyping process. You’ve also copied the code, verified it, and fixed what errors you may have had after doing so. You’ve then compiled and uploaded the code to the Arduino Uno to see if what you’ve done so far even works! Here are the steps again, to test what you’ve done up to this point in the project:
- Set up the prototype MP3 player circuit of the OwlBot project.
- Create the code for the Arduino Uno in the Arduino IDE.
- Verify the code and fix any errors.
- Compile and Upload the code to the Arduino Uno using the provided USB cable that came with the Arduino.
- Leave the Arduino connected to your computer once you’re done compiling and uploading the code to the Arduino. Start waving your hand in front of the PIR sensor. Once motion is detected, the MP3 player should start playing the owl sounds audio to the speakers.
Once you’ve compiled and uploaded your code to the Arduino, and have started waving your hand in front of the PIR sensor, you should start hearing the owl sounds playing from the MP3 player to the speakers and see the printouts on the serial monitor telling you whether motion is detected or is not detected.
If everything is working as it should, then congratulations! You’ve successfully completed the second step of the OwlBot prototype. Check out the video below to see the full prototyping process, as well as to see this part of the project working!
In step 3 of the OwlBot project, we’re going to be adding a couple of red LEDs to our circuitry, to allow for blinking red eyes on the OwlBot. When motion is detected we’ll be able to hear owl hooting sounds and see flashing red LEDs! So, stick around for more!
Improving Sound Quality on the DFPlayer Pro
After compiling and running the code in the Arduino IDE, the Arduino Uno is ready. When testing out the PIR motion sensor by waving your hand in front of it, this time we should hear the owl sounds playing from the MP3 player to the speakers.
You may notice that when the audio is played on your speakers that there may be some distortion of the audio coming from the speakers. It may sound as if the speakers are making a “rattling” sound. To alleviate this noise in the audio and improve the sound quality, we can add a 1 kilo-ohm resistor (brown–black–red) inline with the connection made at the RX pin of the DFPlayer Pro Mini MP3 Player, as shown in the image below the bullet points below:
- To reduce noise to the speakers we’ll take a 1kΩ resistor and place one of its terminals or leads to the RX pin of the DFPlayer Pro on the breadboard. Next, place the other terminal or lead of the resistor to a point on the breadboard where no other items are connected inline to that point. On my breadboard, I’ve placed a lead of the resistor in point a8, inline with the RX pin of the DFPlayer Pro on the breadboard, and I’ve placed the other lead end of the resistor at point a2, as shown in the image below.
- Connect the male end of the blue wire to point b2, inline with the resistor lead at point a2. Next connect the other male end of the same blue jumper wire to pin 2 of the Arduino.
- Connect the male end of the orange wire to pin 3 of the Arduino. Next connect the other male end of the same orange jumper wire to the TX pin of the DFPlayer Pro Mini MP3 Player. In my case, point a9 is inline with the TX pin of the DFPlayer Pro on the breadboard.
Video Build of the OwlBot: Part 2 – Making Sounds
Overview
This has been Part 2 of the build for the OwlBot project. In Part 1, we made connections for the PIR sensor and Arduino, and in Part 2 we’ve added the DFPlayer Pro Mini MP3 Player to our setup. We downloaded an MP3 file containing owl sounds for our OwlBot. We uploaded the MP3 file to the DFPlayer Pro, and we added to our code the functionality into the Arduino for our prototype modules to make owl sounds when motion is detected.
What’s Next
Now that we have implemented motion sensing and sound with the help of the HC-SR051 PIR sensor and DFPlayer Pro Mini MP3 Player modules, the next step in prototyping the OwlBot will be to prototype the OwlBot’s flashing red eyes. We’ll do this using a pair of bright red LEDs. We’ll add the LEDs to our prototype circuit, and then add to our code for the Arduino to be able to control the eyes to flash while the MP3 player plays owl sounds when motion is detected by the PIR sensor. All this will be in Part 3 of the OwlBot project!
Click here to go to “How to Make an OwlBot: The Bird Intimidator – Part 3: Flashing Red LED Eyes”, the next step of this project!
Thank you for participating in this project. We hope that you have enjoyed the build thus far. Let us know by giving a comment in the comments section below. Share the link to your friends and family. Let them know how much you’ve enjoyed the process of prototyping the OwlBot!