Project - Rfid Door Lock
Project - Rfid Door Lock
NAME : ___________________________
CLASS : ____________________________
Content by:
www.aimsity.com Aimsity
Table of Content
ARDUINO BOARD........................................................................................................................3
ARDUINO COMPONENT..............................................................................................................7
Project outcome...........................................................................................................................9
Step 5 : Now let’s create the program for scanning RFID tags............................................. 15
Full Code.................................................................................................................................... 25
ARDUINO BOARD
PARTS IN YOUR KIT
PROJECT : RFID DOOR LOCK
Learning Objectives
● Learn how RFID modules read and process data from RFID tags.
● Interface an LCD module using the I2C protocol for efficient communication.
● Operate a servo motor to enable mechanical movement for locking/unlocking.
● Combine RFID, LCD, and servo motor into a single working system.
● Store and validate authorized IDs for secure access.
Introduction
The RFID-based door lock system is a smart and secure method for access control.
This project uses RFID (Radio Frequency Identification) technology to authenticate
users and grant access to a secured area. When an authorized RFID card or tag is
presented, the system unlocks a door by moving a servo motor. It provides feedback to
the user through an LCD (using I2C communication) to display status messages such
as "Access Granted" or "Access Denied."
This project is ideal for enhancing your understanding of Arduino programming, circuit
integration, and IoT security concepts.
ARDUINO COMPONENT
Project outcome
Step 1 : Connect the LCD
SDA Pin 20
SCL Pin 21
VCC 5V
GND GND
Step 2: Connect the Servo Motor
SDA Pin 53
SCK Pin 52
MOSI Pin 51
MISO Pin 50
IRQ None
RST Pin 49
VCC 3.3V
GND GND
Step 4 : Open software and connect arduino to laptop
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 49
#define SS_PIN 53
void setup() {
Serial.begin(9600);
lcd.init(); lcd.backlight(); lcd.print("Put your card");
SPI.begin(); mfrc522.PCD_Init();
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent() ||
!mfrc522.PICC_ReadCardSerial()) return;
But first you need to open your Serial monitor in Arduino IDE.
After that you will have this screen.
● Purpose: Controls LCD displays using the I2C interface, requiring fewer pins.
● Why It's Needed: Displays messages like "Welcome!" or "Door is locked" on a
16x2 LCD.
● Example: lcd.print("Welcome!"); displays text on the LCD.
SPI Library (#include <SPI.h>)
● Purpose: Handles communication between the Arduino and devices using the
SPI protocol.
● Why It's Needed: Used to communicate with the RFID reader module.
● Example: SPI.begin(); initializes SPI communication.
● SS_PIN and RST_PIN are the pins used to connect the RFID reader to the
Arduino Mega.
● UID is the authorized card's unique identifier. Only this card can control the lock.
● lock keeps track of whether the door is locked (1) or unlocked (0).
3. Timers for Auto-Lock:
unsigned long unlockTime = 0; // Time when the door was last unlocked
const unsigned long autoLockDelay = 10000; // 10 seconds before
auto-lock
● unlockTime stores the time (in milliseconds) when the door was unlocked.
● autoLockDelay is the delay after which the door locks itself.
4. Setup Function:
void setup() {
Serial.begin(9600); // Start serial communication for debugging
servo.attach(6); // Attach the servo motor to pin 6
servo.write(70); // Move servo to the locked position
lcd.init(); // Initialize the LCD display
lcd.backlight(); // Turn on the LCD backlight
SPI.begin(); // Initialize SPI communication for RFID
rfid.PCD_Init(); // Initialize the RFID reader
}
● Servo: The door starts in the locked position (70 degrees on the servo motor).
● LCD: Displays messages to the user.
● RFID Reader: Gets ready to scan RFID cards.
5. Auto-Lock Logic:
● If the door is unlocked (lock == 0) and 10 seconds have passed, the servo
moves to the locked position (70 degrees).
● A message ("Auto-locked") is displayed on the LCD.
if (!rfid.PICC_IsNewCardPresent())
return;
if (!rfid.PICC_ReadCardSerial())
return;
● The RFID reader checks if a new card is placed on it. If no card is detected, the
loop continues.
7. UID Extraction:
● The program reads the RFID card's UID and converts it into a string format for
comparison with the authorized UID.
8. UID Verification:
● If the card matches the authorized UID and the door is unlocked, the door locks.
● If the card matches the authorized UID and the door is locked, the door unlocks.
● If the card is unauthorized, the LCD shows "Wrong card!".
9. Lock and Unlock Actions:
● Lock the Door:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong card!");
delay(1500);
lcd.clear();
● If an unauthorized card is scanned, the LCD displays "Wrong card!" for 1.5
seconds.
Full Code
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
servo.attach(6); // Attach servo to pin 6 (Mega-specific)
servo.write(70); // Set servo to locked position
lcd.init();
lcd.backlight();
SPI.begin();
rfid.PCD_Init(); // Initialize the RFID reader
// Confirm initialization
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is locked");
delay(1500); // Display initial state for 1.5 seconds
lcd.clear();
}
void loop() {
// Display welcome message on the LCD
lcd.setCursor(4, 0);
lcd.print("Welcome!");
lcd.setCursor(1, 1);
lcd.print("Put your card");
// Auto-lock logic
if (lock == 0 && (millis() - unlockTime >= autoLockDelay)) {
servo.write(70); // Lock the door
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Auto-locked");
delay(1500);
lcd.clear();
lock = 1; // Update lock status
}