Multiple RFID readers connected to Arduino Uno

Hy, I have problems with multiple RFID(MFRC522) usage. I use the MFRC522.h library from https://github.com/miguelbalboa/rfid, and I want to read 8 RFID modules with one Arduino Uno, but I have managed only 2! I connect all the SPI pins, SS1 to pin10 and SS2 to pin2. How can I get to read 8 RFID with one Arduino Uno?

 #include 
    #include 

    #define RST_PIN         9          // Configurable, see typical pin layout above
    #define SS_1_PIN        10         // Configurable, see typical pin layout above
    #define SS_2_PIN        2         // Configurable, see typical pin layout above

    //#define SS_3_PIN        10

    #define LedPin          8
    #define NR_OF_READERS  2

    byte ssPins[] = {SS_1_PIN, SS_2_PIN};
    int a=0;
    int b=0;

    MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.

    /**
       * Initialize.
     */
    void setup() {

    Serial.begin(115200); // Initialize serial communications with the PC
    //while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    pinMode(LedPin,OUTPUT);
    SPI.begin();        // Init SPI bus

    for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
    }
    }

    /**
    * Main loop.
       */
       void loop() {
       for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
       // Look for new cards
    
    
    if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
      Serial.print(F("Reader: "));
      Serial.print(reader);
      // Show some details of the PICC (that is: the tag/card)
      Serial.print(F(" Card UID:"));
      dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
      Serial.println();
     Serial.print(F("PICC type: "));
      MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
      Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
              if (mfrc522[reader].uid.uidByte[0] == 140 &
              mfrc522[reader].uid.uidByte[1] == 120 & 
              mfrc522[reader].uid.uidByte[2] == 34 & 
              mfrc522[reader].uid.uidByte[3] == 43 & 
              reader==0 ) {
              Serial.println(F("pirmā karte atrasta"));  
              a++; 
              }
                if (mfrc522[reader].uid.uidByte[0] != 140 &
              mfrc522[reader].uid.uidByte[1] != 120 & 
              mfrc522[reader].uid.uidByte[2] != 34 & 
              mfrc522[reader].uid.uidByte[3] != 43 & 
              reader==0 ) {
              Serial.println(F("pirmā karte nodzes"));  
              a=0; 
              }
              
             if (mfrc522[reader].uid.uidByte[0] == 5 &
              mfrc522[reader].uid.uidByte[1] == 233 & 
              mfrc522[reader].uid.uidByte[2] == 26 & 
              mfrc522[reader].uid.uidByte[3] == 43 & 
              reader==1 ) {
              Serial.println(F("otrā karte atrasta"));
              b++;   
              }
          if (mfrc522[reader].uid.uidByte[0] != 5 &
              mfrc522[reader].uid.uidByte[1] != 233 & 
              mfrc522[reader].uid.uidByte[2] != 26 & 
              mfrc522[reader].uid.uidByte[3] != 43 & 
              reader==1 ) {
              Serial.println(F("otrā karte nodzēs"));
              b=0;   
              }
              
              if (mfrc522[reader].uid.uidByte[0] == 140 &
              mfrc522[reader].uid.uidByte[1] == 120 & 
              mfrc522[reader].uid.uidByte[2] == 34 & 
              mfrc522[reader].uid.uidByte[3] == 43 & 
              reader==1 ) {
                Serial.println(F("otrā karte nodzēs"));
              b=0;   
              }
              
                       if (mfrc522[reader].uid.uidByte[0] == 5 &
              mfrc522[reader].uid.uidByte[1] == 233 & 
              mfrc522[reader].uid.uidByte[2] == 26 & 
              mfrc522[reader].uid.uidByte[3] == 43 & 
             reader==0 ) {
              Serial.println(F("pirmā karte nodzes"));  
              a=0; 
              }
    Serial.println();

      // Halt PICC
      mfrc522[reader].PICC_HaltA();
      // Stop encryption on PCD
      mfrc522[reader].PCD_StopCrypto1();
      } //if (mfrc522[reader].PICC_IsNewC
      } //for(uint8_t reader
      if (a>0 & b>0)
      {
      digitalWrite(LedPin,HIGH);
       delay(1000);
      digitalWrite(LedPin,LOW); 
      }

  

    }

    /**
    * 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);
     }
    }

It really does not make sense to use a for loop when every iteration of the loop results in different action. Dump the stupid for loop, and create a function. Call the function 8 times.

Explain what happens when you connect more than 2 devices.

Explain why you are not using memcmp() to compare the array of bytes for a tag to a set of known values.

I wrote the code different from the first one. I have the problem that, when I connect only one RFID reader, then it works, I can change the SS pins for the reader between 10 and 2, and everithing is OK. But when I connect now two RFID readers, nothing happens, I don't know, how to let the Arduino choose one of the two RFID, by changing the chip select.

#include 
#include 

#define SS_1_PIN 10
#define SS_2_PIN 2
#define RST_PIN 9

MFRC522 rfid(SS_1_PIN, RST_PIN); // Instance of the class
MFRC522 rfid1(SS_2_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 
// Init array that will store new NUID 
byte nuidPICC[3];
void setup() { 
  Serial.begin(9600);
}

void loop() {
RFID();
RFID1(); 
     



}

//===========================================

void RFID()
{
  //Serial.println("pirmais");
    SPI.begin(); // Init SPI bus
 rfid.PCD_Init(); // Init MFRC522 
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  } 
  // Look for new 1 cards
  if ( ! rfid.PICC_IsNewCardPresent())
  return;
    // Verify if the NUID has been readed
  if (  !rfid.PICC_ReadCardSerial())
  return;

  // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
      
    }
    
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();

  // Halt PICC
  rfid.PICC_HaltA();


  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  SPI.end();
}

//==============================================
void RFID1()
{
   //Serial.println("otrais");
   SPI.begin(); // Init SPI bus
  rfid1.PCD_Init(); // Init MFRC522
   for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  
 if ( ! rfid1.PICC_IsNewCardPresent())
 return;      
 // Verify if the NUID has been readed
 if ( ! rfid1.PICC_ReadCardSerial())
     return; 
  // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid1.uid.uidByte[i];
    }
    Serial.print(F("In dec: "));
    printDec(rfid1.uid.uidByte, rfid1.uid.size);
    Serial.println();

  // Halt PICC
  rfid1.PICC_HaltA();

  // Stop encryption on PCD
  rfid1.PCD_StopCrypto1();
   
   SPI.end();
}
/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

SPI.begin() belongs in setup(). SPI.end() belongs in the bit bucket.

The calls to PCD_Init() belong in setup().

byte nuidPICC[3];

    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];

The 4 elements from rfid.uid.uidByte will still NOT fit in a 3 element array.

You still need to post a link (NOT just a URL) to the hardware you are trying to use.

I made the correction for the code which you said, but still my Arduino Uno don't read both RFID readers. The same situation, I think it doesn't select slave device.

#include 
#include 

#define SS_1_PIN 10
#define SS_2_PIN 2
#define RST_PIN 9

MFRC522 rfid(SS_1_PIN, RST_PIN); // Instance of the class
MFRC522 rfid1(SS_2_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 
// Init array that will store new NUID 
byte nuidPICC[4];
void setup() { 
  Serial.begin(9600);
 SPI.begin(); // Init SPI bus
 rfid.PCD_Init(); // Init MFRC522 
 rfid1.PCD_Init(); // Init MFRC522
}

void loop() {
RFID();
RFID1(); 
     



}

//===========================================

void RFID()
{
  //Serial.println("pirmais");

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  } 
  // Look for new 1 cards
  if ( ! rfid.PICC_IsNewCardPresent())  
  return;

    // Verify if the NUID has been readed
  if (  !rfid.PICC_ReadCardSerial())
  return;

  // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i]; 
    }
    
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();

  // Halt PICC
  rfid.PICC_HaltA();


  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

//==============================================
void RFID1()
{
   for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  
 if ( ! rfid1.PICC_IsNewCardPresent())
 return; 
     
 // Verify if the NUID has been readed
 if ( ! rfid1.PICC_ReadCardSerial())
     return;
  
  // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid1.uid.uidByte[i];
    }
    Serial.print(F("In dec: "));
    printDec(rfid1.uid.uidByte, rfid1.uid.size);
    Serial.println();

  // Halt PICC
  rfid1.PICC_HaltA();

  // Stop encryption on PCD
  rfid1.PCD_StopCrypto1();
   
}
/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}
MFRC522 rfid(SS_1_PIN, RST_PIN); // Instance of the class
MFRC522 rfid1(SS_2_PIN, RST_PIN); // Instance of the class

You have TWO instances. Number BOTH of them, so you don't look stupid.

Where is the link?

What kind of link do you need?

stangists: What kind of link do you need?

You still need to post a link (NOT just a URL) to the hardware you are trying to use.

http://playground.arduino.cc/Learning/MFRC522

Here is all the information about the RFID reader I'm using.

The library sets the chip select pin high or low when it starts reading from, or writing to, the chip on the reader/writer.

The chip appears to have a FIFO buffer that will store data that it scans from a card, even when the Arduino is not actively talking to the chip.

So, it would appear that you could simply create a for loop, and iterate over an array of instances, seeing if any of them have data.

There is even a ReadUidMultiReader example that shows how to use two readers. It seems like it could be extended to more than 2.

Have you tried that example?

I got another Arduino Uno, and with that the code worked, and also worked the ReadUidMultiReader example code. It worked perfect with 2 and 3 RFID readers, but when I connected 4, then only two sent the information to the Arduino and one of them sent with a delay. But two of the remaining RFID readers did not react.

#include 
#include 

#define SS_1_PIN 2
#define SS_2_PIN 8
#define SS_3_PIN 7
#define SS_4_PIN 4
#define RST_PIN 9

MFRC522 rfid(SS_1_PIN, RST_PIN); // Instance of the class
MFRC522 rfid1(SS_2_PIN, RST_PIN); // Instance of the class
MFRC522 rfid2(SS_3_PIN, RST_PIN); // Instance of the class
MFRC522 rfid3(SS_4_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 
// Init array that will store new NUID 
byte nuidPICC[4];
void setup() { 
  Serial.begin(115200);
 SPI.begin(); // Init SPI bus
 rfid.PCD_Init(); // Init MFRC522 
 rfid1.PCD_Init(); // Init MFRC522
 rfid2.PCD_Init(); // Init MFRC522
 rfid3.PCD_Init(); // Init MFRC522
}

void loop() {
RFID();
RFID1(); 
RFID2();
RFID3(); 
     



}

//===========================================

void RFID()
{
  //Serial.println("pirmais");

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  } 
  // Look for new 1 cards
  if ( ! rfid.PICC_IsNewCardPresent())  
  return;

    // Verify if the NUID has been readed
  if (  !rfid.PICC_ReadCardSerial())
  return;

  // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i]; 
    }
    
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();

  // Halt PICC
  rfid.PICC_HaltA();


  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

//==============================================
void RFID1()
{
   for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  
 if ( ! rfid1.PICC_IsNewCardPresent())
 return; 
     
 // Verify if the NUID has been readed
 if ( ! rfid1.PICC_ReadCardSerial())
     return;
  
  // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid1.uid.uidByte[i];
    }
    Serial.print(F("In dec: "));
    printDec(rfid1.uid.uidByte, rfid1.uid.size);
    Serial.println();

  // Halt PICC
  rfid1.PICC_HaltA();

  // Stop encryption on PCD
  rfid1.PCD_StopCrypto1();
   
}

//=============================================

void RFID2()
{
   for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  
 if ( ! rfid2.PICC_IsNewCardPresent())
 return; 
     
 // Verify if the NUID has been readed
 if ( ! rfid2.PICC_ReadCardSerial())
     return;
  
  // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid2.uid.uidByte[i];
    }
    Serial.print(F("In dec: "));
    printDec(rfid2.uid.uidByte, rfid2.uid.size);
    Serial.println();

  // Halt PICC
  rfid2.PICC_HaltA();

  // Stop encryption on PCD
  rfid2.PCD_StopCrypto1();
   
}

//=============================================

void RFID3()
{
   for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  
 if ( ! rfid3.PICC_IsNewCardPresent())
 return; 
     
 // Verify if the NUID has been readed
 if ( ! rfid3.PICC_ReadCardSerial())
     return;
  
  // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid3.uid.uidByte[i];
    }
    Serial.print(F("In dec: "));
    printDec(rfid3.uid.uidByte, rfid3.uid.size);
    Serial.println();

  // Halt PICC
  rfid3.PICC_HaltA();

  // Stop encryption on PCD
  rfid3.PCD_StopCrypto1();
   
}
/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

I also have the same problem..!!

I also have the same problem..!! I need to connect 4 MFRC522 readers in Arduino Mega but I'm only using 2 readers to find the correct and make it work before I use the other 2.. I've tried the code above of stangists but it didn't work..I also tried the ReadUidMultiReader example code but only one reader is working.. Both Readers are working individually but only one when I connect them.. I shared the SCK, MISO, MOSI, RST, and have different SS pins.. Thank you in advance

nel_buen:
I also have the same problem…!!
I need to connect 4 MFRC522 readers in Arduino Mega but I’m only using 2 readers to find the correct and make it work before I use the other 2… I’ve tried the code above of stangists but it didn’t work…I also tried the ReadUidMultiReader example code but only one reader is working…
Both Readers are working individually but only one when I connect them… I shared the SCK, MISO, MOSI, RST, and have different SS pins…
Thank you in advance

Did you checked that you have enough power for 4 readers?

One RFID reader can take up to 30 mA, so in total could be 120mA.

byte ssPins[] = {SS_1_PIN, SS_2_PIN};

I have connected two RFID successfully but I do not what is "byte". Can Anyone let me know what's that mean?

A ‘byte’ is a variable type just like char or int or …

A variable of type ‘byte’ is 8 bits in size and can contain 256 possible values from 0 to 255.

I have problems using two rfid rc522 with one arduino mega, to make a door access system there that can help my problem in the program?

https://youtu.be/LKSD05IwodI Multiple RFID

i have face the same problem, it take me some time to troubleshoot the problem. i am using low cost RFID rc-522 module cost around 4 USD 1st i have use library mfrc522 example read multi reader, successfully upload to arduino(in this case i use arduino nano) when i open serial monitor warning appear "Communication failure, is the MFRC522 properly connected?" for both reader the warning dose not appear if just using 1 reader then i figure out the problem appear when i connect miso pin to the 2nd reader then i figure out from google that we need to put pull up resistor to miso pin the recommended resistor value is 4.7k but i just have 10k , 330R and 220R

i ve successfully run the program with adding pull-up resistor 220R or 330R connected to 5V

hope this post will help some body

Pude utilizar 2 módulos RFID el soft ReadUidMultiReader de las librerias MRFC522 agregando dos diodos zener de 3.1V en cada Pin MISO. Espero que le sea útil a alguien!! Saludos.