0

I am trying to detect a RFID card using the NFC-PN532 module and Adafruit_PN532 library.

Initialization code:-

#include <Adafruit_PN532.h>
#include <Wire.h> 

#define SCK  (13)
#define MISO (12)
#define MOSI (11)
#define SS   (10)
Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);

void setup()
{  
  Serial.begin(9600);
  Serial.println("Initializing please wait.......");
  delay(3000);
  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) 
  {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }

  Serial.print("Device Found PN5 Chip"); 
  Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware version > "); 
  Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.println((versiondata>>8) & 0xFF, DEC);
  nfc.SAMConfig(); //Set to read RFID tags
  Serial.println("Waiting for RFID Card ...");
}

Main loop:-

void loop() 
{
  uint8_t success = 0;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  uint8_t uidLength;
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  if (success) 
  {
      Serial.println("Card Detected!");
  }
  else
  {
      Serial.println("Card Not Detected or Removed!");
  }
}

Whenever i place a valid card on the PN532, Card Detected! is printed on the serial monitor.

However, when i remove the card, Card Not Detected or Removed! does not get printed.

How can i make the program to loop in such a way that it can detect when the card is present/removed?

  • once you fix this (not sure why it is behaving this way) you wont detect when a card is removed ... you'll detect when a card is not present ... there's a difference :p – Jaromanda X Mar 19 '19 at 12:53
  • @JaromandaX yes, you are right. But then how do i modify the code to detect "card inserted"and "card removed"? – Sandrocottus Mar 19 '19 at 12:56
  • you'd achieve that with code that only prints card removed if this is the first loop after card was present, ... try adding nfc.setPassiveActivationRetries(0x10); to your setup just before nfc.SAMConfig(); - this should stop the code waiting for a card :p – Jaromanda X Mar 19 '19 at 13:01
3

The default behaviour of readPassiveTargetID is to wait "forever" for a card - which is why your code only sees when there is a card present

So, in setup() add nfc.setPassiveActivationRetries(0x10); as follows - comments came from my own code, but I'm sure they were originally in some example or other :p

// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0x10);

// configure board to read RFID tags
nfc.SAMConfig();

As for "detecting" removal

void loop() 
{
  static bool wasSuccess = false;
  uint8_t success = 0;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  uint8_t uidLength;
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  if (success) 
  {
      wasSuccess = true;
      Serial.println("Card Detected!");
  }
  else if (wasSuccess)
  {
      wasSuccess = false;
      Serial.println("Card Removed!");
  }
  else
  {
      // you may want to remove this, it will flood the output :p
      Serial.println("Card Not Detected!");
  }
}
| improve this answer | |
  • @Patrick - including the detection of remove? I ask, because I've been meaning to write some code for my PN532 :p – Jaromanda X Mar 19 '19 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.