DFPlayer Mini Volume Control With Potentiometer

Hello, I am using a Arduino Mega 2560 and DFPlayer Mini to produce sound for my project, and was wandering, “Can I add a potentiometer to the Arduino to adjust the volume?” Of course, the answer is “Yes!” But, what I want to know is how to make the potentiometer adjust the volume of the music WHILE the music is playing, how do I do that? I tested the code below and the code works just fine (as you will see in the video), except, the code needs to be modified to where I can adjust the volume WHILE music is going on. So, how do I do that? The Arduino is powered via USB and the DFPlayer Mini is powered via a wall adapter. And, yes, their grounds are connected together via a breadboard.

My video link:

DFPlayer Mini Volume Control

Music link:

No Copyright Music

Software:

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

const byte potPin = A0;

int analogValue = 0;  //variable for holding the pot value
byte volumeLevel = 0; //variable for holding volume level

void setup() {

  Serial.begin(9610);

  mySerial.begin(9600);

  myMP3.begin(mySerial);

}

void loop() {
  analogValue = analogRead(potPin); // get value from pot

  volumeLevel = map(analogValue, 0, 1023, 0, 30);   //scale the pot value and volume level

  myMP3.volume(volumeLevel);

  Serial.println(volumeLevel);

  myMP3.play(8);

  delay(282000);

}

Schematic:

Try something like this:

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

const unsigned long trackDuration = 3 * 60 * 1000; // replace with the exact duration of the mp3 file --> 3 * 60 * 1000 = 180000ms = 3min
const byte potPin = A0;

unsigned long prevTime = millis();
byte volumeLevel = 0; //variable for holding volume level

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  myMP3.begin(mySerial);
  
  myMP3.play(8);
}

void loop()
{
  volumeLevel = map(analogRead(potPin), 0, 1023, 0, 30);   //scale the pot value and volume level
  myMP3.volume(volumeLevel);
  Serial.println(volumeLevel);

  if((millis() - prevTime) >= trackDuration)
  {
    prevTime += trackDuration;
    myMP3.play(8);
  }
}

Thanks! That was awesome! Great job!

UptownKitten:
Thanks! That was awesome! Great job!

:smiley:

No prob!

UptownKitten:
How does the code work?

The Arduino starts out by sending a command to the DFPlayer to “start” the track. While the track is playing, the Arduino constantly samples the voltage on the pot and sends the corresponding volume to the DFPlayer. The DFPlayer has the ability to change volume while a track is playing. Once enough time has passed and the track is assumed to be complete, it’s played again.

TBH, a better example would be to use the player.loop() function instead of player.play():

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

const byte potPin = A0;

byte volumeLevel = 0; //variable for holding volume level

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  myMP3.begin(mySerial);
 
  myMP3.loop(8);
}

void loop()
{
  volumeLevel = map(analogRead(potPin), 0, 1023, 0, 30);   //scale the pot value and volume level
  myMP3.volume(volumeLevel);
  Serial.println(volumeLevel);
}

Well, it seems like now it's working half the time, why is that? Like, the DFPlayer suddenly just cut out! How?

I even reset the Arduino and it isn't doing anything!

I turned off the power for a sec and turned everything back on and it still doesn’t work!

UptownKitten:
Well, it seems like now it's working half the time, why is that? Like, the DFPlayer suddenly just cut out! How?

Can you provide more specifics on what exactly is happening and under what circumstances? Also, are you using the code in my first or second post?

Also, you might need a slight delay between volume commands

Okay, so, the Arduino was running its program. It was working. I messed myself up by turning off the power supply and turning it back on AND I unplugged the power from the Arduino via USB. So, then, what happened was that the DFPlayer just stopped working, like, it just suddenly cut out. As if I accidently bumped into a wire. But, there is an exception. When I reset the Arduino it still didn't work. I thought, "Well, maybe there is just a loose connection." But, there wasn't. How did I know that? This is because I tested it with another program, a test program. And, the test program worked just fine. But, the other program that you gave me didn't work. The only thing I changed was "myMP3.play(8);" to "myMP3.loop(8);" and then, it just all of a sudden jacked everything up. I am using the code from the first post too, all I did was modify the code a teensy bit no pun intended. So, I added a delay like you said, still didn't work. I used a test program again, same result as mentioned previously, it worked. So, I don't know what the problem is. I am thinking it is a syntax error in my code. I copied and pasted the code again, still didn't work.

UptownKitten:
The only thing I changed was "myMP3.play(8);" to "myMP3.loop(8);"

For some reason this got confused with emojis. Let me try again: "The only thing that I changed was

myMP3.play(8);

to

myMP3.loop(8);

Okay, so I tested the program again, and the only thing that seems to work is when I change

myMP3.loop(8);

back to

myMP3.play(8)

I wonder why that is… Strange… I used the test program again and the .loop didn’t work, the only thing that worked was the .play. I guess that the song is too long to be looped. Any explanation?

Test program:

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

void setup()
{
  Serial.begin(9601);
  mySerial.begin(9600);

  myMP3.begin(mySerial);
  
  Serial.println("Setting volume to max");
  myMP3.volume(30);
  delay(20);
  
  Serial.println("Playing track 8 for 5 sec");
  myMP3.play(8);
  delay(5000);

  Serial.println("Sleeping for 5 sec");
  myMP3.sleep();
  delay(5000);

  Serial.println("Waking up");
  myMP3.wakeUp();

  Serial.println("Looping track 8");
  myMP3.loop(8);
}

void loop()
{
  //do nothing
}

Actual program:

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

const unsigned long trackDuration = 3 * 60 * 1000; // replace with the exact duration of the mp3 file --> 3 * 60 * 1000 = 180000ms = 3min
const byte potPin = A0;

unsigned long prevTime = millis();
byte volumeLevel = 0; //variable for holding volume level

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  myMP3.begin(mySerial);
 
  myMP3.play(8);
}

void loop()
{
  volumeLevel = map(analogRead(potPin), 0, 1023, 0, 30);   //scale the pot value and volume level
  myMP3.volume(volumeLevel);
  Serial.println(volumeLevel);

  if((millis() - prevTime) >= trackDuration)
  {
    prevTime += trackDuration;
    myMP3.play(8);
  }
}

Try using the official DFRobot lib instead of the _fast version…

I think I recall a loop() issue being brought up before with this. (and other functions not working)… not sure what version you got or if things are updated.

See if that changes your results for .loop().

I dont recall if I have ever changed the volume during the code before… only during set-up DFPlayer initialization

i think i try