Arduino library 路 Communication

Adafruit PN532: read NFC cards and tags over I2C

The PN532 is NXP鈥檚 NFC controller, found on the red and blue "PN532 NFC RFID module V3" boards and on Adafruit鈥檚 shield. It reads 13.56 MHz cards and tags: MIFARE Classic key fobs, NTAG213 stickers and many phones in card mode. Adafruit_PN532 talks to it over I2C, hardware SPI or software SPI, picked by which constructor you use.

Built in on Mokxi: #include <Adafruit_PN532.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit PN532 does

The usual sketch checks the chip with getFirmwareVersion(), calls SAMConfig() to put it in normal reader mode, then waits in readPassiveTargetID() for a card. That call fills a buffer with the card鈥檚 UID (4 bytes for MIFARE Classic, 7 for NTAG) and its length. From there, MIFARE Classic data blocks need an authenticate step with a key before each sector, while NTAG pages read without one.

Compared with the MFRC522 and its library, the PN532 costs a little more but speaks I2C with only two extra pins (IRQ and reset) and reads more card types. The example on this page wires the module to the Uno鈥檚 A4 and A5 with IRQ on pin 2 and reset on pin 3, then places a card on the reader after the sketch starts waiting. Card emulation, the HSU serial mode and the chip鈥檚 GPIO calls are not simulated.

Library Manager nameAdafruit PN532
AuthorAdafruit
Include#include <Adafruit_PN532.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesNFC reader, PN532

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_PN532.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit PN532" by Adafruit from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit PN532 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.3.4, so a sketch written here builds there too.

What to know in the simulator: Card emulation (AsTarget), the GPIO calls and the HSU (serial) interface are not here.

ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET)

The I2C constructor: the IRQ pin the chip pulls low when it has an answer, and its reset pin. Passing four pins instead makes it software SPI.

nfc.begin()

Starts the bus and resets the chip. It does not check the chip is there; getFirmwareVersion() does that.

nfc.getFirmwareVersion()

Returns a 32-bit word: the chip in the top byte (0x32 for a PN532), then the firmware version. A 0 means no answer, so check wiring and the mode switches.

nfc.SAMConfig()

Puts the chip in normal mode so it can read cards. Call it once after the version check.

nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)

Waits for a card and copies its UID into uid, the length into uidLength. Returns true when a card was read.

uint8_t uid[7];
uint8_t uidLength;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) {
  nfc.PrintHex(uid, uidLength);
}

nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya)

Unlocks the sector that holds block 4 with key A (the 0 argument). Required before reading or writing a MIFARE Classic block.

nfc.mifareclassic_ReadDataBlock(4, data)

Reads the 16 bytes of block 4 into data, after the sector has been authenticated.

nfc.ntag2xx_ReadPage(4, data)

Reads one 4-byte page of an NTAG213, 215 or 216 sticker. No key is needed.

Example: Read an NFC card

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino 路 Arduino Uno R3
#include <Wire.h>
#include <Adafruit_PN532.h>

#define PN532_IRQ   (2)
#define PN532_RESET (3)

Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

void setup() {
  Serial.begin(115200);
  Serial.println("Hello!");
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (!versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1);
  }
  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);
  nfc.SAMConfig();
  Serial.println("Waiting for an ISO14443A Card ...");
}

void loop() {
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  uint8_t uidLength;
  if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) {
    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);
    delay(1000);
  }
}

Wiring

  • Arduino Uno R3 2 to NFC reader, PN532 IRQ
  • Arduino Uno R3 3 to NFC reader, PN532 RST
  • Arduino Uno R3 A4 to NFC reader, PN532 SDA
  • Arduino Uno R3 A5 to NFC reader, PN532 SCL
  • Arduino Uno R3 5V to NFC reader, PN532 VCC
  • Arduino Uno R3 GND to NFC reader, PN532 GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

Didn't find PN53x board

getFirmwareVersion() got no answer. On the common red module, set the two mode switches to I2C (1 on, 2 off on most boards), check SDA and SCL, and power-cycle it after moving the switches.

The sketch stops at readPassiveTargetID() and never prints anything else

That call waits for a card. With no card it blocks, so other code in loop() never runs. Call nfc.setPassiveActivationRetries(0xFF) for a bounded wait, or pass a timeout in milliseconds as the last argument.

Authentication failed for a MIFARE block

The key is wrong for that sector, or the card is an NTAG, which has no keys. New MIFARE Classic cards use FF FF FF FF FF FF as key A. Check the UID length: 7 bytes means an NTAG, so read pages instead.

Questions and answers

Should I use the PN532 or the RC522?

The RC522 is cheaper and SPI only. The PN532 adds I2C, reads more card types and is easier to wire alongside other SPI parts, at a slightly higher price.

Projects that use Adafruit PN532

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try Adafruit PN532 without installing anything

Open the example, change the sketch, and keep your version in a free account.