MFRC522 Issue

Hi,

i bought a MFRC522 and use the rfid-library to control it with an Arduino Nano.

My sketch:

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

//RFID-Reader
#define RFID_RST_PIN 9
#define RFID_SS_PIN 10
MFRC522 nfc(RFID_SS_PIN, RFID_RST_PIN);
MFRC522::MIFARE_Key key;  //Schlüssel für Schreiben/Lesen von Tag

bool rfid_da;  //=1: Tag am Lesegerät, =0: kein Tag am Lesegerät
bool rfid_neu = 1;  //=1: ID noch nicht gesendet, =0: ID wurde bereits gesendet


void setup()
{
  Serial.begin(115200);
  SPI.begin();
  nfc.PCD_Init();
  
  for (int i = 0; i < 6; i++) //Schlüssel ist 6 Byte lang
    key.keyByte[i] = 0xFF; //Standartschlüssel FF FF FF FF FF FF

  Serial.println("Setup beendet");
}  //setup ende


void loop()
{
  long zeit = millis();  //in ms
  zeit /= 1000;  //in s
  
  checkRFID();

  if (rfid_da && rfid_neu)  //neues Tag am Leser
  {
    Serial.print(zeit/60);Serial.print(" min ");Serial.print(zeit%60);Serial.print(" sec \t");Serial.println("Tag da");
    rfid_neu = 0;
  }
  if ( ! rfid_da && ! rfid_neu)  //Tag von Leser entfernt
  {
    Serial.print(zeit/60);Serial.print(" min ");Serial.print(zeit%60);Serial.print(" sec \t");Serial.println("Tag weg");
    rfid_neu = 1;
  }
}  //loop Ende



void checkRFID()  //schaut nach, ob ein Tag in Reichweite ist
{
  bool check[2];
  for (int i = 0; i < 2; i++)
    check[i] = nfc.PICC_IsNewCardPresent();
  if (check[0] == 0 && check[1] == 0) //kein Tag am Leser
  {
    rfid_da = 0;
  }
  else  //Tag am Leser
  {
    rfid_da = 1;
  }

//  nfc.PCD_AntennaOff();  //Antenne des RFID-Readers ausschalten
//  nfc.PCD_AntennaOn();  //Antenne des RFID-Readers anschalten

//  nfc.PCD_AntennaOff();  //Antenne des RFID-Readers ausschalten
//  nfc.PCD_AntennaOn();  //Antenne des RFID-Readers anschalten
}  //checkRFID Ende

With the function checkRFID i look if a card is at the reader or not. I found out that PICC_IsNewCardPresent() permanent returns 0 if no card is present and if the return switches between 0 and 1 there is a card present. So i save in the variable rfid_da, if a card is present (=1) or not (=0).

My problem: this doesn't work for a long time. If i place a card on the reader i get the message that the card is present. After some time (somtimes 1/2 minute, sometimes 3 minutes, sometimes 10 minutes) I got the message that no card is present and in the next program run the card is present again, but the card was placed the whole time on the reader.

Is there anyone understanding me and my problem :smiley:
And is there anyone knowing a reason and a solution?

greetings markus

  for (int i = 0; i < 2; i++)
    check[i] = nfc.PICC_IsNewCardPresent();

How long is this loop going to take to execute?

  if (check[0] == 0 && check[1] == 0) //kein Tag am Leser

What is the likelihood that check[0] and check[1] will have different values?

Most RFID readers don't deal well with telling you whether a card is still present. They react with changes in the magnetic field when a card moves. Stationary cards do not generate any magnetic changes, so most readers can not tell whether a card is still there, or not.