Which Library?

In the past there were two separate Arduino libraries for using the Adafruit NFC boards.  One library supported the breakout over a SPI connection, and the other library supported the breakout or shield over an I2C connection.  However both of these libraries have been merged into a single Arduino library, Adafruit-PN532.

The Adafruit PN532 library has the ability to read MiFare cards, including the hard-coded ID numbers, as well as authenticate and read/write EEPROM chunks.  It can work with both the breakout and shield using either a SPI or I2C connection.

Library Installation

Download the Adafruit PN532 library from github. Uncompress the folder and rename the folder Adafruit_PN532. Inside the folder you should see the Adafruit_PN532.cpp and Adafruit_PN532.h files. Install the Adafruit_PN532 library foler by placing it in your arduinosketchfolder/libraries folder. You may have to create the libraries subfolder if this is your first library. You can read more about installing libraries in our tutorial.

Restart the Arduino IDE. You should now be able to select File > Examples > Adafruit_PN532 > readMifare sketch.

If you're using the NFC breakout with a SPI connection that uses the wiring shown on previous pages you can immediately upload the sketch to the Arduino and skip down to the Testing MiFare section.

If you're using the NFC shield, or are using the breakout with an I2C connection then you must make a small change to configure the example for I2C.  Scroll down to these lines near the top of the sketch:

// 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 SPI connection:
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);

Change them so the second line is uncommented and the first line is commented.  This will configure the sketch to make the library use I2C for communication with the NFC shield or breakout.  The modified code should look like:

// 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 SPI connection:
//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);

Then upload the example to the Arduino and continue on.  Note that you need to make a similar change to pick the interface for any other NFC example from the library.

Testing MiFare

In the serial monitor, you should see that it found the PN532 chip. Then you can place your tag nearby and it will display the 4 byte ID code (this one is 0xAE 0x4C 0xF0 0x6C) and then the integer version of all four bytes together. You can use this number to identify each card. Recently NXP made so many cards that they actually ran through all 4 Bytes (2^32) so the number is not guaranteed to be absolutely unique. However, the chances are extremely slim you will have two cards with the same ID so as long as you aren't using these cards for anything terribly important (like money transfer) its fine to use the number as a unique identifier

This guide was first published on Dec 30, 2012. It was last updated on Mar 18, 2024.

This page (Arduino Library) was last updated on Nov 29, 2012.

Text editor powered by tinymce.