0% found this document useful (0 votes)
20 views2 pages

DDDD

This document contains an Arduino sketch for interfacing with an NFC reader using the Adafruit PN532 library. It initializes the reader, waits for a card to be placed on it, and attempts to authenticate and write a predefined UID to a specific block of the card. The code includes error handling for authentication and writing processes.

Uploaded by

Some Guy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

DDDD

This document contains an Arduino sketch for interfacing with an NFC reader using the Adafruit PN532 library. It initializes the reader, waits for a card to be placed on it, and attempts to authenticate and write a predefined UID to a specific block of the card. The code includes error handling for authentication and writing processes.

Uploaded by

Some Guy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <Wire.

h>
#include <Adafruit_PN532.h>

#define SDA_PIN 21
#define SCL_PIN 22

Adafruit_PN532 nfc(SDA_PIN, SCL_PIN);

// Replace with your student card UID


uint8_t newUID[4] = {0x2F, 0x47, 0x81, 0x0E};

void setup(void) {
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN);

nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (!versiondata) {
Serial.println("Didn't find PN532");
while (1);
}
nfc.SAMConfig();

Serial.println("Place your magic card on the reader...");


}

void loop(void) {
uint8_t uid[7]; uint8_t uidLength;

if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) {


Serial.print("Found card with UID: ");
for (uint8_t i = 0; i < uidLength; i++) {
Serial.print(uid[i], HEX); Serial.print(" ");
}
Serial.println();

// Calculate BCC
uint8_t bcc = newUID[0] ^ newUID[1] ^ newUID[2] ^ newUID[3];

// Fill block 0 data


uint8_t block0[16] = {
0x2F, 0x47, 0x81, 0x0E, 0xE7,
0x38, 0x02, 0x00, 0x00,
0x00, 0x00, 0x00, 0x21, 0x07, 0x44, 0x00
};

if (nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 0, 0, (uint8_t[])


{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF})) {
Serial.println("Authenticated block 0");

if (nfc.mifareclassic_WriteDataBlock(0, block0)) {
Serial.println("✅ UID successfully written to block 0!");
} else {
Serial.println("❌ Failed to write block 0");
}
} else {
Serial.println("❌ Auth failed");
}
delay(5000);
}
}

You might also like