RC522 RFID Module with Arduino
A simple Arduino project that reads the unique ID of an RFID card using an RC522 module and displays it on a 16×2 I2C LCD.
Devices & Components
1
Arduino Uno Rev3
1
USB Cable Type A Male to Micro Type B Male
1
16x2 LCD display with I²C interface
1
RC522 Module Kit
1
jumper wires for arduino
Software & Tools
Arduino IDE
Project description
Code
ReadWriteBlockWise
cpp
Arduino Code
1/* 2Code to get data from the UART ( @baud 115200 ) and write it on the specified block 3by www.playwithcircuit.com 4*/ 5 6#include <SPI.h> 7#include <MFRC522.h> // Header of Libraray to Run RFID card 8#include <LiquidCrystal_I2C.h> // Header of Library to Run I2C LCD 9 10#define SS_PIN 10 11#define RST_PIN 9 12 13MFRC522 rfid(SS_PIN, RST_PIN); 14MFRC522::MIFARE_Key key; 15 16// Set the LCD address to 0x27 for a 16 chars and 2 line display 17LiquidCrystal_I2C lcd(0x27, 16, 2); 18 19bool oneTimeFlag = false; 20 21void setup() { 22 // initialize the LCD 23 lcd.init(); 24 // Turn ON the Backlight 25 lcd.backlight(); 26 // Clear the display buffer 27 lcd.clear(); 28 29 Serial.begin(115200); 30 SPI.begin(); 31 rfid.PCD_Init(); 32 Serial.println("\nRFID Write Tool Ready"); 33 34 // Use default key (factory value) , its 0xFF 35 for (byte i = 0; i < 6; i++) { 36 key.keyByte[i] = 0xFF; 37 } 38 39 lcd.clear(); 40 lcd.setCursor(0, 0); 41 lcd.print("Place Card on"); 42 lcd.setCursor(0, 1); 43 lcd.print("Card Reader"); 44 delay(1000); 45} 46 47// this functions checks if we are writing into the wrong block or right block 48bool isWritableBlock(byte block) { 49 if (block == 0) return false; // Block 0 is UID (DO NOT WRITE) 50 if ((block + 1) % 4 == 0) return false; // Sector trailer (DO NOT WRITE) 51 return true; 52} 53 54void loop() { 55 byte writeBuffer[18] = { 0 }; 56 byte readBuffer[16] = { 0 }; 57 byte sizeofWriteBuffer = sizeof(writeBuffer); 58 byte bytestobeRead; 59 byte bytesRead; 60 byte sector; 61 MFRC522::StatusCode status; 62 byte blockNum; 63 byte trailerBlock; 64 65 // the flag makes ure that lcd is upadted only once 66 if (oneTimeFlag == false) { 67 lcd.clear(); 68 lcd.setCursor(0, 0); 69 lcd.print("Place Card on"); 70 lcd.setCursor(0, 1); 71 lcd.print("Card Reader"); 72 oneTimeFlag = true; 73 } 74 75 // Look for new card 76 if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return; 77 78 oneTimeFlag = false; 79 80 lcd.clear(); 81 lcd.setCursor(0, 0); 82 lcd.print("Card Detected"); 83 lcd.setCursor(0, 1); 84 lcd.print("Keep it there..."); 85 86 delay(1000); 87 88 // Print UID 89 Serial.print("Card UID: "); 90 lcd.clear(); 91 lcd.setCursor(0, 0); 92 lcd.print("Card UID :"); 93 lcd.setCursor(0, 1); 94 for (byte i = 0; i < rfid.uid.size; i++) { 95 Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); 96 Serial.print(rfid.uid.uidByte[i], HEX); 97 lcd.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); 98 lcd.print(rfid.uid.uidByte[i], HEX); 99 } 100 Serial.println(); 101 delay(3000); 102 103 // flush serial input before getting new data 104 while (Serial.available()) { 105 Serial.read(); 106 } 107 108 do { 109 lcd.clear(); 110 lcd.setCursor(0, 0); 111 lcd.print("Enter Block No: "); 112 Serial.println("Enter block number to write (0-63): "); 113 while (!Serial.available()) 114 ; 115 blockNum = Serial.parseInt(); 116 Serial.read(); // Consume leftover '\n' 117 118 if (!isWritableBlock(blockNum)) { 119 Serial.println("This block is not writable"); 120 lcd.setCursor(0, 1); 121 lcd.print("Invalid Block No"); 122 delay(1000); 123 } else { 124 lcd.setCursor(0, 1); 125 lcd.print(blockNum); 126 delay(1000); 127 break; 128 } 129 } while (1); 130 131 // flush serial input before getting new data 132 while (Serial.available()) { 133 Serial.read(); 134 } 135 136 do { 137 lcd.clear(); 138 lcd.setCursor(0, 0); 139 lcd.print("Enter bytes no.:"); 140 Serial.println("Enter number of bytes to write (1-16): "); 141 while (!Serial.available()) 142 ; 143 bytestobeRead = Serial.parseInt(); 144 Serial.read(); // Consume leftover '\n' 145 146 if (bytestobeRead < 1 || bytestobeRead > 16) { 147 Serial.println("\nInvalid byte count."); 148 lcd.setCursor(0, 1); 149 lcd.print("Invalid bytes no"); 150 delay(1000); 151 } else { 152 lcd.setCursor(0, 1); 153 lcd.print(bytestobeRead); 154 delay(1000); 155 break; 156 } 157 } while (1); 158 159 160 // flush serial input before getting new data 161 while (Serial.available()) { 162 Serial.read(); 163 } 164 165 do { 166 // set the timeout for read opearation, its 60 seconds 167 Serial.setTimeout(60000); 168 lcd.clear(); 169 lcd.setCursor(0, 0); 170 lcd.print("Enter Data : "); 171 Serial.print("Enter your Data : "); 172 while (Serial.available()) Serial.read(); // Clear buffer 173 174 memset(readBuffer, 0x00, 16); 175 // Read and store the specified number 176 bytesRead = Serial.readBytes(readBuffer, bytestobeRead); 177 readBuffer[bytesRead] = '\0'; // Add null terminator after last byte 178 179 if (bytesRead > 0) { 180 lcd.setCursor(0, 1); 181 lcd.print((char *)readBuffer); 182 delay(1000); 183 break; 184 } else { 185 Serial.println("\nNo Data Received."); 186 } 187 } while (1); 188 189 190 // Authenticate block 191 sector = blockNum / 4; // find the sector number from the block number 192 trailerBlock = sector * 4 + 3; // finf=d the trailer block of the particular sector 193 194 status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(rfid.uid)); 195 if (status != MFRC522::STATUS_OK) { 196 Serial.print("Authentication failed: "); 197 Serial.println(rfid.GetStatusCodeName(status)); 198 rfid.PICC_HaltA(); 199 rfid.PCD_StopCrypto1(); 200 lcd.clear(); 201 lcd.setCursor(0, 0); 202 lcd.print("Authen. failed: "); 203 lcd.setCursor(0, 1); 204 lcd.print(rfid.GetStatusCodeName(status)); 205 delay(1000); 206 return; 207 } else { 208 lcd.clear(); 209 lcd.setCursor(0, 0); 210 lcd.print("Authentication"); 211 lcd.setCursor(0, 1); 212 lcd.print("Successfull"); 213 delay(3000); 214 } 215 216 // From Here Onwards Secured Session Starts 217 // Read complete block , here 18 bytes are used because along with 16 bytes two bytes CRC is also Read which is internally calculated and then sent 218 memset(writeBuffer, 0x00, sizeofWriteBuffer); 219 220 status = rfid.MIFARE_Read(blockNum, writeBuffer, &sizeofWriteBuffer); 221 if (status == MFRC522::STATUS_OK) { 222 // Edit Required data 223 memcpy(writeBuffer, readBuffer, bytesRead); 224 // Write complete block 16 bytes , as bytes less than or greater than block size can't be written 225 status = rfid.MIFARE_Write(blockNum, writeBuffer, 16); 226 if (status == MFRC522::STATUS_OK) { 227 Serial.println("Data written successfully."); 228 lcd.clear(); 229 lcd.setCursor(0, 0); 230 lcd.print("Data Written"); 231 lcd.setCursor(0, 1); 232 lcd.print("Successfully"); 233 delay(3000); 234 } else { 235 Serial.print("Write failed: "); 236 Serial.println(rfid.GetStatusCodeName(status)); 237 lcd.clear(); 238 lcd.setCursor(0, 0); 239 lcd.print("Write failed: "); 240 lcd.setCursor(0, 1); 241 lcd.print(rfid.GetStatusCodeName(status)); 242 delay(3000); 243 return; 244 } 245 } else { 246 Serial.print("Read Failed: "); 247 Serial.println(rfid.GetStatusCodeName(status)); 248 lcd.clear(); 249 lcd.setCursor(0, 0); 250 lcd.print("Read failed: "); 251 lcd.setCursor(0, 1); 252 lcd.print(rfid.GetStatusCodeName(status)); 253 delay(3000); 254 return; 255 } 256 257 Serial.print("\nHere is the complete data of card:\n"); 258 // Dump debug info about the card; PICC_HaltA() is automatically called 259 rfid.PICC_DumpToSerial(&(rfid.uid)); 260 // End session 261 rfid.PCD_StopCrypto1(); 262 263 lcd.clear(); 264 lcd.setCursor(0, 0); 265 lcd.print("Remove Card From"); 266 lcd.setCursor(0, 1); 267 lcd.print("Card Reader "); 268 delay(2000); 269}
Comments
Only logged in users can leave comments