Using DFPlayer - Mini MP3 Player. Issues

Hello,

I've just added the DF player to my project. When I run the sample code it works, when I add it to my project it fails. What have I done wrong?

^^ this is the mini DF player.

The code is designed to move a servo to pat someone on the back every time they pat the sensor. (and play a little MP3 clip that tells them they have done a good job.)

Here is my code which sends up the error message "Connecting to DFPlayer Mini failed!" (which is from the setup)

Thanks!

#include <Servo.h>  // include servo libary
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

// Useing 3 states to have One pat = pat of servo. Good But slow.

static const uint8_t PIN_MP3_TX = 8;   // Connects to module's RX (DFPlayer Mini)
static const uint8_t PIN_MP3_RX = 10;  // Connects to module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Create the DFRobot Player mini object
DFRobotDFPlayerMini player;

const byte PinSensor = A0;

Servo patterServo;
unsigned servoDown = 270;  // number to write to servo for it to be down.
unsigned servoUp = 30;     // number to write to servo for it to be up.


int PatThresh = 90;  // when sensor is triggered
int PatHyst = 20;    // a sort of debouce
bool held;

enum State {
  IDLE,
  PATTED,
  RESET
};

State state = IDLE;
unsigned long lastChange = 0;
unsigned long lastChangeMusic = 0;
const unsigned long interval = 200;
const unsigned long track_length = 3000;




void setup() {

  Serial.begin(9600);
  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);
 // delay(1000); 

  // Start communication with DFPlayer Mini
  if (player.begin(softwareSerial)) {
     
    Serial.println("OK");

    // Set volume to maximum (0 to 30).
    player.volume(30);
  } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }

    patterServo.attach(9);
  held = false;
}

void loop() {

  unsigned long now = millis();
  int sensorVal = analogRead(PinSensor);
  unsigned long now2 = millis();

  switch (state) {
    case IDLE:
      patterServo.write(servoUp);
      Serial.println("  IDLE");
      Serial.println(sensorVal);
      if (!held && ((PatThresh + PatHyst) < sensorVal)) {  // Move to patted state if pat is sensed (but not if someone is just holding sensor.)
        lastChange = now;
        lastChangeMusic = now2;
        state = PATTED;
      } else if (held && (PatThresh - PatHyst) > sensorVal) {  // this switches it to being un held if someone was just holding the sensor
        held = false;
        Serial.println("   release");
        Serial.println(sensorVal);
        state = IDLE;
      }
      break;

    case PATTED:
      patterServo.write(servoDown);  // in Patted state, move servo down to pat
      Serial.println("  PATTED");
      Serial.println(sensorVal);
      held = true;

      if (now2 - lastChangeMusic > track_length) {  //track_length is the duration of the audio(1)
        lastChangeMusic = now2;

        //(2) is the 2rd file in the sd card, the order = the order you copied the file to it
        player.play(1);
      }


      if (now - lastChange >= interval) {  // wait for servo to get there and move to state reset
        state = RESET;
        lastChange = now;
      }

      break;
    case RESET:
      patterServo.write(servoUp);  // Move servo back to starting position
      Serial.println("  RESET");
      Serial.println(sensorVal);
      if (now - lastChange >= interval) {  // wait for servo to get there
        state = IDLE;
        lastChange = now;
      }
      break;
  }
}

From the wiki:

Add a 1kΩ resistor between Arduino TX and DFPlayer RX for signal conditioning.

DFPlayer Mini operates at ~3.3V while many MCUs use 5V; the resistor helps with level compatibility and reduces noise.

I have done that.
Also - it works when I only run the sample code on its own? So I assume that shows that the wiring it correct.

I followed this wiring (actually has two resistors?) And I changed it a bit as I only have one speaker.

Which Arduino are you using, Uno, Mega, ESP, etc.?

==>
UNO, MEGA?

Also the speaker output is not stereo. You should only connect one speaker between the speaker terminals.

Is this Golam person working with you?

Why do I not see green flag/notification when you print Golam or GolamMostafa? It only comes when you print @GolamMostafa. So in future, please type @GolamMostafa.

Hello,
Its an UNO.
I am only using one speaker. - so not exactly that wire diagram. But I did use two resistors as they did as I assumed that was a good idea.

Also it works perfectly when I use the sample code so I assume its not a wiring issue?

I can't see anything in your code that would cause it to fail.
Servo does have some effect on pins 9 and 10 so you might want to try different pins for RX and TX. Other than that i can't see anything wrong that would cause player.begin(softwareSerial) to fail

Also try disconnecting the servo and see if it works.

I do not believe this is a valid servo "degree." Valid range is 0 to 180. You can send different values, but the result is unknown.

Is this another mouse-whacker?

That wiring is incorrect, which I think was acknowledged by the author later. Wire each speaker between SPK1 and SPK2. (Or if you do want to use two speakers then wire them in series.)

EDIT: Also, echoing others, what Arduino you are using? And please post a schematic showing all connections including power supply details.
Meanwhile, a module vol of 30 is in danger of crashing your circuit due to voltage dropping on audio peaks. I rarely use more than 20.

EDIT: What is your sensor? And what is the sketch action you expect?

@Terrypin @GolamMostafa @jim-p @xfpd

So the arduino is an arduino Uno Rev 3.

I'm working on a project that as you pat a plastic doll, the blanket pats you back on the back.
The sensor I made myself out of conductive fabric and piezoresistive fabric layered up. It is a force sensor. I have not had an issue with the values I've given the servo!

Here is the schematic!

It looks like you have RX/TX swapped.
In your code D8 is TX and you have it connected to DFplayer TX

Exactly - it is correct - in code d8 is tx and df tx is connected to d8? So to me it looks correct?

RX/TX = 8/10 agrees with schematic of post #12.

TX to RX
RX to TX
Like the comments in your code say

Do you see?

Good catch; however, is it going to help in making a wrong connection?

The code is correct but the OP's schematic labels for the module RX /TX are reversed. A high res photo would show the correct wiring.

Oh! I didn’t realise tx was to rx. I must have read it a hundred times and just had a dyslexic moment . I’ll double check how it’s actually wired in real life. Although weird that it runs the sample code!

Maybe in the sample code you have the pins swapped the correct way.