Mp3 mini player connection failure to Arduino nano

#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
SoftwareSerial MP3(8, 9);
DFRobotDFPlayerMini player;
const int buttonPin1 = A2;
const int buttonPin2 = A1;
const int buttonPin3 = A0;
const int selPin1 = A3;
const int selPin2 = A4;
const int selPin3 = A5;
const int potpin = A7;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int selState1 = 0;
int selState2 = 0;
int selState3 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int vol = 0;
void setup()
{
pinMode(potpin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(selPin1, INPUT);
pinMode(selPin2, INPUT);
pinMode(selPin3, INPUT);
Serial.begin(9600);
MP3.begin(9600);
if (player.begin(MP3))
{
Serial.println("OK");
player.volume(40);
}
else
{
Serial.println("Connecting to DFPlayer Mini failed!");
}
}

void volum()
{
vol = analogRead(potpin);
vol = map(vol, 0, 1023, 0, 50);
player.volume(vol);
}
void loop()
{
selState1 = digitalRead(selPin1);
selState2 = digitalRead(selPin2);
selState3 = digitalRead(selPin3);
delay(10);
if (selState1 == LOW && selState2 == HIGH && selState3 == HIGH)
{
sel1loop();
}
else if (selState1 == HIGH && selState2 == LOW && selState3 == HIGH)
{
sel2loop();
}
else if (selState1 == HIGH && selState2 == HIGH && selState3 == LOW)
{
sel3loop();
}
}

void sel1loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
delay(100);
if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count1 == 0)
{
count1 = 1;
volum();
player.play(1);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW && count1 == 0)
{
count1 = 1;
volum();
player.play(2);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH && count1 == 0)
{
count1 = 1;
volum();
player.play(3);
delay(100);
}
else if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count1 == 1)
{
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW && count1 == 1)
{
count1 = 0;
player.stop();
delay(100);
}
}

void sel2loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
delay(100);
if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count2 == 0)
{
count2 = 1;
volum();
player.play(4);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW && count2 == 0)
{
count2 = 1;
volum();
player.play(5);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH && count2 == 0)
{
count2 = 1;
volum();
player.play(6);
delay(100);
}
else if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count2 == 1)
{
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW && count2 == 1)
{
count2 = 0;
player.stop();
delay(100);
}
}

void sel3loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
delay(100);
if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count3 == 0)
{
count3 = 1;
volum();
player.play(7);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW && count3 == 0)
{
count3 = 1;
volum();
player.play(8);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH && count3 == 0)
{
count3 = 1;
volum();
player.play(9);
delay(100);
}
else if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count3 == 1)
{
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW && count3 == 1)
{
count3 = 0;
player.stop();
delay(100);
}
}
Hardware part is properly soldered but the mp3 player fails to connect with the nano every time, can anyone help. I have tried changing the RX and TX pin also. Is there a test code to check the mp3 player ? Also I shorted the IO_1 and IO_2 pins of the mp3 player and the sound got played!

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post a schematic of your project showing how its components are connected. A photo of a hand drawn circuit is good enough

1 Like

Hello @Terrypin thank you for your help, I used your code for reference and built on it, currently I can play songs via the code on the arduino nano.
Can you please also guide me on the approach or method to use to make the toggle switch play the song on the mp3 player as per the following way,

  1. There are a total of 9 tracks, 3 songs saved in each folder, for which I using the rotary knob which selects the folder
  2. Once folder is selected each track gets assigned to one toggle switch
    (like if folder 1 is selected track 1 = toggle 1, track 2 = toggle 2 and track 3 = toggle 3, so on it follows for folder 2 and 3).
  3. So if folder 2 is selected and toggle 2 is turned on it will play track nos 5, and if folder 3 and toggle 2 then track 8 should play.

Thats the basic level 1 I need to add to my circuit

  1. The level 2 that I want to add is
    whatever the folder selected,
    if toggle 1 is turned on it should play the corresponding sound, but if during this play toggle 3 is turned on sound of toggle 1 should stop and sound of toggle 3 should come in action, like priority.

  2. Vice versa case applies to all three toggle switch whichever is turned ON last that sound takes priority and the last toggle sound should stop.

Please follow the requests in post #2.

hello @Terrypin using your code I am getting a secure connection with the mp3 player and the serial communication is going quite well.
Can you pls suggest how to set code in which nano checks the state of the toggle switches everynow and then such that,
If toggle1 is ON and track01 is playing, if within 10 seconds toggle2 is turned ON track01 should stop and track02 should play the sound. Next if toggle3 is turned ON within 10 seconds track02 should stop and track03 should start playing This condition applies to all three toggle in vice versa sequence.

All these conditions should happen without the need of fliping back the previous toggle switch to OFF state.

And post #2?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.