Action: DFPlayer and speaker
The next two components are needed to let our plant make sounds.
DFPlayer
- The MP3 player module can convert MP3 files into electrical signals that can be directly outputted through a speaker.
- It can read MP3 files from a microSD card (Note: the files must be saved in a specific format for the MP3 player to recognize them, more on that later).
Technical Specifications
- Operating Voltage: 3.2 - 5 V
- Amplifier for speakers up to 3 W
- TF/MicroSD card slot, up to 32GB
Speaker
- The speaker is a transducer that converts an electrical signal into sound.
- When connected to the DFPlayer, it makes the sounds played through the DFPlayer audible.
Technical Specifications
- Electrical Resistance: 8 Ohms
- Power: 3 W (The power rating indicates how much electrical power the speaker can handle and convert into sound power.)
Prepare a sound file
To play a sound with the DFPlayer, we first need to copy a sound in the form of an MP3 file to a microSD card, which we will then insert into the DFPlayer. Insert the microSD card into your laptop. If your laptop does not have a built-in microSD card reader, use a USB adapter for microSD cards.
The MP3s will be played in the order they were copied to the card. The DFPlayer can manage up to 100 folders with up to 255 MP3s each. However, for today, we only need a single sound file.
Find a royalty-free MP3 from one of the following websites:
- https://soundbible.com/
- https://www.partnersinrhyme.com/pir/PIRsfx.shtml
- https://freesound.org/ (login wird benötigt)
Attention! The Soundbible page might show you different pop-up/commercials with download buttons. The correct download button looks like this (marked with purple circle):
Download your desired file and save it to the microSD card. Before you save the file to the card, try to play the sound on your Laptop to make sure it actually works.
If you want to specify the playback order yourself, you need to create a folder named “mp3” in the root directory of the SD card. Additionally, the file name must consist of four numbers, such as “0001.mp3,” and it should be placed in the mp3 folder.
Connect speaker and DFPlayer
The speaker only has two wires because it doesn’t process any signal. The membrane is vibrated by the current, producing sound waves that we can hear as tones:
- Negative terminal (black wire)
- Positive terminal (red wire)
To drive the speaker, we will use the DFPlayer. It has various pins, but for our purpose, the following pins are relevant:
- Negative terminal - GND pin
- Positive terminal - VCC pin
- Serial Input - RX pin
- Serial Output - TX pin
- Speaker Ground Connection - SPK2 pin
- Speaker Mass Connection - SPK1 pin
You have the motion sensor oriented correctly if you see the marking at the top and the slot for the microSD card is visible.
You connect the pins using several wires as follows:
- Make sure your microcontroller is disconnected from the USB power source before making changes to the circuit
- Connect a wire between the Ground pin of the DFPlayer and the G (Ground) slot on the microcontroller.
- Connect a wire between the VCC pin of the DFPlayer and the VV pin on the microcontroller.
- Connect a wire between the Serial Input (RX) pin of the DFPlayer and the D3 pin of the microcontroller.
- Connect a wire between the Serial Output (TX) pin of the DFPlayer and the D2 pin of the microcontroller.
- Connect the SPK2 pin of the DFPlayer to black wire (or white) of the speaker using a wire (you may secure the connection with some tape).
- Connect the SPK1 pin of the DFPlayer to the red wire (or white) of the speaker using an wire (you may secure the connection with some tape).
Programming
Note: If you still have any code in the editor from a previous exercise, feel free to delete it
Here is the code needed to play the MP3 file. We will be using a programming library that provides various functions for controlling the DFPlayer.
To use the DFPlayer, you need to install the DFRobotDFPlayerMini library. Go to the Arduino IDE menu Sketch > Include Library > Manage Libraries and search for “DFPlayer”. Install the library called “DFRobotDFPlayerMini”.
We also need another library called “SoftwareSerial”. Since we don’t have available serial ports on our microcontroller, we will use the Serial library to use the digital pin of the microcontroller for serial communication. This library is already installed, so you don’t need to install it separately.
To use the libraries in your program code, they need to be included in the code. This is typically done at the beginning of the file. The SoftwareSerial library enables serial communication with the microcontroller using digital pins.
#include "SoftwareSerial.h" // load the SoftwareSerial library
#include "DFRobotDFPlayerMini.h" // load the DFPlayer library
SoftwareSerial mySoftwareSerial(D2, D3);
DFRobotDFPlayerMini myDFPlayer;
Following is the program code:
void setup() {
mySoftwareSerial.begin(9600); // Configure SoftwareSerial
Serial.begin(115200); // Configure Serial Monitor
Serial.println(); // print an empty line
Serial.println("Initialize DFPlayer ... (May take 3~5 seconds)");
if (!myDFPlayer.begin(mySoftwareSerial)) { // If DFPlayer fails to start, execute the next 4 lines.
Serial.println("DFPlayer startet nicht:");
Serial.println("1. Überprüfe die Kabel!");
Serial.println("2. Lege eine SD-Karte ein!");
while (true); // Repeat
}
Serial.println("DFPlayer ist gestartet.");
myDFPlayer.volume(10); // Set the volume to 10 (0 to 30).
myDFPlayer.play(1); // Play the first MP3.
}
void loop() {
static unsigned long timer = millis(); // Milliseconds since the program started.
if (millis() - timer > 3000) { // If 3 seconds have passed, then execute the next lines.
timer = millis(); // Update the variable with the time that has elapsed now.
Serial.println("DFPlayer plays the next file.");
myDFPlayer.next(); // Play the next MP3 file.
}
}
After uploading the program to the microcontroller, you should hear a sound. 🔊
Also, observe the serial monitor for debugging information. Sometimes you may need to briefly remove and reinsert the microSD card for it to be recognized.
Wow! You’re doing great! ❤️ Take a short break, get yourself a drink, and stretch your legs.
You can use the setup for the DFPlayer and speaker in the next step as well.