RFID Lock System with MFRC522
How to make RFID Lock System with MFRC522 RFID Module.
Components and supplies
1
Buzzer
1
General Purpose Transistor NPN
1
Jumper wires (generic)
1
Resistor 1k ohm
1
Arduino Mega 2560
1
RFID reader (generic)
1
SG90 Micro-servo motor
Tools and machines
1
Hot glue gun (generic)
1
Soldering iron (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Arduino Code
arduino
1#include <SPI.h> 2#include <MFRC522.h> 3#include <Servo.h> 4 5 6#define SS_PIN 53 7#define RST_PIN 5 8#define O 8 9 10MFRC522 mfrc522(SS_PIN, RST_PIN); 11MFRC522::MIFARE_Key key; 12 13Servo Ser; 14 15int blockNum = 2; 16byte blockData [10]; 17 18byte bufferLen = 18; //Set buffer length for reading, it has to be 2 more than block size : 16 + 2 19byte readBlockData[18]; //Array to store data from card 20byte Registry[18]; //Array to store key 21 22String UID, RUID = "f6255c11"; //UID of the card you are going to use, use MFRC522 Library to find UID of your Card or simply use your phone 23bool Access; 24 25MFRC522::StatusCode status; 26long R; 27void setup() { 28 Serial.begin(115200); //You can connect Serial output for debugging 29 Ser.attach(7); 30 Ser.write(130); 31 SPI.begin(); 32 mfrc522.PCD_Init(); //Initiate tranciever module 33 34 long R = 1234567899; //Default key for which this code would be looking for, change it if you like and then write the same key on RFID card for first time use 35 for(int i = 9;i >= 0;i--) { 36 int D = R % 10; 37 R = R / 10; 38 Registry[i] = D; 39 } 40 Serial.print("Default Key : "); 41 for (int j = 0 ; j < 10 ; j++) { 42 Serial.print(Registry[j]); 43 } 44 Serial.println(); 45 Serial.print("RUID : " + RUID); 46 Serial.println("\ 47"); 48 Serial.println("Place Key"); 49} 50 51void loop() { 52 for (byte i = 0; i < 6; i++) { 53 key.keyByte[i] = 0xFF; 54 } 55 if(!mfrc522.PICC_IsNewCardPresent()) { //Detect if new card is present 56 return; 57 } 58 if (!mfrc522.PICC_ReadCardSerial()) { 59 return; 60 } 61 Serial.println("Card Detected"); 62 R = random(1000000000, 9999999999); //New random key generation 63 64 Serial.print(F("PICC type: ")); 65 MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); 66 67 if (piccType != MFRC522::PICC_TYPE_MIFARE_1K) { //Check whether the card is of supported type otherwise communication drops 68 Serial.println(F("Your tag is not of supported type")); 69 return; 70 } 71 String Type = mfrc522.PICC_GetTypeName(piccType); 72 Serial.println(Type); 73 74 Serial.print("UID : "); 75 UID = ""; 76 77 for(byte i = 0;i < mfrc522.uid.size;i++) { 78 UID = UID + String(mfrc522.uid.uidByte[i], HEX); 79 } 80 Serial.println(UID); 81 82 if(UID == RUID) { //Checks whether Card UID are Stored UID are equal 83 Serial.println("UID Verification Successful"); 84 } 85 else { 86 Serial.println("UID Verification Failed"); 87 } 88 89 Serial.print("Stored Key : "); 90 91 mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid)); //Authenticate the sector and the block for reading memory 92 mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen); //Store read value in readBlockData 93 94 for (int j = 0 ; j < 10 ; j++) { 95 Serial.print(readBlockData[j]); 96 } 97 Serial.println(); 98 99 Serial.print("Current Registered Key Match : "); 100 for (int j = 0 ; j < 10 ; j++) { 101 Serial.print(Registry[j]); 102 } 103 Serial.println(); 104 105 if(Check(readBlockData, Registry)) { //Calls function to check whether the arrays containing Card Key and Stored key are equal 106 Serial.println("Key Match Successful"); 107 Serial.println("Access Granted"); 108 Access = 1; 109 } 110 else { 111 Serial.println("Key Match Failed"); 112 Serial.println("Access Denied"); 113 Ring(0); 114 Serial.println(); 115 mfrc522.PICC_HaltA(); //Communication broken, this makes the module ready to scan card again 116 mfrc522.PCD_StopCrypto1(); 117 return; //nothing else will execute, the loop starts over 118 } 119 120 Serial.print("New Key : "); 121 Serial.println(R); 122 for(int i = 9;i >= 0;i--) { 123 int D = R % 10; 124 R = R / 10; 125 blockData[i] = D; 126 Registry[i] = D; //Storing new key in memory 127 } 128 129 mfrc522.MIFARE_Write(blockNum, blockData, 16); //write the new random key in block 2 of sector 0 130 Serial.println("New Key Written Successfully"); 131 Serial.println(); 132 133 if(Access) { 134 Ring(1); //buzzer plays welcome tone 135 } 136 Ser.write(130); //door opens 137 138 mfrc522.PICC_HaltA(); //communication broken 139 mfrc522.PCD_StopCrypto1(); 140} 141 142void Ring(bool I) { 143 if(I) { 144 tone(O, 330, 200); //welcome tone 145 delay(200); 146 tone(O, 494, 250); 147 Ser.write(0); 148 delay(5000); 149 } 150 else { 151 tone(O, 75, 150); //access denied tone 152 delay(200); 153 tone(O, 75, 150); 154 Ser.write(130); 155 } 156} 157bool Check(byte A[], byte B[]) { 158 for(int i = 0;i < 10;i++) { 159 if(A[i] == B[i]) { 160 continue; 161 } 162 else { 163 return 0; 164 } 165 } 166 return 1; 167}
Downloadable files
Connection Diagram
Connection Diagram

Circuit Diagram Fritzing File
Circuit Diagram Fritzing File
Connection Diagram
Connection Diagram

Circuit Diagram Fritzing File
Circuit Diagram Fritzing File
Comments
Only logged in users can leave comments