Introduction: MP3 Player With Arduino

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects.

In this assembly, we used an mp3 player with Arduino Uno, a mini PAM8403 sound amplifier with volume adjustment, an mp3 module DFPlayer Mini, and a pair of 3-Watt speakers.

This scheme serves not only for the music player, but also to allow voice functions for a parking barrier device or a robot, among others. This assembly will allow for the placement of a recorded voice in the apparatuses. This voice will be natural and not synthesized. For most people, these are preferable features compared to the standard alternatives.

For this assembly, we are specifically using the Arduino Uno, but you can also connect to ESP8266 or ESP32. Obviously, an adjustment will be required on the pins.

Step 1: Datasheet

Step 2: DFPlayer Module

The DFPlayer module uses the serial communication RX TX, VCC, GND. It has output to two speakers and audio input.

Step 3: Mini Amplifier PAM8403

Step 4: Assembly

Step 5: Libraries

Add the following "DFRobotDFPlayerMini" library for communication with the mp3 module.

Simply access "Sketch >> Include Libraries >> Manage Libraries ..."

Step 6: Source Code

We'll start by defining the libraries and constants we'll use with our code.

Start by creating one object, the serial software, and another, which is myDFPlayer.

The buf variable, which is of type String, will serve to store the data coming from the Arduino Serial, which will be the commands for the MP3 module. The "pause" variable will be used to indicate if the music is playing or paused (pause = true, and indicates that it is paused, otherwise it is playing).

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h" //Inicia a serial por software nos pinos 10 e 11 SoftwareSerial mySoftwareSerial(10, 11); // RX, TX //Objeto responsável pela comunicação com o módulo MP3 (DFPlayer Mini) DFRobotDFPlayerMini myDFPlayer; //variável responsável por armazenar os comandos enviados para controlar o player String buf; //variável responsável por armazenar o estado do player (0: tocando ; 1: pausado) boolean pausa = false; //variável responsável por armazenar o estado da equalização //varia de 0 a 5 int equalizacao = 0; // (0 = Normal, 1 = Pop, 2 = Rock, 3 = Jazz, 4 = Classic, 5 = Bass)" //variável responsável por armazenar o total de músicas presentes no SD card. int maxSongs = 0;

Setup

In this step, we put options of prints to give you indications that it is mounting the circuit, with evaluations of false conditions, for example.

void setup()
{ //Comunicacao serial com o modulo mySoftwareSerial.begin(9600); //Inicializa a serial do Arduino Serial.begin(115200); //Verifica se o modulo esta respondendo e se o //cartao SD foi encontrado Serial.println(); Serial.println("DFRobot DFPlayer Mini"); Serial.println("Inicializando modulo DFPlayer... (3~5 segundos)"); if (!myDFPlayer.begin(mySoftwareSerial)) { Serial.println("Nao inicializado:"); Serial.println("1.Cheque as conexoes do DFPlayer Mini"); Serial.println("2.Insira um cartao SD"); while (true); } Serial.println(); Serial.println("Modulo DFPlayer Mini inicializado!"); //Definicoes iniciais myDFPlayer.setTimeOut(500); //Timeout serial 500ms myDFPlayer.volume(10); //Volume 10 vai de 0 a 30 myDFPlayer.EQ(0); //Equalizacao normal //recupera o numero de Músicas encontradas no SD. maxSongs = myDFPlayer.readFileCounts(DFPLAYER_DEVICE_SD); Serial.println(); Serial.print("Numero de arquivos no cartao SD: "); Serial.println(maxSongs); //Mostra o menu de comandos menu_opcoes();

Options Menu

You control this entire assembly through the serial monitor. So each time, the scheme will be printing the Options Menu you have, with commands, directions.

void menu_opcoes()
{ Serial.println(); Serial.println("Comandos:"); Serial.print(" [1-"); Serial.print(maxSongs); Serial.println("] Para selecionar o arquivo MP3"); Serial.println(" [s] parar reproducao"); Serial.println(" [p] pausa/continua a musica"); Serial.println(" [e] seleciona equalizacao"); Serial.println(" [+ or -] aumenta ou diminui o volume"); Serial.println(); }

Loop

void loop()
{ //Aguarda a entrada de dados pela serial while (Serial.available() > 0) { //recupera os dados de entrada buf = Serial.readStringUntil('\n'); //Reproducao (índice da música) if ((buf.toInt() >= 1) && (buf.toInt() <= maxSongs)) { Serial.print("Reproduzindo musica: "); Serial.println(buf.toInt()); myDFPlayer.play(buf.toInt()); // dá play na música menu_opcoes(); } //Pausa/Continua a musica if (buf == "p") { if (pausa) { Serial.println("Continua musica..."); myDFPlayer.start(); } else { Serial.println("Musica pausada..."); myDFPlayer.pause(); } pausa = !pausa; menu_opcoes(); } //Parada if (buf == "s") { myDFPlayer.stop(); Serial.println("Musica parada!"); menu_opcoes(); } //Seleciona equalizacao if (buf == "e") { equalizacao++; if (equalizacao == 6) { equalizacao = 0; } myDFPlayer.EQ(equalizacao); Serial.print("Equalizacao: "); Serial.print(equalizacao); Serial.println(" (0 = Normal, 1 = Pop, 2 = Rock, 3 = Jazz, 4 = Classic, 5 = Bass)"); menu_opcoes(); } //Aumenta volume if (buf == "+") { myDFPlayer.volumeUp(); Serial.print("Volume atual:"); Serial.println(myDFPlayer.readVolume()); menu_opcoes(); } //Diminui volume if (buf == "-") { myDFPlayer.volumeDown(); Serial.print("Volume atual:"); Serial.println(myDFPlayer.readVolume()); menu_opcoes(); } } //while } //loop