Issues with Combining RFID and MP3 Modules on Arduino

Hi everyone,

I'm currently working on a project that involves combining an RFID reader and an MP3 module on an Arduino. Both modules work perfectly fine individually, but I encounter issues when trying to combine them. Here are the details of my setup:

Hardware Setup:

  • RFID Reader: Connected to pins D4 (TX) and D5 (RX).
  • MP3 Module: Connected to pins D8 (RX) and D9 (TX).
  • Arduino Model: [Specify your Arduino model, e.g., Arduino Uno].

Code Overview:

  1. RFID Setup: The RFID reader is initialized with SoftwareSerial on pins D4 and D5.
  2. MP3 Setup: The MP3 module is initialized with SoftwareSerial on pins D8 and D9.
  3. LEDs: Two LEDs are connected to pins A0 and A1 for visual indication.

Here is the combined code:

cpp

#include <DFRobot_DF1201S.h>
#include <SoftwareSerial.h>

// RFID setup
SoftwareSerial rfid(4, 5); // RX, TX
const int ledPin1 = A0; // Pin A0 for the first LED
const int ledPin2 = A1; // Pin A1 for the second LED

// MP3 setup
SoftwareSerial DF1201SSerial(8, 9);  // RX  TX
DFRobot_DF1201S DF1201S;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  Serial.println("Initializing RFID reader...");
  rfid.begin(9600);
  Serial.println("RFID reader initialized.");
  
  Serial.println("Initializing MP3 player...");
  DF1201SSerial.begin(115200);
  
  // Initialize LED pins
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  // Initialize MP3 player
  while (!DF1201S.begin(DF1201SSerial)) {
    Serial.println("Init failed, please check the wire connection!");
    delay(1000);
  }

  Serial.println("MP3 player initialized successfully");

  // Set initial volume
  DF1201S.setVol(15);
  Serial.print("VOL: ");
  Serial.println(DF1201S.getVol());

  // Set to music mode
  DF1201S.switchFunction(DF1201S.MUSIC);
  delay(2000); // Wait for the beep to finish

  // Debug message
  Serial.println("Ready to read RFID tags and play MP3");
}

void loop() {
  readRFID();
  delay(1000); // Add a delay to reduce the frequency of checking
}

void readRFID() {
  if (rfid.available()) {
    Serial.println("RFID data available"); // Debug message
    String rfidData = "";
    while (rfid.available()) {
      char c = rfid.read();
      Serial.print(c); // Debug message to print each character read
      rfidData += c;
      delay(5); // Small delay to ensure all data is read
    }
    Serial.println(); // New line after reading the RFID data
    if (rfidData.length() > 0) {
      Serial.print("RFID Tag Data: ");
      Serial.println(rfidData);
      // Play specific track
      playTrack("001001.mp3");
      // Activate LEDs during MP3 playback
      activateLEDs();
    } else {
      Serial.println("No RFID data read");
    }
  } else {
    Serial.println("No RFID data available"); // Debug message
  }
}

void playTrack(const char* filePath) {
  DF1201S.playSpecFile(filePath);
  Serial.print("Playing ");
  Serial.println(filePath);
}

void activateLEDs() {
  digitalWrite(ledPin1, HIGH); // Turn the first LED on
  digitalWrite(ledPin2, HIGH); // Turn the second LED on
  Serial.println("LEDs are on");

  while (DF1201S.isPlaying()) {
    delay(500); // Check every 500ms
  }

  digitalWrite(ledPin1, LOW); // Turn the first LED off
  digitalWrite(ledPin2, LOW); // Turn the second LED off
  Serial.println("LEDs are off");
}

Issue:

Individually, the RFID reader and MP3 module work perfectly. The RFID reader correctly reads tags and displays the data, and the MP3 module successfully plays MP3 files. However, when combined, the RFID reader fails to detect any RFID tags, consistently showing "No RFID data available" in the serial monitor.

Steps Taken:

  1. Tested Modules Individually: Both work as expected when tested separately.
  2. Checked Connections: Verified that the wiring is correct and modules are properly powered.
  3. Added Debug Messages: Added serial print statements to monitor the flow and debug.

Questions:

  1. Is there a potential conflict in using SoftwareSerial for both modules?
  2. Could the issue be related to power supply or interference between the modules?
  3. Are there any suggestions to better manage or debug this combined setup?

Any help or suggestions would be greatly appreciated. Thank you!


Feel free to adjust any details specific to your setup or add any additional information that might help others diagnose the issue.

Two instances of Softwareserial is begging for trouble.
Switch up to a 2560 or other processor with multiple hardware serial ports,

Or, if a Mega2560 is too large for your liking, a Teensy board has a much smaller footprint (similar to nano) and multiple serial ports. Same for an ESP32 nano