Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <SPI.h>
#include <MFRC522.h>
// this is configured for an Arduino Mega. Check any of the example sketches of the MFRC522
// to check the pinouts
#define RST_PIN 5
#define SS_PIN 53
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600);
while (!Serial);
SPI.begin();
mfrc522.PCD_Init();
mfrc522.PCD_DumpVersionToSerial();
delay(1000);
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
void loop() {
// try to open the block 0 write backdoor of chinese cards
if(!mfrc522.MIFARE_OpenUidBackdoor(true)) {
delay(1000);
return;
}
byte block0_buffer[] = {0x01, 0x02, 0x03, 0x04, // Tag UID. Change this to your liking.
0x04, // UID Checksum (0^1^2^3). Important! XOR all the UID bytes together to get this checksum. If incorrect, card will not work correctly
0x08, // SAK
0x04, 0x00, // ATQA (First block LSB)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Manufacture data
0x00, // Production year
0x00 // Production month
};
MFRC522::StatusCode status = mfrc522.MIFARE_Write((byte) 0, block0_buffer, (byte) 16);
if (status != mfrc522.STATUS_OK) {
Serial.print("Error: ");
Serial.println(mfrc522.GetStatusCodeName(status));
} else {
Serial.print("Done. Wrote ");
// let's pretty-print the written block
for(int i = 0; i < sizeof(block0_buffer); i++) {
printHex(block0_buffer[i]);
}
Serial.print(" ");
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.println("Sleeping for 10s");
delay(10000);
}
void printHex(uint8_t num) {
char hexChar[2];
sprintf(hexChar, "%02X", num);
Serial.print("0x");
Serial.print(hexChar);
Serial.print(" ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment