Skip to content

Instantly share code, notes, and snippets.

@stinethebean3
Created September 13, 2015 23:38
Show Gist options
  • Select an option

  • Save stinethebean3/b62515290630516804c6 to your computer and use it in GitHub Desktop.

Select an option

Save stinethebean3/b62515290630516804c6 to your computer and use it in GitHub Desktop.
Adafruit PN532 read UID example
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
pinMode(13, OUTPUT);
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 an ISO14443A Card ...");
}
void loop(void) {
//Authorized UIDs
uint8_t auth_uids[][7] = {
{0000, 0000, 0000, 0000, 0000, 0000, 0000}, // TAG
};
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t uidLength;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)){
Serial.print("Entry Request for UID: ");
nfc.PrintHex(uid, uidLength);
uint32_t i;
uint32_t b;
int num_uids = sizeof(auth_uids)/sizeof(auth_uids[0]);
Serial.println(num_uids);
for (i=0; i < num_uids; i++){
Serial.print("Comparing UID to: ");
nfc.PrintHex(auth_uids[i], 7);
for (b=0; b < 7; b++){
if (uid[b] == auth_uids[i][b]){
if (b == 6){
digitalWrite(13, HIGH);
Serial.println("Unlocked!");
delay(10000);
digitalWrite(13, LOW);
Serial.println("Locked!");
i=num_uids;
}
} else {
Serial.println("Failed!");
b=7;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment