SD CARD and RFID not working together

Hey.
I have this project where i want to play a soundfile when a specifc RFID tag have been read.
I use a Micro sd card adapter, arduino UNO and a RFID-RC522.
I try to use SPI, by setting the slave pins of the components to HIGH or LOW to either "turn the device off" or "turn the device on". My components share the same MISO, MOSI and CLOCK pin, and each have their own individual slave pin, with the RFID also having a RST pin.
I have triple checked connections and my components work individually.
I THINK my problem is with Spi.transfer but iam not sure.
I upload the code below and hope you can help me.

#include <require_cpp11.h>
#include <MFRC522Hack.h>
#include <MFRC522Extended.h>
#include <MFRC522Debug.h>
#include <deprecated.h>
#include <SD.h>                      
#include <TMRpcm.h>                  
#include <SPI.h>
#include <MFRC522.h>


#define SDSlave        4          //CS pin fra SD card
#define RSTPIN         7          //reset pin from RFID
#define RFIDSLAVEPIN   6         // Slavepin from RFID

int pullldowndelay = 50;
TMRpcm tmrpcm;                       // create an object for use in this sketch  til lyden
MFRC522 mfrc522(RFIDSLAVEPIN, RSTPIN);  // Create MFRC522 instance til RFID scanner


void setup() {
	Serial.begin(9600);
	while (!Serial);                  // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
	SPI.begin();                      // Init SPI bus

	pinMode(RFIDSLAVEPIN, OUTPUT);
	pinMode(SDSlave, OUTPUT);

	digitalWrite(RFIDSLAVEPIN, LOW);
	digitalWrite(SDSlave, HIGH);
	delay(pullldowndelay);

	mfrc522.PCD_Init();                          // Init MFRC522
	mfrc522.PCD_DumpVersionToSerial();            // Show details of PCD - MFRC522 Card Reader details
	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

	tmrpcm.speakerPin = 9;
	digitalWrite(RFIDSLAVEPIN, HIGH);
	digitalWrite(SDSlave, LOW);
	delay(pullldowndelay);
	if (!SD.begin(SDSlave)) {                    // see if the card is present and can be initialized:
		return;                                 // don't do anything more if not
	}


}

void loop() {


	SDCARD();

}


void Sound() {
	digitalWrite(RFIDSLAVEPIN, HIGH);
	digitalWrite(SDSlave, LOW);
	delay(pullldowndelay);
	Serial.print("Sound function called");
	tmrpcm.play("lol.wav");
	delay(8000); // delay after sound is played to let the song finish
	delay(pullldowndelay);
	digitalWrite(SDSlave, HIGH);
	digitalWrite(RFIDSLAVEPIN, LOW);
}

void SDCARD() {
	// Look for new cards https://www.youtube.com/watch?v=3uWz7Xmr55c
	digitalWrite(SDSlave, HIGH);
	digitalWrite(RFIDSLAVEPIN, LOW);
	delay(pullldowndelay);
	if (!mfrc522.PICC_IsNewCardPresent()) {
		Serial.print("No new cards found");
		Serial.println();
		return;
	}

	// Select one of the cards
	if (!mfrc522.PICC_ReadCardSerial()) {
		Serial.print("Problems reading cards");
		Serial.println();
		return;
	}

	// Dump debug info about the card; PICC_HaltA() is automatically called
	mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

	//Show UID on serial monitor
	Serial.print("UID tag :");
	String content = "";
	byte letter;
	for (byte i = 0; i < mfrc522.uid.size; i++)
	{
		Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
		Serial.print(mfrc522.uid.uidByte[i], HEX);
		content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
		content.concat(String(mfrc522.uid.uidByte[i], HEX));
	}
	Serial.println();
	Serial.print("Message : ");
	content.toUpperCase();
	
	if (content.substring(1) == "3A 14 A1 E8")                 //change here the UID of the card/cards that you want to give access
	{
		Serial.println("Authorized access");  
		Serial.println();
		delay(3000);
		Sound();



	}

}

yout not the same guy from FB with the finger print scanner are ya?

what sd module are you using

Nope, but if there is a solution or a suggestion to a solution you are very welcome to link to that.

Iam not sure what the component name is. On the back in says "Micro SD card adapter" and top leeft corner it says "MH".

Can you test the 2 components (RFID reader and SD reader) separately ?
Is there a good reason for naming the routine to read the RFID card as SDCARDS() ?

What about putting a Serial.print in this to see if there has been a failure. The return statement should anyway be omitted.

if (!SD.begin(SDSlave)) {                    // see if the card is present and can be initialized:
return;                                 // don't do anything more if not
}

they have the same problem as you actually which is why i asked. they asked their question about 10 mins ago...

https://stackoverflow.com/questions/41277010/how-to-connect-rc522-rfid-module-and-sd-card-adapter-to-one-arduino-board


https://forum.arduino.cc/index.php?topic=232480.0
https://github.com/TasmanianDevilYouTube/Arduino/tree/master/RFID_master_slave_stored_to_SD

PS the kudos button is under my name :slight_smile:

1 Like

6v6gt: I have tested each component and their setup, seperately(only one of the components connected to the arduino) and they both worked, so iam 99,9% sure that the hardware is working the way it should.

The name mistake, before uploading the code i quickly renamed the functions from danish to english and mistyped what i wanted :smiley:

tbillion. Thank you, i will go have a look :slight_smile:

Thanks for the links, but i have seen them before and tried implementing there suggestions. (I have even been on page two of a Google search so you know things are serious)
If you can link me to the fb post i would appriciate it.

Are you seeing the following message on the serial console at 50 mS intervals ?

"No new cards found"

?

Arduino | Facebook?

your missing the key struct for the mifare card

**
 * ----------------------------------------------------------------------------
 * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
 * for further details and other examples.
 * 
 * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
 * 
 * Released into the public domain.
 * ----------------------------------------------------------------------------
 * This sample shows how to read and write data blocks on a MIFARE Classic PICC
 * (= card/tag).
 * 
 * BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
 * 
 * 
 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 * 
 */

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

MFRC522::MIFARE_Key key;

/**
 * Initialize.
 */
void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
    while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();        // Init SPI bus
    mfrc522.PCD_Init(); // Init MFRC522 card

    // Prepare the key (used both as key A and as key B)
    // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
    for (byte i = 0; i < 6; i++) {
        key.keyByte[i] = 0xFF;
    }

    Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
    Serial.print(F("Using key (for A and B):"));
    dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
    Serial.println();
    
    Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
}

/**
 * Main loop.
 */
void loop() {
    // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

    // Show some details of the PICC (that is: the tag/card)
    Serial.print(F("Card UID:"));
    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println();
    Serial.print(F("PICC type: "));
    MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
    Serial.println(mfrc522.PICC_GetTypeName(piccType));

    // Check for compatibility
    if (    piccType != MFRC522::PICC_TYPE_MIFARE_MINI
        &&  piccType != MFRC522::PICC_TYPE_MIFARE_1K
        &&  piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
        Serial.println(F("This sample only works with MIFARE Classic cards."));
        return;
    }

    // In this sample we use the second sector,
    // that is: sector #1, covering block #4 up to and including block #7
    byte sector         = 1;
    byte blockAddr      = 4;
    byte dataBlock[]    = {
        0x01, 0x02, 0x03, 0x04, //  1,  2,   3,  4,
        0x05, 0x06, 0x07, 0x08, //  5,  6,   7,  8,
        0x08, 0x09, 0xff, 0x0b, //  9, 10, 255, 12,
        0x0c, 0x0d, 0x0e, 0x0f  // 13, 14,  15, 16
    };
    byte trailerBlock   = 7;
    MFRC522::StatusCode status;
    byte buffer[18];
    byte size = sizeof(buffer);

    // Authenticate using key A
    Serial.println(F("Authenticating using key A..."));
    status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("PCD_Authenticate() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
    }

    // Show the whole sector as it currently is
    Serial.println(F("Current data in sector:"));
    mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
    Serial.println();

    // Read data from the block
    Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
    Serial.println(F(" ..."));
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("MIFARE_Read() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
    dump_byte_array(buffer, 16); Serial.println();
    Serial.println();

    // Authenticate using key B
    Serial.println(F("Authenticating again using key B..."));
    status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("PCD_Authenticate() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
    }

    // Write data to the block
    Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
    Serial.println(F(" ..."));
    dump_byte_array(dataBlock, 16); Serial.println();
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("MIFARE_Write() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.println();

    // Read data from the block (again, should now be what we have written)
    Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
    Serial.println(F(" ..."));
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("MIFARE_Read() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
    dump_byte_array(buffer, 16); Serial.println();
        
    // Check that data in block is what we have written
    // by counting the number of bytes that are equal
    Serial.println(F("Checking result..."));
    byte count = 0;
    for (byte i = 0; i < 16; i++) {
        // Compare buffer (= what we've read) with dataBlock (= what we've written)
        if (buffer[i] == dataBlock[i])
            count++;
    }
    Serial.print(F("Number of bytes that match = ")); Serial.println(count);
    if (count == 16) {
        Serial.println(F("Success :-)"));
    } else {
        Serial.println(F("Failure, no match :-("));
        Serial.println(F("  perhaps the write didn't work properly..."));
    }
    Serial.println();
        
    // Dump the sector data
    Serial.println(F("Current data in sector:"));
    mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
    Serial.println();

    // Halt PICC
    mfrc522.PICC_HaltA();
    // Stop encryption on PCD
    mfrc522.PCD_StopCrypto1();
}

/**
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
    }
}

6v6gt yes iam. Untill i Scan a card, it then prints the card information. I should properbly have mentioned that. So my RFID scanner does work, however the it will not play the music file

tbillion thanks for the code and the link, ill begin look at it now :slight_smile:

I found a solution for using both RFID and SD card module, I simply put a 330Ohm resistor between MISO line of SDcard and they are working.
Another solution is to use second Arduino and communicate with Softwareserial to send command and data to save and read from SD card.
Hope this will solve the problem.
:slight_smile:

Its working by put 330Ohm to the MISO line! thanks, dude! :slight_smile:

Hi There,

I am trying the same project and running to the same issue with using the Sd card and RFID kit. I am using a NANO for this project and wondering if you could please upload a wiring diagram. Much appreciated!

Alex

If I understand correctly, he is connecting the MISO ouput of the SD module to the processor through a 3.3K resistor. The MISO output of the other SPI device is connected directly to the same processor pin. The SD MISO can communicate through the resistor because the other device is behaving properly and has released its MISO. The other device can communicate because the badly-behaving SD MISO simply becomes a pullup or pulldown resistor to the other MISO when it is active, which isn't enough to pull the line to the wrong voltage. There might be a speed issue with this solution, but it is certainly worth a try.

Bonjour à tous :slight_smile:

Après tous les essaye que j'ai fait d'après plusieurs solutions proposé par des développeurs je n'ai pas fini mon projet

Alors j'ai décidé de trouver une solution radicale à ce sujet et en fin je viens de trouver la solution le 11/01/2020.

Maintenant mon projet et fonctionnel avec la carte SDCard RFid (MFC522), (LCD 16*2) et Clock (RS2332).

Voici mon code :

After all the tests that I did according to several solutions proposed by developers I have not finished my project

So I decided to find a radical solution on this subject and in the end I just found the solution on 01/11/2020.

Now my project is functional with the SDCard RFid card (MFC522), (LCD 16 * 2) and Clock (RS2332).

Here is my code:

mfrc522.PICC_HaltA();
mfrc522.PCD_SoftPowerDown();
.....
mfrc522.PCD_Init();

Le code source complet un cadeau pour vous tous. :slight_smile: 8)

// Ghariani Rafik ORCA & BABYLONE Copyright 1995 - 2020 
// Fichier pour la sauvegarde des evenements d'access "DoorLog.txt"
// Fichier pour la sauvegarde Meteo "RainLog.txt"
#include <SPI.h>
#include <DS3231.h>
#include <MFRC522.h>
#include <SD.h>
#include <LiquidCrystal.h>
#define SDA A4
#define SCL A5
#define SS_PIN 10
#define RST_PIN 8
#define csPin 9
#define LED_R A0
#define LED_G A1
//#define LED_B A2
//#define btnscrt A2
#define BUZZER A3
#define RELAY_O A1
const int pinBuz = A3; //Buzzer Pin
//const int pinSwi = A2
#define pinSwi A2
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
DS3231 rtc(SDA, SCL);
MFRC522 mfrc522(SS_PIN, RST_PIN);
File myFile;
void setup() 
{ Serial.begin(9600);
  SPI.begin();
  rtc.begin();
  SD.begin(csPin);
  lcd.begin(16, 2);
  pinMode(LED_R, OUTPUT);
  pinMode(LED_G, OUTPUT);
//  pinMode(LED_B, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(RELAY_O, OUTPUT);
  noTone(BUZZER);
 // pinMode(btnscrt, INPUT);
  pinMode(pinSwi,INPUT);
mfrc522.PICC_HaltA();
mfrc522.PCD_SoftPowerDown();
  if(!SD.begin(csPin)) {
    Serial.println("initialization failed!");
    lcd.print("     Failed! ");
    return; }
 { Serial.println("initialization done...");
 lcd.print("    Done... ");
 delay(1000);
 }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("      ORCA ");
  lcd.setCursor(0, 1);
  lcd.print("    BABYLONE");
  delay(3000);
  mfrc522.PCD_Init();
  }
void loop()
{ lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Time:  ");
  lcd.print(rtc.getTimeStr());
  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  lcd.print(rtc.getDateStr());
  delay(1000);
   int btnscrt;
btnscrt = digitalRead(pinSwi);
if (btnscrt == 1)
      { digitalWrite(pinBuz,1);
        tone(BUZZER, 435);
    delay(300);
        tone(BUZZER, 580);
    delay(300);} else
      {noTone(BUZZER);}
     mfrc522.PCD_SoftPowerDown();
  if ( ! mfrc522.PICC_IsNewCardPresent())
  { return; }
  if ( ! mfrc522.PICC_ReadCardSerial())
  { return; }
  Serial.print("UID :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {  Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX)); }
   Serial.println("");
  content.toUpperCase();
  if (content.substring(1) == "FD A9 DA C1")
  { delay(500);
     mfrc522.PICC_HaltA();
     mfrc522.PCD_SoftPowerDown();
      myFile = SD.open("doorlog.txt", FILE_WRITE);
    myFile.print("Prenom-1 Nom-1         |   FD A9 DA C1   | Acces autorise      |   ");
 SDopen();
 dooro();
    delay(5000);
 SDclose();
 doorc();
    delay(500);
 mfrc522.PCD_SoftPowerUp();
 mfrc522.PCD_Init(); }
  else if (content.substring(1) == "A3 EA 5B 22")
  { delay(500);
     mfrc522.PICC_HaltA();
     mfrc522.PCD_SoftPowerDown();
      myFile = SD.open("doorlog.txt", FILE_WRITE);
    myFile.print("Master root            |   A3 EA 5B 22   | Acces autorise      |   ");
 SDopen();
 dooro();
    delay(5000);
 SDclose();
 doorc();
    delay(500);
 mfrc522.PCD_SoftPowerUp();
 mfrc522.PCD_Init(); }
  else if (content.substring(1) == "9E 9B B1 C3")
  { delay(500);
     mfrc522.PICC_HaltA();
     mfrc522.PCD_SoftPowerDown();
      myFile = SD.open("doorlog.txt", FILE_WRITE);
    myFile.print("Prenom-2 Nom-2       |   9E 9B B1 C3   | Acces autorise      |   ");
 SDopen();
 dooro();
    delay(5000);
 SDclose();
 doorc();
    delay(500);
 mfrc522.PCD_SoftPowerUp();
 mfrc522.PCD_Init(); }
  else if (content.substring(1) == "29 6E 44 C4")
 { delay(500);
      mfrc522.PICC_HaltA();
     mfrc522.PCD_SoftPowerDown();
   myFile = SD.open("doorlog.txt", FILE_WRITE);
   myFile.print("Prenom-3 Nom-3         |   29 6E 44 C4   | Acces non autorise  |   ");
 SDlocked();
  doorl();
  delay(1200);
 mfrc522.PCD_SoftPowerUp();
 mfrc522.PCD_Init(); }
 else   { mfrc522.PICC_HaltA();
     mfrc522.PCD_SoftPowerDown();
myFile = SD.open("doorlog.txt", FILE_WRITE);
    myFile.print("Unknow                 |  ");
    for (byte i = 0; i < mfrc522.uid.size; i++) {
    myFile.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     myFile.print(mfrc522.uid.uidByte[i], HEX); } 
 SDprotect();
 doorp();
    delay(1000);  }
 mfrc522.PCD_SoftPowerUp();
 mfrc522.PCD_Init();
}
void SDopen()
{     myFile.print(rtc.getTimeStr()  );
      myFile.print(" |   ");
      myFile.print(rtc.getDateStr());
    myFile.print("  |  Door opens     | ");
    myFile.print(rtc.getTimeStr()  ); }
void SDclose()
{   myFile.print("  |  Door closes | ");
    myFile.println(rtc.getTimeStr()  );
   myFile.close(); }
void SDlocked()
{  myFile.print(rtc.getTimeStr()  );
   myFile.print(" |   ");
   myFile.print(rtc.getDateStr());
      myFile.print("  |  Locked door    | ");
    myFile.print(rtc.getTimeStr()  );
    myFile.println("  |              |");
  myFile.close(); }
void SDprotect()
{ myFile.print("   | Acces denied        |   ");
      myFile.print(rtc.getTimeStr()  );
      myFile.print(" |   ");
      myFile.print(rtc.getDateStr());
      myFile.println("  | Protected door  |");
    myFile.close(); }
void dooro()
{         digitalWrite(LED_G, HIGH);
          tone(BUZZER, 2000);
          delay(300);
          noTone(BUZZER);
          delay(1000);
          digitalWrite(LED_G, LOW);
          digitalWrite(RELAY_O, HIGH);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Door opens..."); }
void doorc()
{         digitalWrite(RELAY_O, LOW);
          lcd.setCursor(0, 1);
          lcd.println("Door closes !!!"); delay(60000); }
void doorl()
{ digitalWrite(LED_R, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    tone(BUZZER, 400);
    delay(300);
    noTone(BUZZER);
     delay(1200);
     digitalWrite(LED_R, LOW);
    lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Locked door !!!"); }
void doorp()
{ lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Access denied");
  lcd.setCursor(0, 1);
  lcd.print("Protected door !");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1800);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER); }

Is it a 3.3k or 330R resistor?

Ok...I enlarged the photo and confirmed it was a 330R resistor. Thanks

one and standerd solution for this" two spi device cant work togather" you need to connect both via 3-state buffer ic like 74244 and both run as your wish