Skip to content

Commit c1eec5f

Browse files
authored
Add files via upload
1 parent 51c99d4 commit c1eec5f

10 files changed

+190
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <SPI.h>
2+
#include <MFRC522.h>
3+
4+
#define SS_PIN 10
5+
#define RST_PIN 9
6+
#define LED 8
7+
8+
byte readCard[4];
9+
String tag_UID = "39C3BB99"; // Replace this with the UID of your tag!!!
10+
String tagID = "";
11+
12+
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
13+
14+
void setup()
15+
{
16+
pinMode(LED, OUTPUT);// initialize digital pin LED_BUILTIN as an output.
17+
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
18+
SPI.begin(); // SPI bus
19+
mfrc522.PCD_Init(); // Initialise MFRC522
20+
}
21+
22+
void loop()
23+
{
24+
//Wait until new tag is available
25+
while (readID())
26+
{
27+
if (tagID == tag_UID)
28+
{
29+
digitalWrite(LED, !digitalRead(LED)); // Turn on or off the onboard led
30+
}
31+
}
32+
}
33+
34+
//Read new tag if available
35+
boolean readID()
36+
{
37+
//Check if a new tag is detected or not. If not return.
38+
if ( ! mfrc522.PICC_IsNewCardPresent())
39+
{
40+
return false;
41+
}
42+
//Check if a new tag is readable or not. If not return.
43+
if ( ! mfrc522.PICC_ReadCardSerial())
44+
{
45+
return false;
46+
}
47+
tagID = "";
48+
// Read the 4 byte UID
49+
for ( uint8_t i = 0; i < 4; i++)
50+
{
51+
//readCard[i] = mfrc522.uid.uidByte[i];
52+
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Convert the UID to a single String
53+
}
54+
tagID.toUpperCase();
55+
mfrc522.PICC_HaltA(); // Stop reading
56+
return true;
57+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include <SPI.h> //include the SPI library
2+
#include <MFRC522.h> //include the MFRC522 RFID reader library
3+
4+
#define RST_PIN 9 //reset pin, which can be changed to another digital pin if needed.
5+
#define SS_PIN 10 //SS or the slave select pin, which can be changed to another digital pin if needed.
6+
7+
MFRC522 mfrc522(SS_PIN, RST_PIN); // create a MFRC522 instant.
8+
MFRC522::MIFARE_Key key; //create a MIFARE_Key struct named 'key' to hold the card information
9+
10+
byte data1[14] = {"Circuit-Digest"}; //The first data that needs to be written to the tag.
11+
byte data2[12] = {"Jobit-Joseph"}; //The second data that needs to be written to the tag.
12+
byte readbackblock[18]; //Array for reading out a block.
13+
14+
void setup()
15+
{
16+
Serial.begin(115200); // Initialize serial communications with the PC
17+
SPI.begin(); // Init SPI bus
18+
mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
19+
Serial.println("Scan a MIFARE Classic card");
20+
21+
for (byte i = 0; i < 6; i++)
22+
{
23+
key.keyByte[i] = 0xFF; // Prepare the security key for the read and write operations.
24+
}
25+
}
26+
27+
void loop()
28+
{
29+
// Look for new cards if not found rerun the loop function
30+
if ( ! mfrc522.PICC_IsNewCardPresent()) {
31+
return;
32+
}
33+
34+
// read from the card if not found rerun the loop function
35+
if ( ! mfrc522.PICC_ReadCardSerial())
36+
{
37+
return;
38+
}
39+
Serial.println("card detected. Writing data");
40+
writeBlock(1, data1); //write data1 to the block 1 of the tag
41+
writeBlock(2, data2); //write data2 to the block 2 of the tag
42+
43+
Serial.println("reading data from the tag");
44+
readBlock(1, readbackblock); //read block 1
45+
//print data
46+
Serial.print("read block 1: ");
47+
for (int j = 0 ; j < 14 ; j++)
48+
{
49+
Serial.write (readbackblock[j]);
50+
}
51+
Serial.println("");
52+
readBlock(2, readbackblock); //read block 2
53+
//print data
54+
Serial.print("read block 2: ");
55+
for (int j = 0 ; j < 12 ; j++)
56+
{
57+
Serial.write (readbackblock[j]);
58+
}
59+
Serial.println("");
60+
61+
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));//uncomment below line if want to see the entire memory dump.
62+
63+
64+
}
65+
66+
67+
68+
//Write specific block
69+
int writeBlock(int blockNumber, byte arrayAddress[])
70+
{
71+
//check if the block number corresponds to data block or triler block, rtuen with error if it's trailer block.
72+
int largestModulo4Number = blockNumber / 4 * 4;
73+
int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector
74+
if (blockNumber > 2 && (blockNumber + 1) % 4 == 0) {
75+
Serial.print(blockNumber);
76+
Serial.println(" is a trailer block: Error");
77+
return 2;
78+
}
79+
80+
81+
//authentication
82+
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
83+
if (status != MFRC522::STATUS_OK) {
84+
Serial.print("Authentication failed: ");
85+
Serial.println(mfrc522.GetStatusCodeName(status));
86+
return 3;//return "3" as error message
87+
}
88+
89+
//writing data to the block
90+
status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
91+
//status = mfrc522.MIFARE_Write(9, value1Block, 16);
92+
if (status != MFRC522::STATUS_OK) {
93+
Serial.print("Data write failed: ");
94+
Serial.println(mfrc522.GetStatusCodeName(status));
95+
return 4;//return "4" as error message
96+
}
97+
Serial.print("Data written to block ");
98+
Serial.println(blockNumber);
99+
}
100+
101+
102+
103+
//Read specific block
104+
int readBlock(int blockNumber, byte arrayAddress[])
105+
{
106+
int largestModulo4Number = blockNumber / 4 * 4;
107+
int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector
108+
109+
//authentication of the desired block for access
110+
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
111+
112+
if (status != MFRC522::STATUS_OK) {
113+
Serial.print("Authentication failed : ");
114+
Serial.println(mfrc522.GetStatusCodeName(status));
115+
return 3;//return "3" as error message
116+
}
117+
118+
//reading data from the block
119+
byte buffersize = 18;
120+
status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);//&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
121+
if (status != MFRC522::STATUS_OK) {
122+
Serial.print("Data read failed: ");
123+
Serial.println(mfrc522.GetStatusCodeName(status));
124+
return 4;//return "4" as error message
125+
}
126+
Serial.println("Data read successfully");
127+
}
Loading
Loading

Interfacing RFID Reader With Arduino/Images/RC522 Schematics.svg

Lines changed: 6 additions & 0 deletions
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)