RFID Reader with Micro SD card Reader on Arduino Mega

I'm doing a project and I need to use RFID and micro sd card reader together on arduino mega. So I did the connections and use SPI connections on the same pins. For example, RFID's SCK and SD card reader's SCK to 52 pin. I use the correct SS pins to but it does not work. RFID some times works after I put 330 ohm resistor between sd card's miso pin and mega's miso pin but after scanning some card's it starts not working. My project is due 1 week and it is the last thing i need to do.

Code:

#include <SPI.h>
#include <TMRpcm.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <SD.h>
#include <MFRC522.h>
#include <LiquidCrystalTr.h>
#include <Keypad.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 10);  

//Speacker and Micro SD Card Reader
#define sdpin 13

TMRpcm tmrpcm;

// Creating RFID
#define RST_PIN         8        
#define SS_PIN          9

MFRC522 mfrc522(SS_PIN, RST_PIN);

MFRC522::MIFARE_Key key;

//KEYPAD
const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {37, 36, 35, 34}; 
byte colPins[COLS] = {33, 32, 31, 30}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

bool captureInput = false; 
String capturedInput = "";

//LCD

const int rs = 6, en =7, d4 =2, d5 = 3, d6=4, d7=5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int wb = 11;
int wa = 0;


//Students and Teacher

byte cardID[4];
int ID[4];
int slot = 0;


byte Tcard[4] = {0xc3, 0xc1, 0x90, 0xa5};

char Tname[] = "Mahmut Ağan";

char Tid[] = "6546";


byte card[7][4] = {{0x63, 0x96, 0x15, 0x34}, {0x63, 0x95, 0xa1, 0x34}, {0xa1, 0x05, 0xd3, 0x1d}, {0x91, 0xaa, 0x92, 0x1d}, {0x23, 0x5D, 0x18, 0x16}, {0x93, 0x75, 0x27, 0x16}, {0x50, 0x15, 0xCA, 0x80}};

char name[7][30] = {" Charlotte Taylor-Smith  ", " Elizabeth Woolridge Grant  ", "  Kağan Akgün", "   Emre Göğer", "Jasper Fahey", "Murathan Hatay", "Emirhan Çoban"};

char id[7][4] = {"7849", "5925", "2858", "9298", "9132", "2201", "4234"};

bool att[7] = {0, 0, 0, 0, 0, 0, 0};

bool dis[7] = {1, 0, 0, 0, 1, 0, 1};

bool te = 0;

//Fun Card

byte fun[4] = {0xe3, 0x72, 0x13, 0xad};

//

int timer = 0, ftimer = 0, ntimer = 0, stimer = 0;

char * start = " Scan card or Press 'A'  ";
char * attendance = " Attendance has already been taken   ";

int iLineNumber = 1;  
int LineNumber = 0;                                                                 // Line number to show your string (Either 0 or 1)

int iCursor = 0;

int place;

String sending;
//

void setup() {
  mySerial.begin(9600); 
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  lcd.begin(16, 2); 
  //Speaker and sd
  tmrpcm.speakerPin = 12;
  while(!SD.begin(sdpin)){
    lcd.print("problem");
    delay(1000);
    lcd.clear();
  }
  tmrpcm.setVolume(1);
}     



void loop() { 

  if(mySerial.available() > 0){
    char sdata[10]; 
    mySerial.readBytesUntil('\n', sdata, 10);
    Serial.println(sdata);
    int rdata = atoi(sdata);
    while(rdata == 1){
      lcd.setCursor(3, 0);
      lcd.print("Connection");
      lcd.setCursor(5, 1);
      lcd.print("Error");
      mySerial.readBytesUntil('\n', sdata, 10);
      int rdata = atoi(sdata);
      Serial.println(rdata);
      if(rdata == 0){
        break;
      }
      delay(1000);
      lcd.clear();
    }
  }


  for(int i = 0; i < 4; i++){
    ID[i] = 0;
  }
  
  lcd.setCursor(4,0);
  lcd.print("EEE1002");
  lcd.setCursor(0,1);
  Scroll(start);
  
  stimer = 0;
  while(stimer < 8){
    delay(50);
    stimer++;
    char key = customKeypad.getKey();

    //Starts the ID taking from keypad if 'A' is pushed

    if(key){
      
      if(key == 'A'){

        lcd.clear();
        lcd.print("Please enter ID:");

        while(1){

          key = customKeypad.getKey();

          if(key){

            //Takes the written ID

            if(slot<=3 && slot>=0 && key != 'A' && key != 'B' && key != 'C' && key != 'D' && key != '#' && key != '*'){
              ID[slot]= key;
              lcd.setCursor(slot,1);
              lcd.print(key);
              slot++;
            }

            //Deletes one character while writing the ID

            if(key == 'C'){
              if(slot>0&&slot<=4){
                slot=slot-1;
                lcd.setCursor(slot,1);
                lcd.print(" ");
              }
            }
            
            //Cancels the operation

            if(key == 'D'){
              slot=0;
              break;
            }

            //Accepts the written ID

            if(key == 'B'){
              tmrpcm.play("abcd.wav");
              lcd.clear();
              Wait();
              lcd.clear();
              
              //ID Part
              
              if(ID[0] == Tid[0] && ID[1] == Tid[1] && ID[2] == Tid[2] && ID[3] == Tid[3] && te == 0 && ftimer == 0){
                tmrpcm.play("efgh.wav");
                te=1;
                mySerial.println("-1");
                if(strlen(Tname) > 16){
                  while(ntimer < 30){
                    Scroll(Tname);  
                    delay(280);
                    ntimer++; 
                  }
                }else{
                  place = floor((16 - strlen(Tname))/2);
                  lcd.setCursor(place,0);
                  lcd.print(Tname);
                }
                ntimer = 0;
                iCursor = 0;
                while(ntimer < 30){
                  Scroll(" Attendance taking has been started  ");
                  delay(300);
                  ntimer++;
                }
                lcd.clear();
                timer = 0;
                ntimer = 0;
                iCursor = 0;
                lcd.clear();
                slot=0;
                return;
              }
              else if(ID[0] == Tid[0] && ID[1] == Tid[1] && ID[2] == Tid[2] && ID[3] == Tid[3] && te == 1){
                tmrpcm.play("defg.wav");
                te=0;
                for(int b = 0; b < 7; b++){
                  att[b] = 0;
                }
                if(strlen(Tname) > 16){
                  while(ntimer < 30){
                  Scroll(Tname);  
                  delay(280);
                  ntimer++; 
                  }
                }else{
                  place = floor((16 - strlen(Tname))/2);
                  lcd.setCursor(place,0);
                  lcd.print(Tname);
                }
                ntimer = 0;
                iCursor = 0;
                while(ntimer < 30){
                  Scroll(" Attendance taking has been over  ");
                  delay(280);
                  ntimer++;
                }
                ntimer = 0;
                lcd.clear();
                slot=0;
                timer = 0;
                iCursor = 0;
                return;
              }
              else if(ID[0] == Tid[0] && ID[1] == Tid[1] && ID[2] == Tid[2] && ID[3] == Tid[3] && ftimer > 0){
                tmrpcm.play("defg.wav");
                ntimer = 0;
                iCursor = 0;
                while(ntimer < 30){
                  Scroll1(" Attendance taking has been over  ");
                  delay(280);
                  ntimer++;
                }
                lcd.clear();
                slot=0;
                timer = 0;
                iCursor = 0;
                return;
              }

              for(int i = 0; i < 7; i++){
                if(ID[0] == id[i][0] && ID[1] == id[i][1] && ID[2] == id[i][2] && ID[3] == id[i][3] && te == 1 && att[i] == 0){ //Takes the
                  if(dis[i] == 1){
                    tmrpcm.play("ghij.wav");
                  }
                  int b = i+1;
                  Serial.println(b);
                  iCursor = 0;
                  lcd.setCursor(4, 0);
                  lcd.print("Welcome");
                  if(strlen(name[i]) > 16){
                    while(ntimer < 30){
                    Scroll(name[i]);
                    delay(280);
                    ntimer++; 
                    }
                  }else{
                    place = floor((16 - strlen(name[i]))/2);
                    lcd.setCursor(place,1);
                    lcd.print(name[i]);
                    delay(4000);
                  }
                  lcd.clear();
                  att[i] = 1;
                  timer = 0;
                  ntimer = 0;
                  return;
                }
                else if(ID[0] == id[i][0] && ID[1] == id[i][1] && ID[2] == id[i][2] && ID[3] == id[i][3] && te == 1 && att[i] == 1){ 
                  if(dis[i] == 1){
                    tmrpcm.play("fghi.wav");
                  }
                  iCursor = 0;
                  while(ntimer < 30){
                    Scroll1(attendance);
                    delay(280);
                    ntimer++;
                  }
                  lcd.clear();
                  timer = 0;
                  ntimer = 0;
                  iCursor = 0;
                  return;
                  return;
                }
                else if(ID[0] == id[i][0] && ID[1] == id[i][1] && ID[2] == id[i][2] && ID[3] == id[i][3] && te == 0){ 
                  if(dis[i] == 1){
                    tmrpcm.play("cdef.wav"); 
                  }
                  iCursor = 0;
                  lcd.setCursor(0,0);
                  lcd.print("Wait the Teacher");
                  while(ntimer < 30){
                    Scroll(" Attendance taking has not been started  ");
                    delay(280);
                    ntimer++;
                  }
                  ntimer = 0;
                  iCursor = 0;
                   lcd.clear();
                   slot=0;
                   return;
                }
              }

              //ID is not in the class list
              iCursor = 0;
      
              tmrpcm.play("bcde.wav");
              delay(1000);
              tmrpcm.play("hijk.wav");

              while(ntimer < 35){
                Scroll1(" You are not a participant in this course  ");
                delay(280);
                ntimer++;
              }
              ntimer = 0;
              iCursor = 0;
              lcd.clear();

              slot=0;
              return;
            }
          }
        }
      }
    }
    
  }

  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    if(te == 1){
      timer++;
      delay(50);//0.05 seconds
    }

    if(timer == 8000){// 8000 about 11 minutes
      ftimer = 1;
      timer = 0;
      te = 0;
      for(int b = 0; b < 7; b++){
        att[b] = 0;
      }
      lcd.clear();
      ntimer = 0;
      iCursor = 0;
      while(ntimer < 30){
        Scroll(" Attendance taking has been over  ");
        delay(280);
        ntimer++;
      }
      ntimer = 0;
      iCursor = 0;
      lcd.clear();
    }

    if(ftimer > 0){
      ftimer++;
      delay(50);
    }

    if(ftimer == 8000){ // 16000 about 22 minutes
      ftimer = 0;
    }

  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    lcd.clear();
    return;
  }
  
  tmrpcm.play("abcd.wav");

  lcd.clear();
  Wait();
  lcd.clear();

  //Get the UID of the scaned card 

  for (byte i = 0; i < mfrc522.uid.size; i++) {
     cardID[i]=mfrc522.uid.uidByte[i];
  }

  //Fun Card

  if(cardID[0] == fun[0] && cardID[1] == fun[1] && cardID[2] == fun[2] && cardID[3] == fun[3]){
    lcd.print("how do you have");
    lcd.setCursor(0,1);
    lcd.print("this card");
    delay(3000);
    lcd.clear();
    return;
  }

  // CARD

  if(cardID[0] == Tcard[0] && cardID[1] == Tcard[1] && cardID[2] == Tcard[2] && cardID[3] == Tcard[3] && te == 0 && ftimer == 0){
    tmrpcm.play("efgh.wav");
    te=1;
    char sendData[] = "-1";
    mySerial.println(sendData);
    if(strlen(Tname) > 16){
      while(ntimer < 30){
        Scroll(Tname);  
        delay(280);
        ntimer++; 
      }
    }else{
      place = floor((16 - strlen(Tname))/2);
      lcd.setCursor(place,0);
      lcd.print(Tname);
    }
    ntimer = 0;
    iCursor = 0;
    while(ntimer < 30){
      Scroll(" Attendance taking has been started  ");
      delay(300);
      ntimer++;
    }
    lcd.clear();
    timer = 0;
    ntimer = 0;
    iCursor = 0;
    lcd.clear();
    slot=0;
    return;
  }
  else if(cardID[0] == Tcard[0] && cardID[1] == Tcard[1] && cardID[2] == Tcard[2] && cardID[3] == Tcard[3] && te == 1){
    tmrpcm.play("defg.wav");
    te=0;
    for(int b = 0; b < 7; b++){
      att[b] = 0;
    }
    if(strlen(Tname) > 16){
    while(ntimer < 30){
      Scroll(Tname);  
      delay(280);
      ntimer++; 
    }
    }else{
      place = floor((16 - strlen(Tname))/2);
      lcd.setCursor(place,0);
      lcd.print(Tname);
    }
    ntimer = 0;
    iCursor = 0;
    while(ntimer < 30){
      Scroll(" Attendance taking has been over  ");
      delay(280);
      ntimer++;
    }
    lcd.clear();
    slot=0;
    timer = 0;
    iCursor = 0;
    return;
  }
  else if(cardID[0] == Tcard[0] && cardID[1] == Tcard[1] && cardID[2] == Tcard[2] && cardID[3] == Tcard[3] && ftimer > 1){
    tmrpcm.play("defg.wav");
    ntimer = 0;
    iCursor = 0;
    while(ntimer < 30){
      Scroll1(" Attendance taking has been over  ");
      delay(280);
      ntimer++;
    }
    lcd.clear();
    slot=0;
    timer = 0;
    iCursor = 0;
    return;
  }

  for(int i = 0; i < 7; i++){

    if(cardID[0] == card[i][0] && cardID[1] == card[i][1] && cardID[2] == card[i][2] && cardID[3] == card[i][3] && te == 1 && att[i] == 0){ 
      int b = i+1;
      char sendData[10];
      sprintf(sendData, "%d", b);
      mySerial.println(sendData);
      if(dis[i] == 1){
        tmrpcm.play("ghij.wav");
      }
      iCursor = 0;
      lcd.setCursor(4, 0);
      lcd.print("Welcome");
      if(strlen(name[i]) > 16){
        while(ntimer < 30){
          Scroll(name[i]);  
          delay(280);
          ntimer++; 
        }
      }else{
        place = floor((16 - strlen(name[i]))/2);
        lcd.setCursor(place,1);
        lcd.print(name[i]);
        delay(4000);
      }
      lcd.clear();
      att[i] = 1;
      timer = 0;
      ntimer = 0;
      iCursor = 0;
      return;
    }
    else if(cardID[0] == card[i][0] && cardID[1] == card[i][1] && cardID[2] == card[i][2] && cardID[3] == card[i][3] && te == 1 && att[i] == 1){ 
      if(dis[i] == 1){
        tmrpcm.play("fghi.wav");
      }
      iCursor = 0;
      while(ntimer < 30){
       Scroll1(attendance);
       delay(280);
       ntimer++;
      }
      lcd.clear();
      timer = 0;
      ntimer = 0;
      iCursor = 0;
      return;
    }
    else if(cardID[0] == card[i][0] && cardID[1] == card[i][1] && cardID[2] == card[i][2] && cardID[3] == card[i][3] && te == 0){ //Does not take attendance because the lesson is deactive
      if(dis[i] == 1){
        tmrpcm.play("cdef.wav"); 
      }
      iCursor = 0;
      lcd.setCursor(0,0);
      lcd.print("Wait the Teacher");
      while(ntimer < 30){
       Scroll(" Attendance taking has not been started  ");
       delay(280);
       ntimer++;
      }
      ntimer = 0;
      iCursor = 0;
      lcd.clear();
      slot=0;
      return;
    }
  }
  
  //Card is not in the class list
  iCursor = 0;
      
  tmrpcm.play("bcde.wav");
  delay(1000);
  tmrpcm.play("hijk.wav");

  while(ntimer < 35){
    Scroll1(" You are not a participant in this course  ");
    delay(280);
    ntimer++;
  }
  ntimer = 0;
  iCursor = 0;
  lcd.clear();

  slot=0;
}


void Scroll(const char * text) {
  int iLenOfLargeText = strlen(text);                                            // Calculate lenght of string.
  if (iCursor == (iLenOfLargeText - 1) )                                              // Reset variable for rollover effect.
  {
    iCursor = 0;
  }
  //lcd.clear();
  lcd.setCursor(0,iLineNumber);
  
  if(iCursor < iLenOfLargeText - 16)                                                  // This loop exicuted for normal 16 characters.
  {
    for (int iChar = iCursor; iChar < iCursor + 16 ; iChar++)
    {
      lcd.print(text[iChar]);
    }
  }
  else
  {
    for (int iChar = iCursor; iChar < (iLenOfLargeText - 1) ; iChar++)                //  This code takes care of printing charecters of current string.
    {
      lcd.print(text[iChar]);
    }
    for (int iChar = 0; iChar <= 16 - (iLenOfLargeText - iCursor); iChar++)           //  Reamining charecter will be printed by this loop.
    {
      lcd.print(text[iChar]);   
    }
  }
  iCursor++;
}

void Scroll1(const char * text) {
  int iLenOfLargeText = strlen(text);                                            // Calculate lenght of string.
  if (iCursor == (iLenOfLargeText - 1) )                                              // Reset variable for rollover effect.
  {
    iCursor = 0;
  }
  //lcd.clear();
  lcd.setCursor(0,LineNumber);
  
  if(iCursor < iLenOfLargeText - 16)                                                  // This loop exicuted for normal 16 characters.
  {
    for (int iChar = iCursor; iChar < iCursor + 16 ; iChar++)
    {
      lcd.print(text[iChar]);
    }
  }
  else
  {
    for (int iChar = iCursor; iChar < (iLenOfLargeText - 1) ; iChar++)                //  This code takes care of printing charecters of current string.
    {
      lcd.print(text[iChar]);
    }
    for (int iChar = 0; iChar <= 16 - (iLenOfLargeText - iCursor); iChar++)           //  Reamining charecter will be printed by this loop.
    {
      lcd.print(text[iChar]);   
    }
  }
  iCursor++;
}

void Wait(){
  lcd.print("Please Wait");
  while(wa <9){
    if(wb % 3 == 2){
      lcd.setCursor(12, 0);
      lcd.print("  ");
      lcd.setCursor(11, 0);
      lcd.print(".");
    }
    else if(wb % 3 == 0){
      lcd.setCursor(11, 0);
      lcd.print(" ");
      lcd.setCursor(13, 0);
      lcd.print(" ");
      lcd.setCursor(12, 0);
      lcd.print(".");
    }
    else if(wb % 3 == 1){
      lcd.setCursor(11, 0);
      lcd.print("  ");
      lcd.setCursor(13, 0);
      lcd.print(".");
    }
    delay(300);
    wa++;
    wb++;
  }
  wa = 0;
  wb = 11;
}

What Arduino board? That should always be mentioned in the OP.
My apologies. I missed that your board is a Mega.

What, exact, SD card reader do you have? Do you have a schematic of the reader?

Some SD card modules will not play well with other devices on the SPI bus. The problem is with the way the level shifter on the SD module is wired. The ones that do not work well have the MISO signal running through the level shifter. That causes the MISO signal to not be released so that the other devices can control it. The modules made by Adafruit (and some others) have the MISO signal going from the SD card straight to the MISO, bypassing the level shifter. Look at the schematic for the Adafruit module to see what to look for. DO (data out) is the MISO signal.

A solution is to use the hardware SPI for one device and a software SPI for the other.

....

Thanks, not sure how i missed that.

I skipped over it, too... then I read your comment... : )

On the SD card reader, it says HW-125. I am very new to this and this is my first year as an electrical and electronics engineering student.

Get each piece working separately, THEN integrate. Use a screw terminal shield for reliable wiring. You can also put two wires in one screw terminal.
Use a separate +5V power supply or the USB instead of Vin to insure enough power. SD card alone can draw 125mA on top of what Mega needs (~200mA) and that will overload mega onboard regulator, if using Vin, and drawing from mega's +5 for sd card power.
SD cards don't reset when cpu does after downloading. Power cycle after downloading to insure sync.
tip: sd cards have 32gb limit (8gb works well) & DOS 8.3 filename restrictions.

In-Depth Tutorial to Interface Micro SD Card Module with Arduino (lastminuteengineers.com)

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