Skip to content

AnduinoNFC

Brian Carbonette edited this page Feb 27, 2017 · 1 revision

AnduinoNFC

The Anduino shield comes equipped with a PN532 NFC chip and uFL antenna header to connect an RFID antenna. This enables contactless communication at 13.56 MHz. The AnduinoNFC library supports passive tag reading of Mifare Classic and Ultralight tags. Reading NDEF encoded messages has been introduced via Don's NDEF library. Some stats at a glance...

  • Supports Mifare Classic and Ultralight Tags
  • NDEF messages (read, write, format)
  • NXP PN532 connected via SPI
  • 13.56 MHz RFID
  • Anduino shield has a built in pull up resistor connected to PN532 IRQ for easy hardware interrupt integration

Upon installing the library into your Arduino IDE you can quickly get started reading, writing and formatting cards via the example sketches available by navigating to File>>Examples>>AnduinoNFC. Some of the first NFC methods you'll run into..

In your sketch be sure to include the NFC Library by adding...

#include "AnduinoNFC.h"

to the top of your sketch as well as...

AnduinoNFC NFC = AnduinoNFC();

and call...

NFC.begin();

in your setup() to properly create your NFC object and initialize the chip.

There are example sketches which integrate AnduinoNFC with other Anduino libraries which help you to see how we can create complete solutions utilizing the different components available on the Anduino shield. Check out the keyCardAccess or nfcColorSelect sketches to see these in action.

begin()

  •  Initializes SPI and wakes up PN532.
    
  •  Attaches hardware interrupt so RFID card reads can happen asynchronously.
    
  •  This happens on PN532_IRQ_PIN see [AnduinoPins.h](https://github.com/andium/Anduino/blob/master/libraries/AnduinoLCD/src/AnduinoPins.h#L38).
    

packetReady()

  •  Checks if there is an RFID tagPresent() that can be read and that the PN532 hardware interrupt service routine has been taken signaling that there is RFID data to be processed.
    
  •  Utilizing the interrupt prevents us from polling for RFID card reads in our main loop and allows us to debounce our reads without introducing delays into our sketch.  If the RFID card was processed within the past 2 seconds, packetReady() will return false and your code will continue without servicing a duplicate read.
    
if (NFC.packetReady())
{
    NfcTag tag = NFC.read(); //read the RFID tag 
}

read()

  •  Attempts to read the RFID tag data and stores the tag information in an [NfcTag](https://github.com/don/NDEF#nfctag) object.
    
  •  This contains info like, UID, tag type, size or an NDEF message for NDEF formatted tags.
    
  •  Adafruit has some great documentation on NDEF(NFC Data Exchange Format) fields and data types. 
    
  •  To learn more you can also go straight to the source and download the full spec on the [NFC Forum site](http://members.nfc-forum.org/specs/spec_list/). 
    
NfcTag tag = NFC.read();
tag.print();  //print tag details to serial terminal

write(NdefMessage& ndefMessage)

  • Writes an NdefMessage to a supported card.
    
  • Writes containing NdefMessages are written to successive [NdefRecords](https://learn.adafruit.com/adafruit-pn532-rfid-nfc/ndef#ndef-records) as space permits. 
    
  • In most cases we will explicitly create a an NdefRecord, add it to an NdefMessage, and then write this to an RFID card like the example below, where we add an URI record.  After this write, if you were to scan this RFID tag with your NFC capable phone or tablet it will navigate to this wiki page.
    
if (NFC.packetReady()) {
       NdefMessage message = NdefMessage();
       message.addUriRecord("https://github.com/andium/Anduino/wiki/AnduinoNFC");
       bool success = NFC.write(message); //write this to our tag
    }

erase()

  • Writes an empty NDEF record to the tag.

format()

  • Formats the Mifare Classic or Ultralight tag to support NDEF.
  • To see this in action check out the formatToNDEF sketch in the examples.

clean()

  • Reformats the RFID tag as a Mifare Classic or Ultraight tag.
  • To see this in action check out the revertToClassic sketch in the examples.

#Contributions

The AnduinoNFC library builds on the PN532 support drivers and NDEF library authored by Adafruit Industries, xiongyihui, Seed Studio, and Don Coleman.

Clone this wiki locally