Arduino RFID Lock System

by hariramanil810 in Circuits > Arduino

6 Views, 0 Favorites, 0 Comments

Arduino RFID Lock System

Screenshot (729).png

This is an arduino RFID Lock system which is a simple project

Supplies

If you have hard-time 3d printing stuff and other materials which i have provided in this project please refer the professionals for the help, JLCPCB is one of the best company from shenzhen china they provide, PCB manufacturing, PCBA and 3D printing services to people in need, they provide good quality products in all sectors


Please use the following link to register an account in JLCPCB

jlcpcb.com/RNA


Pcb Manufacturing

----------

2 layers

4 layers

6 layers

jlcpcb.com/RNA


PCBA Services

JLCPCB have 350k+ Components In-stock. You don’t have to worry about parts sourcing, this helps you to save time and hassle, also keeps your costs down.

Moreover, you can pre-order parts and hold the inventory at JLCPCB, giving you peace-of-mind that you won't run into any last minute part shortages. jlcpcb.com/RNA


3d printing

-------------------

SLA -- MJF --SLM -- FDM -- & SLS. easy order and fast shipping makes JLCPCB better companion among other manufactures try out JLCPCB 3D Printing servies

JLCPCB 3D Printing starts at $1 &Get $54 Coupons for new users



RFID stands for Radio Frequency IDentification and it’s a non-contact technology that’s broadly used in many industries for tasks such as personnel tracking, access control, supply chain management, books tracking in libraries, tollgate systems and so on.[/column]

Testing

Screenshot (731).png
Screenshot (732).png
Screenshot (733).png

The RFID reader consist of a radio frequency module, a control unit and an antenna coil which generates high frequency electromagnetic field. On the other hand, the tag is usually a passive component, which consist of just an antenna and an electronic microchip, so when it gets near the electromagnetic field of the transceiver, due to induction, a voltage is generated in its antenna coil and this voltage serves as power for the microchip.

Now as the tag is powered it can extract the transmitted message from the reader, and for sending message back to the reader, it uses a technique called load manipulation. Switching on and off a load at the antenna of the tag will affect the power consumption of the reader’s antenna which can be measured as voltage drop. This changes in the voltage will be captured as ones and zeros and that’s the way the data is transferred from the tag to the reader.

There’s also another way of data transfer between the reader and the tag, called backscattered coupling. In this case, the tag uses part of the received power for generating another electromagnetic field which will be picked up by the reader’s antenna.

Schematics

Screenshot (734).png
Screenshot (735).png
Screenshot (737).png
Screenshot (736).png

When power ON this door lock, the servo motor activates and pushes the door lock forward. Also displayed as “Welcome, put your card” on the LCD. Then when the RFID tag is moved closer to the RFID reader, it is scanned. In that case, it is displayed as “scanning” on the LCD. Then, if the RFID tag is correct, the servo motor is activated and the door lock is pulled back. The LCD shows “Door is Open”. When the RFID tag is moved closer to the RFID reader again, if it gets the correct tag, the servo motor will push the lock forward. Displays “Door is locked” on LCD. If a wrong RFID tag is used according to the program, it will be displayed as “Wrong card” on the LCD.

The fingerprint access control systems are preferred only for high security or personal security solutions, are highly personalized, and are costly. The PIN and RFID are cost-effective solutions applicable to public access systems like hotel rooms, classrooms, and offices. Of these two, RFID access control systems come with higher security and offer wider applications. For example, RFID solutions can be used simultaneously for access control as well as attendance in school/college classrooms.

In this project, we are designing an RFID door lock system and will explore how the locking mechanism can be built in different ways.

The read bytes of NUID are stored in a character array. This character array is compared to a predefined NUID using user-defined compareNUID() function. The function returns a boolean value used to determine access control. If the scanned NUID is matched with the programmed ID, a message granting access is displayed on the OLED screen. If the scanned NUID does not match the programmed ID, a message denying access is displayed on the OLED screen.

Wiring

Screenshot (738).png
Screenshot (739).png
Screenshot (740).png
Screenshot (741).png
Screenshot (742).png

The door lock system consists of two critical circuits – the access control system and a locking mechanism. For the access control system here, an RFID is used. This door lock uses an RC522 RFID reader to read MIFARE and NTAG compatible RFID cards. Each lock passes access to only one RFID card, so all other cards are denied access. The system can also be programmed to provide additional access to a master card, which may be used instead of a lost card. The user interface is designed using an SSD1306 OLED display. Therefore, the complete access control system is designed by interfacing the RC522 RFID reader and SSD1306 OLED display with Arduino UNO.


Next, constants are defined for circuit connections with the OLED display, and an object of the Adafruit_SSD1306 class is instantiated. The constants for circuit connections with RC522 are defined, and an object of the MFRC522 class is instantiated. A variable to store the MIFARE key is defined. A byte array ‘ nuidPICC[]’ is defined to store 4 bytes of RFID NUID. Variables are declared to store string representations of the NUID in decimal and hexadecimal formats. Similarly, a variable to store a string representation of the MIFARE key is defined. A bitmap is stored in PROGMEM for site loge, and an array is defined for the same.

In the setup() function, the baud rate for serial communication is set to 9600 bps. The OLED display is initialized by calling display.begin(SSD1306_SWITCHCAPVCC) method and display is flashed by calling display.display() method. The SPI protocol is initialized by calling SPI.begin() method. The RFID module initializes the PCD_Init() method on the RFID object. The MIFARE key bytes are set to FF and stored in an upper-case string representation. The MIFARE key is published on the Serial Monitor. The OLED display is cleared, and the site logo is flashed.

In the loop() function, some initial messages are first flashed on the screen. RFID card/key/tag is detected by calling PICC_IsNewCardPresent() method on the RFID object. Next, the card NUID/UID is read by calling the PICC_ReadCardSerial() method on the RFID object. The MIFARE card type is detected using the PICC_GetType(rfid.uid.sak) method and displayed on the Serial Monitor. Next, the individual bytes of the NUID are accessed using the rfid.uid.uidByte[] property. First, the bytes are serialized in decimal format and converted to a string object. The string containing a decimal representation of NUID is published on the Serial Monitor. Next, the bytes are serialized in hexadecimal format and converted to a string object. The string containing a hexadecimal representation of NUID is published on the serial monitor.

The RC522 RFID reader is interfaced with Arduino using SPI protocol. The following circuit connections are made for interfacing the RFID reader with Arduino UNO. To learn more about reading RFID cards using RC522,

Code

Screenshot (743).png
Screenshot (744).png

The locking mechanism can be built in two different ways. A do-it-yourself lock can be designed using a servo motor in one method. If the locking mechanism is built using a servo motor, it requires a PWM pin on Arduino for control.

It is recommended that servo is provided separate power supply using batteries as power fluctuations from the servo motor can damage the Arduino board.

In the second method, the lock can be designed using a solenoid lock. In this case, the lock can be controlled using any GPIO on Arduino via a relay module.

The RFID reader used to design this door lock is RC522. This RFID reader can read MIFARE and NTAG compatible RFID cards. The RFID cards either have a 7-byte long Unique Identity Number (UID) or 4-byte long Non-Unique Identity Number (NUID). Even if NUID cards are used, there is almost no possibility of getting two cards with the same NUID. The locking systems are designed such that the ID number of scanned cards is not disclosed in the process of access control. Even if the ID of a card is accessed from a different device, it is far from possible to get an RFID card of the same ID number.

When an RFID card is scanned through the RC522, it reads the NUID/UID of the particular card. The scanned NUID is compared against a pre-stored ID number for the door lock system. If the ID of the scanned card matches with the programmed ID, a welcome message is displayed on the OLED display. If the ID of the scanned card does not match with the programmed ID, the door remains unlocked, and a message denying access is displayed on the OLED display. At the same time, the door is unlocked either by rotating the servo in case of DIY servo lock or activating the relay in case of solenoid lock. The lock is programmed to pass access to only one RFID card.

The code

The sketch begins with importing SPI and MFRC522 library for working with the RC522 reader. The library used in this project is Arduino MFRC522 Library from miguelbalboa, available at Github. Download the library as a ZIP file. Navigate to Sketch->Include Library->Add .ZIP Library in Arduino IDE. Select the rfid-master.ZIP, and the library for RC522 is added to the Arduino environment.

This is followed by importing Wire, Adafruit_GFX, and Adafruit_SSD1306 libraries for working with SSD1306 OLED. These libraries can be easily found in the library manager.

Code

#include <SPI.h>

#include <MFRC522.h>

#include <Wire.h>

#include <Servo.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):#define OLED_MOSI 6#define OLED_CLK 7#define OLED_DC 4#define OLED_CS 2#define OLED_RESET 5 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#define SS_PIN 10#define RST_PIN 9MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the classMFRC522::MIFARE_Key key;

int servoPin = 3;Servo servo;int angle = 0; // servo position in degreesboolean access_flag;

// Init array that will store new NUID byte nuidPICC[4];

String NUID_dec, NUID_hex;String RKey;

// 'eeworldonlineblack-135x45', 130x45pxconst unsigned char epd_bitmap_eeworldonlineblack_135x45 [] PROGMEM = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xe0, 0x00, 0x60, 0x00, 0x67, 0xf8, 0x1f, 0xc1, 0xf8, 0x00, 0x3f, 0x06, 0x0c, 0x1f, 0xe0, 0x18, 0x00, 0xe0, 0x00, 0x60, 0x00, 0x67, 0xf0, 0x1f, 0xc1, 0xe0, 0x00, 0x1f, 0x04, 0x0c, 0x1f, 0x80, 0x00, 0x00, 0xe0, 0x00, 0x60, 0x00, 0x47, 0xf0, 0x1f, 0xc1, 0xe0, 0x00, 0x0f, 0x00, 0x0c, 0x1f, 0x00, 0x00, 0x00, 0xe1, 0xff, 0xe1, 0xff, 0xc3, 0xf0, 0x1f, 0xc1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 0x0f, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x87, 0xff, 0x87, 0xff, 0xf0, 0x83, 0xc2, 0x0f, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x87, 0xff, 0x87, 0xff, 0xf0, 0x87, 0xc2, 0x0f, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x8f, 0xff, 0x87, 0xff, 0xf0, 0x07, 0xc2, 0x1f, 0xc1, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x8f, 0xff, 0x87, 0xff, 0xf0, 0x07, 0xc0, 0x1f, 0xc1, 0xfe, 0x07, 0x07, 0xfc, 0x1e, 0x07, 0xf0, 0x00, 0x8f, 0xff, 0x8f, 0xff, 0xf0, 0x07, 0xe0, 0x1f, 0xc0, 0x00, 0x07, 0x07, 0xfc, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x03, 0xf8, 0x0f, 0xe0, 0x1f, 0xc0, 0x00, 0x0f, 0x07, 0xfc, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x01, 0xf8, 0x0f, 0xe0, 0x1f, 0xe0, 0x00, 0x0f, 0x07, 0xfc, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0xf8, 0x0f, 0xe0, 0x3f, 0xf0, 0x00, 0x1f, 0x07, 0xfc, 0x1f, 0x80, 0x08, 0x00, 0x00, 0x03, 0x00, 0x03, 0xf8, 0x0f, 0xf0, 0x3f, 0xf8, 0x00, 0x7f, 0x07, 0xfc, 0x1f, 0xe0, 0x3c, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x83, 0xff, 0x7d, 0xfe, 0x7f, 0xf7, 0xfb, 0xef, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7d, 0xff, 0x3d, 0xfe, 0x7f, 0xf7, 0xf9, 0xef, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7e, 0xff, 0x5d, 0xfe, 0x7f, 0xf7, 0xfa, 0xef, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xff, 0x4d, 0xfe, 0x7f, 0xf7, 0xfa, 0xef, 0xf3, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xff, 0x6d, 0xfe, 0x7f, 0xf7, 0xfb, 0x6f, 0xf0, 0x40, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xff, 0x75, 0xfe, 0x7f, 0xf7, 0xfb, 0xaf, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7e, 0xff, 0x71, 0xfe, 0x7f, 0xf7, 0xfb, 0x8f, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xff, 0x79, 0xfe, 0x7f, 0xf7, 0xfb, 0xcf, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x83, 0xff, 0x7d, 0xff, 0x0f, 0xf7, 0xfb, 0xef, 0xf8, 0x00};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 784)const int epd_bitmap_allArray_LEN = 1;const unsigned char* epd_bitmap_allArray[1] = { epd_bitmap_eeworldonlineblack_135x45};


void setup() { servo.attach(servoPin);//Secure lock on the doorfor(angle = 0; angle < 90; angle++) { servo.write(angle); delay(15);}Serial.begin(9600);display.begin(SSD1306_SWITCHCAPVCC);display.display();

SPI.begin(); // Init SPI busrfid.PCD_Init(); // Init MFRC522 //Define Keyfor (byte i = 0; i < 6; i++) {key.keyByte[i] = 0xFF;}RKey = String(key.keyByte[0], HEX) + " " +String(key.keyByte[1], HEX) + " " +String(key.keyByte[2], HEX) + " " +String(key.keyByte[3], HEX) + " " +String(key.keyByte[4], HEX) + " " +String(key.keyByte[5], HEX);RKey.toUpperCase();Serial.print(F("MFRC522 Key: "));Serial.println(RKey);

display.clearDisplay();display.drawBitmap(0, 0, epd_bitmap_eeworldonlineblack_135x45, 130, 45, WHITE);display.display();delay(1000);}

void loop() {printtext("RFID Door Lock");

// Reset the loop if no new card present on the sensor/reader.if ( ! rfid.PICC_IsNewCardPresent()) return;

// Verify if the NUID has been readedif ( ! rfid.PICC_ReadCardSerial()) return;

Serial.print(F("PICC type: "));MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);Serial.println(rfid.PICC_GetTypeName(piccType));

NUID_dec = String(rfid.uid.uidByte[0])+ " " +String(rfid.uid.uidByte[1]) + " " +String(rfid.uid.uidByte[2]) + " " +String(rfid.uid.uidByte[3]);Serial.print("NUID Tag (DEC): "); Serial.println(NUID_dec);

NUID_hex = String(rfid.uid.uidByte[0], HEX) + " " +String(rfid.uid.uidByte[1], HEX) + " " +String(rfid.uid.uidByte[2], HEX) + " " +String(rfid.uid.uidByte[3], HEX);NUID_hex.toUpperCase();Serial.print("NUID Tag (HEX): "); Serial.println(NUID_hex);

for (byte i = 0; i < 4; i++) {nuidPICC[i] = rfid.uid.uidByte[i];}

if (compareNUID(nuidPICC, 41, 153, 94, 89)) { Serial.println("Access Granted"); printtext("Access Granted"); access_flag = true; } else{ Serial.println("Access Denied"); printtext("Access Denied"); access_flag = false; } Serial.println();

// Halt PICCrfid.PICC_HaltA();

// Stop encryption on PCDrfid.PCD_StopCrypto1();

if (access_flag == true){Serial.println("Unlocking the door");for(angle = 90; angle > 0; angle--) { servo.write(angle); delay(15);}}else{servo.write(90);delay(15);} }

void printtext(String s){display.clearDisplay();display.setTextSize(2); display.setTextColor(SSD1306_WHITE);display.setCursor(0, 0);display.println(String(s));display.display();delay(1000);}


boolean compareNUID(byte x[4], byte x0, byte x1, byte x2, byte x3){if (x[0] == x0 && x[1] == x1 && x[2] == x2 && x[3] == x3)return true; else return false;}