This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tag_Write_Pattern.ino
104 lines (83 loc) · 3.83 KB
/
Tag_Write_Pattern.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Write personal data of a MIFARE RFID card using a RFID-RC522 reader
Uses MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
-----------------------------------------------------------------------------------------
MFRC522 Arduino Arduino Arduino Arduino Arduino
Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
Signal Pin Pin Pin Pin Pin Pin
-----------------------------------------------------------------------------------------
RST/Reset RST 9 (7) 5 D9 RESET/ICSP-5 RST
SPI SS SDA(SS) 10 53 D10 10 10
SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
Hardware required:
Arduino
PCD (Proximity Coupling Device): NXP MFRC522 Contactless Reader IC
PICC (Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203.
The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 49 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
#define PATTERN_ID 6
#define BLOCK 1
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Write TAG Pattern ID"));
}
void loop() {
MFRC522::StatusCode status;
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.print(F("Card UID(HEX):")); //Dump HEX UID
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print(F(" PICC type: ")); // Dump PICC type
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
byte readbackblock[18];//This array is used for reading out a block. The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.
readBlock(BLOCK, readbackblock);//read the block back
Serial.print("read block: ");
for (int j = 0 ; j < 16 ; j++) //print the block contents
{
Serial.print (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
Serial.print (" ");
}
Serial.println("");
byte blockcontent[16];
for (int i = 0; i<16; i++){
blockcontent[i]=0;
}
blockcontent[0]= PATTERN_ID;
writeBlock(BLOCK, blockcontent);//the blockcontent array is written into the card block
Serial.println(" ");
readBlock(BLOCK, readbackblock);//read the block back
Serial.print("read block: ");
for (int j = 0 ; j < 16 ; j++) //print the block contents
{
Serial.print (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
Serial.print (" ");
}
Serial.println("");
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}