New card present command for pn532

hello everyone,
i wanted to use pn532 rf reader in project. previously i was using rc522 reader in project but due to some limitation of rc522 i switch over to pn532 rfid reader. but now i am facing continuous rfid reading issue in my program. previously in rc522 reader there was a command for new id reading checking like {
// Look for new cards
if (!mfrc522.PICC_IsNewCardPresent())
return;

// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial())
return;

but in pn 532 i didnt found any command to search new card. please help me to find command to for pn532 to check new card or help to stop continual scanning of pn532 reader until new card present on reader...
following is my program.

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

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK (12)
#define PN532_MOSI (11)
#define PN532_SS (10)
#define PN532_MISO (9)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a software SPI connection (recommended):
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


void setup(void) {
  Serial.begin(115200);
  while (!Serial) delay(10); // for Leonardo/Micro/Zero

  Serial.println("Hello!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

  Serial.println("Waiting for an ISO14443A Card ...");
}

void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an NTAG203 card.  When one is found 'uid' will be populated with
  // the UID, and uidLength will indicate the size of the UUID (normally 7)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");

    if (uidLength == 7)
    {
      uint8_t data[32];

      // We probably have an NTAG2xx card (though it could be Ultralight as well)
      Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");

      // NTAG2x3 cards have 39*4 bytes of user pages (156 user bytes),
      // starting at page 4 ... larger cards just add pages to the end of
      // this range:

      // See: http://www.nxp.com/documents/short_data_sheet/NTAG203_SDS.pdf

      // TAG Type       PAGES   USER START    USER STOP
      // --------       -----   ----------    ---------
      // NTAG 203       42      4             39
      // NTAG 213       45      4             39
      // NTAG 215       135     4             129
      // NTAG 216       231     4             225

      for (uint8_t i = 0; i < 42; i++)
      {
        success = nfc.ntag2xx_ReadPage(i, data);

        // Display the current page number
        Serial.print("PAGE ");
        if (i < 10)
        {
          Serial.print("0");
          Serial.print(i);
        }
        else
        {
          Serial.print(i);
        }
        Serial.print(": ");

        // Display the results, depending on 'success'
        if (success)
        {
          // Dump the page data
          nfc.PrintHexChar(data, 4);
        }
        else
        {
          Serial.println("Unable to read the requested page!");
        }
      }
    }
    else
    {
      Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");
    }

  
  }
}`

if success gets the value card details will be printed, But i didn't understand the problem that you are facing

In above example rf id tag continuous read until i remove card. But my requirement is only one time reading of rfid tag.

i just added a checking condition hope it helps

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

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK (12)
#define PN532_MOSI (11)
#define PN532_SS (10)
#define PN532_MISO (9)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

boolean cardPresent = false;
// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a software SPI connection (recommended):
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


void setup(void) {
  Serial.begin(115200);
  while (!Serial) delay(10);  // for Leonardo/Micro/Zero

  Serial.println("Hello!");

  nfc.begin();

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

  Serial.print("Found chip PN5");
  Serial.println((versiondata >> 24) & 0xFF, HEX);
  Serial.print("Firmware ver. ");
  Serial.print((versiondata >> 16) & 0xFF, DEC);
  Serial.print('.');
  Serial.println((versiondata >> 8) & 0xFF, DEC);

  Serial.println("Waiting for an ISO14443A Card ...");
}

void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an NTAG203 card.  When one is found 'uid' will be populated with
  // the UID, and uidLength will indicate the size of the UUID (normally 7)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  if (success) {
    if (! cardPresent) {
      // Display some basic information about the card
      Serial.println("Found an ISO14443A card");
      Serial.print("  UID Length: ");
      Serial.print(uidLength, DEC);
      Serial.println(" bytes");
      Serial.print("  UID Value: ");
      nfc.PrintHex(uid, uidLength);
      Serial.println("");
      cardPresent = true;

      if (uidLength == 7) {
        uint8_t data[32];

        // We probably have an NTAG2xx card (though it could be Ultralight as well)
        Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");

        // NTAG2x3 cards have 39*4 bytes of user pages (156 user bytes),
        // starting at page 4 ... larger cards just add pages to the end of
        // this range:

        // See: http://www.nxp.com/documents/short_data_sheet/NTAG203_SDS.pdf

        // TAG Type       PAGES   USER START    USER STOP
        // --------       -----   ----------    ---------
        // NTAG 203       42      4             39
        // NTAG 213       45      4             39
        // NTAG 215       135     4             129
        // NTAG 216       231     4             225

        for (uint8_t i = 0; i < 42; i++) {
          success = nfc.ntag2xx_ReadPage(i, data);

          // Display the current page number
          Serial.print("PAGE ");
          if (i < 10) {
            Serial.print("0");
            Serial.print(i);
          } else {
            Serial.print(i);
          }
          Serial.print(": ");

          // Display the results, depending on 'success'
          if (success) {
            // Dump the page data
            nfc.PrintHexChar(data, 4);
          } else {
            Serial.println("Unable to read the requested page!");
          }
        }
      } else {

        Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");
      }
    }

  } else {
    cardPresent = false;
  }
}
1 Like

Just not read it again until rfid tag not changed

thank you for replying... i checked this code...by this modification rfid tag scan only one time but when we again scan tag at that time its not getting read,,,,

Do you mean by scanning same tag or different tag that not getting reading

Both same and different card not able to read

if only one card is reading then if(success) part is not getting false i think .i ll give a code will you share the output what does it shows.

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

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK (12)
#define PN532_MOSI (11)
#define PN532_SS (10)
#define PN532_MISO (9)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

boolean cardPresent = false;
// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a software SPI connection (recommended):
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


void setup(void) {
  Serial.begin(115200);
  while (!Serial) delay(10);  // for Leonardo/Micro/Zero

  Serial.println("Hello!");

  nfc.begin();

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

  Serial.print("Found chip PN5");
  Serial.println((versiondata >> 24) & 0xFF, HEX);
  Serial.print("Firmware ver. ");
  Serial.print((versiondata >> 16) & 0xFF, DEC);
  Serial.print('.');
  Serial.println((versiondata >> 8) & 0xFF, DEC);

  Serial.println("Waiting for an ISO14443A Card ...");
}

void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an NTAG203 card.  When one is found 'uid' will be populated with
  // the UID, and uidLength will indicate the size of the UUID (normally 7)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  if (success) {
     Serial.print(" Card Detected ");
    if (! cardPresent) {
      // Display some basic information about the card
      Serial.println("Found an ISO14443A card");
      Serial.print("  UID Length: ");
      Serial.print(uidLength, DEC);
      Serial.println(" bytes");
      Serial.print("  UID Value: ");
      nfc.PrintHex(uid, uidLength);
      Serial.println("");
      cardPresent = true;

      if (uidLength == 7) {
        uint8_t data[32];

        // We probably have an NTAG2xx card (though it could be Ultralight as well)
        Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");

        // NTAG2x3 cards have 39*4 bytes of user pages (156 user bytes),
        // starting at page 4 ... larger cards just add pages to the end of
        // this range:

        // See: http://www.nxp.com/documents/short_data_sheet/NTAG203_SDS.pdf

        // TAG Type       PAGES   USER START    USER STOP
        // --------       -----   ----------    ---------
        // NTAG 203       42      4             39
        // NTAG 213       45      4             39
        // NTAG 215       135     4             129
        // NTAG 216       231     4             225

        for (uint8_t i = 0; i < 42; i++) {
          success = nfc.ntag2xx_ReadPage(i, data);

          // Display the current page number
          Serial.print("PAGE ");
          if (i < 10) {
            Serial.print("0");
            Serial.print(i);
          } else {
            Serial.print(i);
          }
          Serial.print(": ");

          // Display the results, depending on 'success'
          if (success) {
            // Dump the page data
            nfc.PrintHexChar(data, 4);
          } else {
            Serial.println("Unable to read the requested page!");
          }
        }
      } else {

        Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");
      }
    }

  } else {
     Serial.print(" Card Removed ");
    cardPresent = false;
  }
}

thank you for replying... above program works same as before but only difference is its show "card detected " continuously when card present and read card only one time after downloading program. in next tag scanning operation its doesn't read tag but shows "card detected continuously...

Can you show the output after uploading the code and don't place the card tell me if they are showing card detected or not. Tell me the output before and after placing the card

add

      // get the reader to exit its continuous loop when a card wasn't present
      nfc.setPassiveActivationRetries(0x02); // 1 or 2 tries should be enough

in the setup after you have initialized the NFC reader.

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