[RFID] mfrc522 Stops Reading Cards After 1 Hour

Arduino seems to run fine 24/7.
Only RFID stops reading the cards after a few hours.
But whenever I restart the Arduino RFID card always works again.

I have read many other articles about memory heap and periodically resetting RFID to tackle this problem but as an inexperienced Arduino user I'm very confused.

Here is my code:

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN); 

const String allowedID[] = {"11 C8 AE C3", "91 CB D2 1G"};



void setup() { 
  SPI.begin(); // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522 
  Serial.begin(9600);
  Serial.println(F("Scan Your Card"));
}

void loop() 
{
  scanFob(); 
  delay(200);
}

void scanFob(){
  // Look for new cards
  if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) return;
  
  String content= "";
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
     content.toUpperCase();
  }
  Serial.println("UID tag :" + content);
  
  if (content.substring(1) == allowedID[0] || content.substring(1) == allowedID[1]){
    Serial.println("Authorized access");
  }
  else{
    Serial.println("Access denied"); 
  }
}

You are using String objects which are a common cause of heap fragmentation. Plain old C strings (char arrays) do not have this problem.

Grab a free memory function (Adafruit has one) and you can use it to see if you have a leak.

sure, but here everything is self contained in a function as local variables, so all memory gets freed up.

@nanostray2007

➜ you are missing a mfrc522.PICC_HaltA();

What is 1G in Hexadecimal ? :scream: :innocent:

There is no need for a String or cStrings, you can just compare bytes using memcmp() since the UID is stored as a byte array.

Try this (typed here, fully untested. Note : switch the IDE monitor to 115200 bauds)

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);

const byte UIDSize = 4;

const byte allowedID[][UIDSize] = {
  {0x11, 0xC8, 0xAE, 0xC3},
  {0x91, 0xCB, 0xD2, 0x1F}
};

const byte allowedIDCount = sizeof allowedID / sizeof allowedID[0];

void printlnUID(byte *uid, byte uidSize) {
  for (byte i = 0; i < uidSize; i++) {
    Serial.print(uid[i] < 0x10 ? " 0" : " ");
    Serial.print(uid[i], HEX);
  }
  Serial.println();
}

byte checkCard() {
  byte index = 0;

  if (! mfrc522.PICC_IsNewCardPresent())  return allowedIDCount;
  if (! mfrc522.PICC_ReadCardSerial())    return allowedIDCount;

  // UID is stored in mfrc522.uid.uidByte and its size is in mfrc522.uid.size
  Serial.print(F("UID tag :"));  printlnUID(mfrc522.uid.uidByte, mfrc522.uid.size);

  for (index = 0; index < allowedIDCount; index++) {
    if ((mfrc522.uid.size == UIDSize) && (memcmp(mfrc522.uid.uidByte, allowedID[index], UIDSize))) break;
  }

  if (index >= allowedIDCount) {
    Serial.println("Access denied");
  } else {
    Serial.println("Authorized access");
  }
  mfrc522.PICC_HaltA();
  return index;
}

void setup() {
  SPI.begin();                                // Init SPI bus
  mfrc522.PCD_Init();                         // Init MFRC522
  Serial.begin(115200);                       // No reason to go slow at 9600 bauds...
  Serial.println(F("Scan Your Card"));
}

void loop() {
  checkCard();
}

Possibly. There's several temporary Strings being created though, so I think I'd want to prove it's not leaking.

I'm unaware of memory leaks in the String class. The compiler / String class instantiates and delete automagically the temp Strings.

There are no nested calls and limited use of the '+' operator

Whilst inside the scanFob function you definitely have stuff happening to the heap, once you leave the function and go back to the loop(), the memory should be pristine again.

Well, I wasn't convinced so I tweaked that code so that I could test it without the RFID hardware. You were quite right - the heap is stable :grinning_face_with_smiling_eyes:

Trust but verify is my motto too :slight_smile:

Wow, I didn't expect such detailed responses in merely few hours!

I will try them out.

Thanks a lot for the help!

@nanostray2007 Installation and Troubleshooting is for Problems with the Arduino itself NOT your project. It says so in the description of the section. Therefore I have moved your post here.

You may want to read this before you proceed:-
how to get the best out of this forum

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