Go Down

Topic: RC522 & DFPlayer on Elegoo Uno R3 (problem fixed) (Read 215 times) previous topic - next topic

Dagnir

Greetings.
I'm trying to get my Elegoo Uno R3 to be able to react to different RFID-Chips by playing mp3s.
It works perfectly fine seperate from one another, I can tell the RFIDs apart and play the mp3s that I want to. But when trying to combine those two, I've run into problems: Using the DFPlayer works just fine until I call
SPI.begin();
after wich none of the commands called for the Player do anything. But without that command the RC522 doesn't work.

You seem to need a separate SS-Pin (why is it called SDA on the RC522 and Transciever on the DFPlayer?) for each device, but on my board there's only one SS (at least if the Elegoo is like the Arduino Uno).
Does anyone here know a way to get two slaves working without buying a new board?

Pins:
Elegoo UnoRC522DFPlayer
9RST---
10SDATx
11MOSIRx
12MISO---
13SCK---
3.3V3.3V---
5V---VCC
GNDGNDGND(separate)

Tx and Rx are connected to the breadbord through a 1kOhm resistor (unlike seen here, but I read it'd reduce noise).
Used these connections to the RC522.

Copied together codes from this and this source:
Code: [Select]

#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN         9
#define SS_PIN          10

MFRC522 mfrc522(SS_PIN, RST_PIN);

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

SoftwareSerial mySoftwareSerial(10,11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value); //[i]idk what this does?!?[/i]

void setup()
{
mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));
  
  
  myDFPlayer.volume(10);  //Set volume value. From 0 to 30
    myDFPlayer.play(1);  //Play the first mp3 [i]does work[/i]
    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    myDFPlayer.play(1);      //[i]added to test, should play the first mp3, doesn't work[/i]
    mfrc522.PCD_Init();         // Init MFRC522 card  
}

Power_Broker

1.) Use different pins for TX and RX - use any digital pins NOT used by SPI
2.) The stock DFPlayerMini.h library is trash - use this highly efficient library instead
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Dagnir

Okay, I switched Tx to Rx/Digital Pin 0 and Rx to Tx/D1, changed it in the declaration of mySoftwareSerial and used your library. But I don't have the possibility to use a second hardware serial - in the example you said one could use a SoftwareSerial instead, but I get an Error:
could not convert 'myDFPlayer.DFPlayerMini_Fast::begin(mySoftwareSerial.SoftwareSerial::<anonymous>)' from 'void' to 'bool'
What's the intended way to use a SoftwareSerial in your library?

Power_Broker

#3
Apr 23, 2019, 01:05 am Last Edit: Apr 23, 2019, 01:05 am by Power_Broker
The DFPlayerMini_Fast class accepts a stream class as an argument when calling .begin(). Here is the relevant code from my library:

Code: [Select]

class DFPlayerMini_Fast
 {
 public:
   Stream* _serial;
   void begin(Stream& stream);



Therefore, you just pass the name of your instantiated softwareSerial class to the .begin() method. Can you post your code?
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Dagnir

#4
Apr 23, 2019, 02:06 pm Last Edit: Apr 23, 2019, 09:56 pm by Dagnir
Code: [Select]
#include "Arduino.h"
#include "SoftwareSerial.h"
#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast myDFPlayer;

void setup()
{
  Serial.begin(115200);
  SoftwareSerial mySoftwareSerial(0,1); // RX, TX
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
 
    if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use SoftwareSerial to communicate with mp3.
      Serial.println(F("Unable to begin:"));
      Serial.println(F("1.Please recheck the connection!"));
      Serial.println(F("2.Please insert the SD card!"));
    }
    myDFPlayer.volume(8);  //Set volume value. From 0 to 30
    SPI.begin();                // Init SPI bus
    myDFPlayer.play(1);
    mfrc522.PCD_Init();
}

I also tried passing the adress (&) of mySoftwareSerial, but it didnt work with the passed pointer either.
[EDIT:] I tried using a SoftwareSerial the same way in your example.ino and it compiled without an error, yet didn't really work apart from the printing stuff... I'm confused.
SoftwareSerial mySoftwareSerial(5,6); //instead of Serial1, changed wires accordingly
myMP3.begin(mySoftwareSerial);
are the only lines of code I changed in example.ino
 

Power_Broker

#5
Apr 24, 2019, 12:18 am Last Edit: Apr 24, 2019, 12:18 am by Power_Broker
1.) This shouldn't matter a whole lot, but you should have it as
Code: [Select]

#include "DFPlayerMini_Fast.h"
since the library isn't "pre-compiled"

2.) Simplify the project temporarily by only focusing on getting the DFPlayer working. Delete/comment out the code for the SPI device and we'll revisit it once the MP3 player is working.

3.) You never called mySoftwareSerial.begin(9600) - nothing will work on this port without such a call

4.) You don't need to include Arduino.h
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Dagnir

Okay, thanks, 3) and looking at your .h again fixed my problems!
Found out your begin() method returns nothing while that of the standard librarys begin() returns a bool (pointing out if it was successful) and
if (!myDFPlayer.begin(mySoftwareSerial))
is dependant on that return.

Thanks for all those tips and help and the new library!

Power_Broker

Glad to help!


Found out your begin() method returns nothing while that of the standard librarys begin() returns a bool
That's a really good point - Imma change my library tonight to have it return a bool because of this 8)
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up