Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Last active April 2, 2021 02:29
Show Gist options
  • Save stonehippo/6326056 to your computer and use it in GitHub Desktop.
Save stonehippo/6326056 to your computer and use it in GitHub Desktop.
Reading out the contents of an NTAG203 NFC tag using an Adafruit PN532 shield for Arduino. This sketch reads and prints to the serial console all 42 pages of memory on the tag.
/*
Dump the memory on a NTAG203 NFC tag using the Adafruit PN532 RFID/NFC Shield
http://www.adafruit.com/products/789
This sketch requires the installation of the Adafruit I2C library for the shield,
which can be found at https://github.com/adafruit/Adafruit_NFCShield_I2C
The datasheet and other info for the NTAG203 can be found at
http://www.nxp.com/products/identification_and_security/smart_label_and_tag_ics/ntag/series/NTAG203.html
NTAG203 NFC chips are available from http://andytags.com
*/
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
#define IRQ (2)
#define RESET (3)
Adafruit_NFCShield_I2C nfc(IRQ, RESET);
void setup(){
Serial.begin(115200);
Serial.println("Starting NFC...");
nfc.begin();
uint32_t card = nfc.getFirmwareVersion();
if (! card) {
Serial.print("Card fail! I die now...");
while(1); // DIE!
}
nfc.SAMConfig();
Serial.println("Waiting for a tag...");
}
void loop(){
uint8_t success;
uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if(success){
/*
The NTAG203 has a 7 byte UID, so I'll assume that if this tag does, it is the
right type of tag. But this isn't a fool-proof assumption, since Mifare Ultralight
tags also use a 7 byte UID. This code will still work with those tags, but it will
only read out the first 42 pages (out of 64) on the tag.
I don't know enough about the NFC card structure to know if there's a reliable
method to determine the NFC tag type.
*/
if (uidLength == 7) {
uint8_t data[32];
/*
Note that the tag must remain in range of the reader while the loop is running
for all of the pages to be read out. If the tag moves out of range, the page
printing will just stop at whatever point it was at.
*/
for (uint8_t i = 0; i < 42; i++) {
success = nfc.mifareultralight_ReadPage(i, data);
if (success) {
Serial.print("Page ");Serial.print(i);Serial.print(": ");
nfc.PrintHexChar(data, 4);
}
}
Serial.println("");
delay(1000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment