MFRC522 (RFID) Intermittently Stops Working

I use an MFRC522 RFID scanner (example) with the Argon in my IoT class, and the sensor will work for a few scans, and then suddenly stops scanning (i.e. waving a keycard has no effect). I've verified that the Argon itself isn't locked up, and this problem recurs eventually even with different sensors. Some combination of reset, re-flash, or safe mode + re-flash usually gets it working again until it inevitably happens again.

I'm guessing it has something to do with the library, but I'm not quite sure what. I'm posting an example of the code, but the problem of the sensor stopping happens with libraries examples too so I don't think the problem is specific to my code below.

Has anyone else run into this or identified a solution?

Library
ARGON-RFID-MFRC522

Code

#include "Particle.h"

SYSTEM_MODE(AUTOMATIC);
SYSTEM_THREAD(ENABLED);

SerialLogHandler logHandler(LOG_LEVEL_INFO);

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

constexpr uint8_t RST_PIN = A5;  // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = A4;   // Configurable, see typical pin layout above

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

const String card1 = "0E 8B 8E 6A";  
const String card2 = "4E B3 01 BF";  

unsigned long prevMillis = 0;
unsigned long scanDelay = 2000;  // time between scans

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.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card
                                        // Reader details
    Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
    String scannedCard = "";

    unsigned long curMillis = millis();
    if (curMillis - prevMillis > scanDelay) {
        prevMillis = curMillis;
        if (mfrc522.PICC_IsNewCardPresent() == true) {
            if (mfrc522.PICC_ReadCardSerial() == true) {
                for (byte i = 0; i < mfrc522.uid.size; i++) {
                    if (mfrc522.uid.uidByte[i] < 0x10) {
                        scannedCard = scannedCard + " 0";
                    } else {
                        scannedCard = scannedCard + " ";
                    }
                    scannedCard =
                        scannedCard + String(mfrc522.uid.uidByte[i], HEX);
                }
                scannedCard.toUpperCase();
                scannedCard.trim();
                if (scannedCard == card1) {
                    Serial.println("Card 1 scanned");
                } else if (scannedCard == card2) {
                    Serial.println("Card 2 scanned");
                } else {
                    Serial.println("Card not recognized");
                }
            }
        }
    }
}

Hi @rob7 -

If you want you can have a look at THIS EXAMPLE

It was one of my very first Particle devices built, so I am pretty sure there are better ways to do it, but these dives are still going strong each reading a couple of 1000 scans every day. You should be able to see the library I used in the code as well. The application is quite similar to yours.

Hope this helps.
Regards.

Thanks for that suggestion. I'll check that once the semester is done.

1 Like

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