Created
March 19, 2014 19:26
-
-
Save pingswept/9649339 to your computer and use it in GitHub Desktop.
Code to read UID from Charlie Card using Adafruit PN532 NFC shield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************/ | |
// Based on code from Adafruit's PN532 NFC shield example | |
// BSD license | |
/**************************************************************************/ | |
#include <Wire.h> | |
#include <Adafruit_NFCShield_I2C.h> | |
#define IRQ (2) | |
#define RESET (3) // Not connected by default on the NFC Shield | |
Adafruit_NFCShield_I2C nfc(IRQ, RESET); | |
void setup(void) { | |
Serial.begin(115200); | |
Serial.println("Hello!"); | |
nfc.begin(); | |
uint32_t versiondata = nfc.getFirmwareVersion(); | |
if (! versiondata) { | |
Serial.print("Didn't find PN53x board"); | |
while (1); // halt | |
} | |
// Got ok data, print it out! | |
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); | |
// configure board to read RFID tags | |
nfc.SAMConfig(); | |
Serial.println("Waiting for a card ..."); | |
} | |
void loop(void) { | |
uint8_t success; | |
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID | |
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) | |
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found | |
// 'uid' will be populated with the UID, and uidLength will indicate | |
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) | |
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); | |
if (success) { | |
// Display some basic information about the card | |
Serial.println("Found a card"); | |
Serial.print(" UID Length: "); | |
Serial.print(uidLength, DEC); | |
Serial.println(" bytes"); | |
Serial.print(" UID Value: "); | |
nfc.PrintHex(uid, uidLength); | |
Serial.println(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment