← Back to the chapter · Previous detail · Next detail →

Technical detail · NFC Tag Programming

PN532 reader sketch and wiring review

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc(pn532_i2c);

void setup() {
  Serial.begin(115200);
  nfc.begin();
  Serial.println("Hold a writable NDEF tag near the reader");
}

void loop() {
  if (!nfc.tagPresent()) {
    delay(250);
    return;
  }

  NfcTag tag = nfc.read();
  Serial.print("UID: ");
  Serial.println(tag.getUidString());

  if (!tag.hasNdefMessage()) {
    Serial.println("No NDEF message found");
    return;
  }

  NdefMessage message = tag.getNdefMessage();
  Serial.print("Records: ");
  Serial.println(message.getRecordCount());
}

Set the PN532 board to the interface used by the sketch: I2C, SPI, or UART. Confirm voltage compatibility for the specific breakout board before connecting it to a microcontroller. Keep SDA/SCL, SPI chip select, or UART pins documented in the lab record. Test one known-good formatted tag before debugging application code.

This sketch reads the UID and NDEF record count; it does not write or verify a newly programmed tag.

← Back to the chapter · Previous detail · Next detail →