Introduction: Arduino Mp3 Player/Timer
It's a standard Arduino Mp3 Player with a timer function, because why not? Here is a video of me demoing the finished product: https://youtu.be/EpJ-ysjU_DA
Step 1: Materials
- An Arduino Uno - A DF Player Mp3 ModuleFull Preview
- AN LCD Keypad Shield - Lots of wires (I also used a few male to female headers) - A breadboard or two (depending on how you approach the wiring - A Potentiometer - 10k resistor (to enhance audio quality) - Speakers that can be attached to the module via breadboard (I used some old laptop speakers, that's why I used a second resistor) - A micro SD card with mp3 files on it
Step 2: Wiring
Digital Pins 4 - 10 connect to the LCD Shield along with all the GND, Analog, Power Pins, etc on the other end of the shield.
Digital Pins 3 (in the video it's 10 but that pin is being used by the LCD so use 3 instead) and 11 go to the Mp3 module.
I used the Analog 5, Power and GND pins on the LCD Shield for the Potentiometer.
Watch the following video to see how to hook up the Mp3 module:
Step 3: Code
// mp3 code: http://educ8s.tv/arduino-mp3-player/
// mp3 module commands: https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#1.29_Serial_Mode
// lcd code: http://www.hobbytronics.co.uk/arduino-lcd-keypad-shield
// potentiometer code: https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v32/experiment-2-reading-a-potentiometer
#include #include "SoftwareSerial.h" SoftwareSerial mySerial(11, 3); // pins that arduino is using to talk to mp3 module # define Start_Byte 0x7E # define Version_Byte 0xFF # define Command_Length 0x06 # define End_Byte 0xEF # define Acknowledge 0x00 // Error Checking
# define ACTIVATED LOW
boolean isPlaying = false; // used for pasue and resume int x = 0; // lcd buttons int volume = 0; // used to adjust volume int sensorValue; // stores potentiometer reading int num = 0; int paused = 0; // used to stop of resume visuals int count = 0; int count2 = 0; // timer char high = '-'; // defualt visuals char low = '_'; // defualt visuals int visual = 0; // visual setting (dots = 1 or dashes = 0) int minutes = -1; int seconds = 0; int timer = 0; // timer is not activated int setting = 0;
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int sensorPin = 1; // The potentiometer is connected to // analog pin 1
void setup () {
// set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("mp3 player");
mySerial.begin (9600); // begins serial communication with module delay(1000); playFirst(); // plays the first song on the sd card isPlaying = true;
}
void loop () {
sensorValue = analogRead(sensorPin); // takes in sensor value // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); x = analogRead(0); // lcd buttons visualizer(); // lauches visualizer
if (timer == 0) { if (setting = 0) { minutes = -1; seconds = 0; count2 = 0; } }
else if (timer == 1) { if (minutes > -1) { if (count2 == 2) { seconds -= 1; count2 = 0; } count2 += 1;
lcd.clear(); lcd.print("Time: "); lcd.print(minutes); lcd.print("min"); lcd.print(seconds); lcd.print("sec");
if (minutes == 0) { if (seconds == 0) { timer = 0; execute_CMD(0x0E, 0, 1); // pauses playback isPlaying = false; paused = 1; lcd.clear(); lcd.print("Time's up"); delay(1000); lcd.clear(); lcd.print("Hit resume, to.."); delay(1200); lcd.clear(); lcd.print("resume playback!");
} }
if (seconds == 0) { minutes -= 1; seconds = 59; } }
}
switch (sensorValue / 100) { // potientiometer volume control case (1): volume = 3; setVolume(); break; case (2): volume = 6; setVolume(); break; case (3): volume = 9; setVolume(); break; case (4): volume = 12; setVolume(); break; case (5): volume = 15; setVolume(); break; case (6): volume = 18; setVolume(); break; case (7): volume = 21; setVolume(); break; case (8): volume = 24; setVolume(); break; case (9): volume = 27; setVolume(); break; case (10): volume = 30; setVolume(); break; }
if (x < 60) { // right lcd button
if (isPlaying) // plays next song { playNext(); }
if ( visual == 0) { // toggles visual settings high = ':'; // changes from dashes to dots low = '.'; visual = 1; }
else { high = '-'; // changes back to dashes low = '_'; visual = 0; }
if (isPlaying == true) { lcd.clear(); lcd.print ("playing next"); }
else if (isPlaying == false) { lcd.clear(); lcd.print("Hit Resume"); } }
else if (x < 200) { // up lcd button
lcd.clear(); lcd.print("Time: "); lcd.print(minutes + 1); minutes += 1; setting = 1;
}
else if (x < 400) { //down lcd button timer = 1; }
else if (x < 600) { //left lcd button
if (isPlaying) // pause/resume { pause(); isPlaying = false; // changes from ture to false if paused lcd.clear(); lcd.print ("paused"); if (seconds > 0){ seconds -= 1; delay(1000); }
else if (minutes > 0) { minutes -= 1; seconds = 59; delay(1000); } } else { isPlaying = true; // returns to true if resumed play(); lcd.clear(); lcd.print ("playing"); if (seconds > 0){ seconds -= 1; delay(1000); }
else if (minutes > 0) { minutes -= 1; seconds = 59; delay(1000); } } }
else if (x < 800) { //select lcd button
if (isPlaying) // plays previous song { playPrevious(); }
if ( visual == 0) { // toggles visual settings high = ':'; // changes from dashes to dots low = '.'; visual = 1; }
else { high = '-'; // changes back to dashes low = '_'; visual = 0; }
if (isPlaying == true) { lcd.clear(); lcd.print ("playing previous"); if (seconds > 0){ seconds -= 1; delay(1000); }
else if (minutes > 0) { minutes -= 1; seconds = 59; delay(1000); } }
else if (isPlaying == false) { lcd.clear(); lcd.print("Hit Resume"); if (seconds > 0){ delay(1000); seconds -= 1; }
else if (minutes > 0) { delay(1000); minutes -= 1; seconds = 59; } } }
delay(200); }
void playFirst() // plays fist song on sd card { execute_CMD(0x3F, 0, 0); // sends initialization paratmeters to module delay(250); setVolume(); // sets volume delay(250); execute_CMD(0x0f, 0, 1); // selects folder execute_CMD(0x08, 0, 1); // begins playback (repeat playlist) }
void pause() { paused = 1; // stops visuals execute_CMD(0x0E, 0, 0); // pauses music delay(250); }
void play() { paused = 0; // resume visuals execute_CMD(0x0D, 0, 1); // resumes music delay(250); }
void playNext() { execute_CMD(0x01, 0, 1); // plays next song delay(250); }
void playPrevious() { execute_CMD(0x02, 0, 1); // plays previous song delay(250); }
void setVolume() { execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30) delay(250); }
void visualizer() { // visuals for (int i = 0; i < 17; i++) { // displays 16 characters char symbol; int level = random(1, 6); // picks a number between 1 and 6
if (paused == 1) { // stops visuals if paused symbol = low; }
else if (level < 4) { // prints a high or low based on the number chosen symbol = low; }
else { symbol = high; }
lcd.print(symbol); // prints to screen delay(i / 3 + 5); // delays based on value of i } }
void execute_CMD(byte CMD, byte Par1, byte Par2) // Excecute the command and parameters { // Calculate the checksum (2 bytes) word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2); // Build the command line byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte }; //Send the command line to the module for (byte k = 0; k < 10; k++) { mySerial.write( Command_line[k]); } }