Arduino Uno RFID MFRC522 Stops Detecting Cards

I have code written below that detects different RFID cards and outputs when they are placed over the RFID reader. It then prints out when the cards are removed. (Inspired by this tutorial) There are also some lights that are turned on and off with a mosfet when cards are placed and removed.

I'm having quite the issue though. Intermittently after a while the Arduino stops detecting new cards. It gets to "here-3" and then returns to the beginning of the loop because a card is not detected. If I press the reset button on the Arduino or execute the resetFunc it fixes the issue for a while.

I read online that the string class could be of issue so I had rewritten it with bytes for the UIDs and I still have the issue. I also heard that using jumper wires could be an issue so I soldering the wire's straight to the RFID.

If anyone could shed any light on this issue it would be much appreciated.

//https://highvoltages.co/tutorial/arduino-tutorial/arduino-mfrc522-tutorial-is-rfid-tag-present-or-removed/#Code-description-and-video-tutorial
#include <SPI.h>
#include <MFRC522.h>

#define IDENTIFIER 0
// SWORD  0
// AXE    1
// SPEAR  2
// HAMMER 3
// ARROW  4
// SHIELD 5

const byte rfidTags[6][10] = {
  {0x50, 0x74, 0x3A, 0xA5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, //sword
  {0x04, 0x60, 0xC1, 0xBA, 0xDF, 0x4C, 0x81, 0x00, 0x00, 0x00}, //axe
  {0xF0, 0xCB, 0xD1, 0xA5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, //spear
  {0x04, 0x4D, 0x37, 0xA2, 0x07, 0x4F, 0x80, 0x00, 0x00, 0x00}, //hammer
  {0x30, 0x55, 0xCC, 0xA4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, //arrow
  {0x04, 0xB1, 0xE9, 0xBA, 0xDF, 0x4C, 0x80, 0x00, 0x00, 0x00} //shield
};

const char stoneNames[6][7] = {"SWORD", "AXE", "SPEAR", "HAMMER", "ARROW", "SHIELD"};

const char resetChars[6] = {'w', 'x', 'p', 'h', 'a', 's'};

#define PIN_CORRECT_SIGNAL  3
#define PIN_LED_ON          4
#define MOSFET              6

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // 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
  delay(4);       // Optional delay. Some board do need more time after init to be ready, see Readme
  Serial.print("STARTING-");
  Serial.println(stoneNames[IDENTIFIER]);

  pinMode (PIN_LED_ON, OUTPUT);
  pinMode (PIN_CORRECT_SIGNAL, OUTPUT);
  pinMode (MOSFET, OUTPUT);

  digitalWrite(PIN_LED_ON, LOW);
  digitalWrite(PIN_CORRECT_SIGNAL, LOW);
  digitalWrite(MOSFET, LOW);
}

//uint8_t buf[10] = {};
MFRC522::Uid id;
//MFRC522::Uid id2;
uint8_t control = 0x00;

void(* resetFunc) (void) = 0;

void PrintHex(uint8_t *data, uint8_t length) {
  char tmp[16];
  for (int i = 0; i < length; i++) {
    sprintf(tmp, "0x%.2X", data[i]);
    Serial.print(tmp);
    Serial.print(" ");
  }
}

void cpid(MFRC522::Uid * id) {
  memset(id, 0, sizeof(MFRC522::Uid));
  memcpy(id->uidByte, mfrc522.uid.uidByte, mfrc522.uid.size);
  id->size = mfrc522.uid.size;
  id->sak = mfrc522.uid.sak;
}

void checkSerial() {
  while (Serial.available() > 0) {
    char receivedChar = Serial.read();
    if (receivedChar == resetChars[IDENTIFIER]) {
      resetFunc();
    }
  }
}

void loop() {
  Serial.println("here-1");
  checkSerial();
//  MFRC522::MIFARE_Key key;        // So I have no idea if these lines are used
//  for (byte i = 0; i < 6; i++) {  // I'm not really sure
//    key.keyByte[i] = 0xFF;        // but I'm leaving them
//  }                               // bc maybe the brilliant man on
//  MFRC522::StatusCode status;     // youtube knew what he was doing
  Serial.println("here-2");
  if (!mfrc522.PICC_IsNewCardPresent()) {
    Serial.println("here-3");
    return;
  }
  Serial.println("here-4");
  if (!mfrc522.PICC_ReadCardSerial()) {
    Serial.println("here-5");
    return;
  }
  Serial.println("here-6");
  cpid(&id);
  Serial.println("here-7");
  //  PrintHex(id.uidByte, id.size);

  Serial.println("here-8");
  for (int i = 0; i < 6; i++) {
    int memcmp_result = memcmp(id.uidByte, rfidTags[i], sizeof(mfrc522.uid.uidByte));
    if (memcmp_result == 0) { ////0 means the uid matches up http://www.cplusplus.com/reference/cstring/memcmp/
      if (i == IDENTIFIER) { // CORRECT STONE
        Serial.print(stoneNames[IDENTIFIER]);
        Serial.println("-CORRECT");

        digitalWrite(PIN_CORRECT_SIGNAL, HIGH);
        digitalWrite(PIN_LED_ON, HIGH);
        digitalWrite(MOSFET, HIGH);
      }
      else {
        Serial.print(stoneNames[IDENTIFIER]);
        Serial.println("-WRONG");
        Serial.print(stoneNames[i]);
        Serial.print("-NOT-");
        Serial.println(stoneNames[IDENTIFIER]);

        digitalWrite(PIN_CORRECT_SIGNAL, LOW);
        digitalWrite(PIN_LED_ON, HIGH);
        digitalWrite(MOSFET, HIGH);
      }
      Serial.println("here-9");
      break;
    }
  }
  Serial.println("here-10");

  while (true) { //I don't really understand why this works but it does
    control = 0;
    Serial.println("here-11");
    for (int i = 0; i < 3; i++) {
      if (!mfrc522.PICC_IsNewCardPresent()) {
        if (mfrc522.PICC_ReadCardSerial()) {
          control |= 0x16;
        }
        if (mfrc522.PICC_ReadCardSerial()) {
          control |= 0x16;
        }
        control += 0x1;
      }
      control += 0x4;
    }

    if (control == 13 || control == 14) {
      //card is still here
      Serial.println("here-12");
      checkSerial();
    } else {
      Serial.println("here-13");
      break;
    }
  }
  Serial.println("here-14");
  digitalWrite(PIN_LED_ON, LOW);
  digitalWrite(PIN_CORRECT_SIGNAL, LOW);
  digitalWrite(MOSFET, LOW);

  Serial.print(stoneNames[IDENTIFIER]);
  Serial.println("-REMOVED");

  Serial.println("here-15");
  mfrc522.PICC_HaltA();
  Serial.println("here-16");
  mfrc522.PCD_StopCrypto1();
  Serial.println("here-17");
  
}
1 Like

Yes RFID is a pain.
Here is some code I use (hacked from a project I am working on)
Basically it stops and restarts the card reader every time.
You need to add the SPI stuff to stop()/ restart() and do some other fix up coding
myRFID.cpp (13.9 KB)

1 Like

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