Using two dfplayer modules

I'm using arduino nano board, and I'm trying to play two different music simultaneously. One dfplayer has to be controlled by a touch sensor, and another dfplayer will be controlled by the distance sensor. But I don't know how to connect 2 dfplayers with the nano board. Now I connected one dfplayer.

//Very much inspired by https://www.dfrobot.com/blog-1462.html by DFRobot Feb 26 2020
//Additions made by Just Baselmans https://www.youtube.com/justbaselmansYT Jan 23 2023

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

// Initialize software serial on pins 10 and 11
SoftwareSerial mySoftwareSerial1(10, 11);
SoftwareSerial mySoftwareSerial2(7, 8);
DFRobotDFPlayerMini myDFPlayer;
String line;
char command;
int dis1 = A1;
int touch = A2;
int track = 1;

void setup() {
  // Serial communication with the module
  mySoftwareSerial1.begin(9600);
  mySoftwareSerial2.begin(9600);
  // Initialize Arduino serial
  Serial.begin(115200);
  analogReference(DEFAULT);
  pinMode (dis1, INPUT);
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini"));
  Serial.println(F("Initializing DFPlayer module ... Wait!"));

  if (!myDFPlayer.begin(mySoftwareSerial1)) {
    Serial.println(F("Not initialized:"));
    Serial.println(F("1. Check the DFPlayer Mini connections"));
    Serial.println(F("2. Insert an SD card"));
    while (true)
      ;
  }
  Serial.println();
  Serial.println(F("DFPlayer Mini module initialized!"));
  //myDFPlayer.setTimeOut(500);  
  myDFPlayer.volume(15);       
  myDFPlayer.EQ(0);     

}

void loop() {
  command = Serial.peek();
  line = Serial.readStringUntil('\n');
  int raw1=analogRead(dis1);
 int touchval = analogRead(touch);
  //Serial.println(raw1);
  //Serial.println(touchval);
  delay(100);
  int range = map(raw1, 220, 700, 0, 7);
  switch (range) {
    case 0:
      myDFPlayer.play(1);
      Serial.println("Range 8");
      break;
    case 1:
      myDFPlayer.play(2);
      Serial.println("Range 7");
      break;
    case 2:
      myDFPlayer.play(3);
      Serial.println("Range 6");
      break;
    case 3:
      myDFPlayer.play(4);
      Serial.println("Range 5");
      break;
    case 4:
      myDFPlayer.play(5);
      Serial.println("Range 4");
      break;
    case 5:
      myDFPlayer.play(6);
      Serial.println("Range 3");
      break;
    case 6:
      myDFPlayer.play(7);
      Serial.println("Range 2");
      break;
    case 7:
      myDFPlayer.play(8);
      Serial.println("Range 1");
      break;
    }
    
}

In the Arduino IDE, on the menu bar click on Edit then Copy for Forum, then post your code here

Much bettor
Thank you

Sorry, but, 2 instances of SoftwareSerial is not a particularly good idea.

Then, how can I play two dfplayers?
My project deadline is 2 days left and I only have a nano, fio, uno board

Does your current code work correctly for just the one player?

yes it works

Well, just like SoftwareSerial, you can create two players:
DFRobotDFPlayerMini myDFPlayer1;
DFRobotDFPlayerMini myDFPlayer2;

Check to see if they are working and connected:
!myDFPlayer1.begin(mySoftwareSerial1, false, true)
!myDFPlayer2.begin(mySoftwareSerial2, false, true)

Play a track:
myDFPlayer1.play(1);
delay(10);
myDFPlayer2.play(1);

The rest is up to you.

D'accord - nous verrons.

Connect them in I/O Mode (VCC, GND, SPK_1, SPK_2). When you want DFPlayerMini to play NEXT file, "short-press" grounding IO_2, for PREV file, "short-press" grounding IO_1. "Long-press" will increase/decrease volume.

Thank you sooo much this works well

Glad it worked
Have a nice day!

Toujours le sceptique

I'm curious. Are they playing simultaneously (sounds from both speakers playing over each other), or successively?

Could you share your code please?

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