RFID card UID not displaying in serial monitor

pretty new to programming in general. i having trouble getting this to actually output the RFID tags UID to the serial monitor. I need to know it in order to name the .wav files the same thing. in the serial monitor it just says " Card UID: " with on uid after. why would this be?

#include <SD.h>
#include <MFRC522.h>

// Set up the RFID reader
const byte RST_PIN = 9;                 // Reset pin for the RFID reader
const byte RFID_SS_PIN = 10;            // SS (slave select) pin for the RFID reader
MFRC522 mfrc522(RFID_SS_PIN, RST_PIN);  // Create an instance of the RFID reader

// Set up the micro SD card
const int chipSelect = 4;  // Chip select pin for the SD card module

void setup() {
  Serial.begin(9600);  // Initialize serial communication
  SPI.begin();         // Initialize the SPI bus
  mfrc522.PCD_Init();  // Initialize the RFID reader

  // Initialize the SD card
  Serial.println("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Error initializing SD card");
    return;
  }
  Serial.println("SD card initialized");
}

void loop() {
  // Look for an RFID tag
  if (mfrc522.PICC_IsNewCardPresent()) {
    // Select the RFID tag
    MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);

    // Read the UID (unique identifier) of the RFID tag
    String uid = "";
    for (byte i = 0; i < mfrc522.uid.size; i++) {
      uid += String(mfrc522.uid.uidByte[i], HEX);
    }
    Serial.println("Card UID: " + uid);


    // Open the corresponding file on the SD card based on the UID of the RFID tag
    String fileName = uid + ".wav";  // Assume the file is a WAV
    File songFile = SD.open(fileName);
    if (!songFile) {
      Serial.println("Error opening file");
      return;
    }

    // Play the song
    // (You'll need to use a music player library or something similar to actually play the song)
  }
}

you are missing the reading of the UID

// Look for an RFID tag
if (mfrc522.PICC_IsNewCardPresent()) {
  if (rfid.PICC_ReadCardSerial()) {
     ... //now you can read the info

    // when you are done
    rfid.PICC_HaltA();           // Halt PICC
    rfid.PCD_StopCrypto1();      // Stop encryption on PCD
  }
}

Oh sweet thanks!! got it now

have fun

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