3 Replies Latest reply on May 30, 2020 9:46 AM by butcher97

    Using multiple MFRC522 RFID readers on one Arduino

    tddj81

      I'm still pretty new to this, but I'm trying to connect up to 12 MFRC522 RFID readers to my arduino uno. They're the cheap under $5 ones from ebay. I can get one to work using the miguelbalboa/rfid library but I can't seem to get a second one working. I've attached the code I'm trying to use. Both readers are sharing the same MOSI, MISO, and SCK pins. The library I'm trying to use is found at https://github.com/miguelbalboa/rfid

       

      MOSI pin 11

      MISO pin12

      SCK  pin 13

       

      #include <SPI.h>

      #include <MFRC522.h>

       

      #define RST_PIN 7 //

      #define RST_PIN2  8    // Configurable, see typical pin layout above

      #define SS_PIN 9 //

      #define SS_PIN2   10    // Configurable, see typical pin layout above

       

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

      MFRC522 mfrc522_2(SS_PIN2, RST_PIN2);   // Create MFRC522 instance.

       

       

      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

      mfrc522_2.PCD_Init(); // Init MFRC522

      ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details

      Serial.println(F("Scan PICC to see UID, type, and data blocks..."));

      }

       

      void loop() {

      // Look for new cards

      if ( ! mfrc522.PICC_IsNewCardPresent()) {

      return;

      }

       

      // Select one of the cards

      if ( ! mfrc522.PICC_ReadCardSerial()) {

      return;

      }

       

       

      // Dump debug info about the card; PICC_HaltA() is automatically called

      mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

       

        delay(1000);

       

        // Look for new cards

        if ( ! mfrc522_2.PICC_IsNewCardPresent()) {

          return;

        }

       

        // Select one of the cards

        if ( ! mfrc522_2.PICC_ReadCardSerial()) {

          return;

        }

       

        // Dump debug info about the card; PICC_HaltA() is automatically called

        mfrc522_2.PICC_DumpToSerial(&(mfrc522.uid));

      }

       

      void ShowReaderDetails() {

      // Get the MFRC522 software version

      byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);

      Serial.print(F("MFRC522 Software Version: 0x"));

      Serial.print(v, HEX);

      if (v == 0x91)

      Serial.print(F(" = v1.0"));

      else if (v == 0x92)

      Serial.print(F(" = v2.0"));

      else

      Serial.print(F(" (unknown)"));

      Serial.println("");

      // When 0x00 or 0xFF is returned, communication probably failed

      if ((v == 0x00) || (v == 0xFF)) {

      Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));

      }

      byte w = mfrc522_2.PCD_ReadRegister(mfrc522_2.VersionReg);

      Serial.print(F("MFRC522_2 Software Version: 0x"));

      Serial.print(w, HEX);

      if (w == 0x91)

          Serial.print(F(" = v1.0"));

      else if (w == 0x92)

          Serial.print(F(" = v2.0"));

      else

          Serial.print(F(" (unknown)"));

      Serial.println("");

      // When 0x00 or 0xFF is returned, communication probably failed

      if ((w == 0x00) || (v == 0xFF)) {

         Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));

        }

      }

       

      I'm not opposed to using more than one arduino, because I'm going to have it feed information to a raspberry pi. I would prefer to use as few as possible.

       

      Thanks in advance.

        • Re: Using multiple MFRC522 RFID readers on one Arduino
          jeroenb42

          Hi Terry, did you ever get this working? And if so, how? I'm trying the same thing but somehow the SPI bus seems to freeze as soon as I attach the 2nd RFID reader.

          1 of 1 people found this helpful
          • Re: Using multiple MFRC522 RFID readers on one Arduino

            I know this is very late, but i am hoping this will help someone at a later date. The issue with these MFRC522 boards is that they are not very well designed to work with other boards on the same SPI bus. In the datasheet, it has no references to the state of the MISO line when not selected. When testing myself, the MISO is driven when not in use instead of being in a high-impedance state. The simplest solution to have these working on 1 SPI bus is to use a tri-state buffer on the MISO line with the active line of the tri-state buffer connected to the CS line. This will ne the CS line inverted to when the chip is not in use the CS input to the tr-state buffer is low, and vice versa. This will ensure the MISO is high impedance when not selected and connected to the MISO bus when selected.

            To test this is the issue:

                 Connect all the NFC boards to the controller

                 Test to ensure there is no (viable) data being receiver over the MISO

                 remove all but 2 boards from the MISO and see the same result as above

                 Remove one of the connections to the MISO and see if the reader is now sending data over the MISO           correctly

                 If you are reading the data correctly, the issue is with the MISO

             

            Another option is to create an SPI object for each reader in your code and only change the MISO lines for each, keeping the same MOSI, SLCK and CS lines. Then selecting the boards to read from will change the MISO input and read from the correct board. This is useful for boards that can have any pins as SPI pins, but does not work for boards that requires specific pins for its SPI lines, e.g. STM32 series.

             

            Hopfully this helps anyone struggling with these NFC boards trying to communicate over 1 SPI bus

            2 of 2 people found this helpful