0% found this document useful (0 votes)
18 views24 pages

Project - Rfid Door Lock

The document outlines a project for creating an RFID-based door lock system using Arduino, which includes components like an LCD, servo motor, and RFID module. It details the learning objectives, project setup, wiring connections, and programming steps necessary to implement the system. The project aims to enhance understanding of Arduino programming and IoT security concepts by allowing authorized access control through RFID authentication.

Uploaded by

Nurul Afiqah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views24 pages

Project - Rfid Door Lock

The document outlines a project for creating an RFID-based door lock system using Arduino, which includes components like an LCD, servo motor, and RFID module. It details the learning objectives, project setup, wiring connections, and programming steps necessary to implement the system. The project aims to enhance understanding of Arduino programming and IoT security concepts by allowing authorized access control through RFID authentication.

Uploaded by

Nurul Afiqah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Arduino

NAME : ___________________________

CLASS : ____________________________

Content by:

www.aimsity.com Aimsity
Table of Content

ARDUINO BOARD........................................................................................................................3

PARTS IN YOUR KIT.................................................................................................................... 4

PROJECT : RFID DOOR LOCK................................................................................................... 6

ARDUINO COMPONENT..............................................................................................................7

Project outcome...........................................................................................................................9

Step 1 : Connect the LCD..........................................................................................................10

Step 2: Connect the Servo Motor............................................................................................. 11

Step 3 : Connect the RFID Module........................................................................................... 12

Step 4 : Open software and connect arduino to laptop......................................................... 13

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

LCD 12C Arduino Mega Pin

SDA Pin 20

SCL Pin 21

VCC 5V

GND GND
Step 2: Connect the Servo Motor

Servo motor Arduino Mega Pin


Signal Pin 6
VCC 5V
GND GND
Step 3 : Connect the RFID Module

RFID Module Arduino Mega pin

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

1. Open your ‘Arduino IDE’

Step 1: Start the Arduino IDE

● Turn on your laptop and connect your Arduino Mega to


the USB port.
● Locate the Arduino IDE icon on your desktop or in your
applications menu and double-click it to open.

Step 2: Create a New Sketch

● In the Arduino IDE, click on File in the top menu.


● Select New Sketch from the dropdown menu.

Step 3: Select the Board

● Click on Tools in the top menu.


● Navigate to Board, then select Arduino Mega from the list.

Step 4: Select the Port

● Click on Tools in the top menu.


● Navigate to Port, then choose the port labeled as COM ‘#’ (where ‘#’ is
any number that the port assigned to your Arduino).

Step 5: Select the Programmer

● Click on Tools in the top menu.


● Navigate to Programmer, then select AVR ISP from the list.
Step 5 : Now let’s create the program for scanning RFID tags.

#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 49
#define SS_PIN 53

LiquidCrystal_I2C lcd(0x27, 16, 2);


MFRC522 mfrc522(SS_PIN, RST_PIN);

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;

lcd.clear(); lcd.print("Scanned UID:");


Serial.print("UID: ");
for (uint8_t i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i], HEX); Serial.print(" ");
lcd.setCursor(i * 3, 1); lcd.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
mfrc522.PICC_HaltA();
}
This code will show you your UID of your card and tag .
Example :

But first you need to open your Serial monitor in Arduino IDE.
After that you will have this screen.

Step 5 : Start Coding


1. Library Inclusions:
#include <Servo.h> // Controls the servo motor.
#include <LiquidCrystal_I2C.h> // Controls the LCD display.
#include <SPI.h> // Handles communication with the RFID reader.
#include <MFRC522.h> // Controls the RFID reader.

Servo Library (#include <Servo.h>)

● Purpose: Controls servo motors by setting their angles.


● Why It's Needed: Allows you to easily move the servo to lock/unlock the door.
● Example: servo.write(70); rotates the servo to 70 degrees.

LiquidCrystal_I2C Library (#include <LiquidCrystal_I2C.h>)

● 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.

MFRC522 Library (#include <MFRC522.h>)

● Purpose: Controls the MFRC522 RFID reader to read RFID cards.


● Why It's Needed: Reads the card's UID to determine if the card is authorized.
● Example: rfid.PICC_ReadCardSerial(); reads the data from the RFID
card.

2. Pin Definitions and Variables:


#define SS_PIN 53
#define RST_PIN 49
String UID = "93 51 86 0F"; // The UID of the authorized card
byte lock = 1; // Door starts in the locked state (1 = locked, 0 =
unlocked)

● Change the UID with your UID number.

● 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 (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 the lock status
}

● 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.

6. RFID Card Detection:

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:

String ID = ""; // Initialize an empty string


for (byte i = 0; i < rfid.uid.size; i++) {
ID += String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
ID += String(rfid.uid.uidByte[i], HEX);
}
ID.toUpperCase(); // Convert the UID to uppercase

● 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 (ID.substring(1) == UID && lock == 0) {


// Lock the door
} else if (ID.substring(1) == UID && lock == 1) {
// Unlock the door
} else {
// Display "Wrong card!"
}

● 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:

servo.write(70); // Move servo to the locked position


lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is locked");
lock = 1; // Update lock status

● The servo rotates to lock the door.


● The lock status is updated to 1 (locked).

● Unlock the Door:

servo.write(160); // Move servo to the unlocked position


lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is open");
lock = 0; // Update lock status
unlockTime = millis(); // Start the auto-lock timer

● The servo rotates to unlock the door.


● The lock status is updated to 0 (unlocked).
● The unlockTime is recorded for the auto-lock timer.
10. Handle Unauthorized Cards:

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>

#define SS_PIN 53 // Use Mega's pin 53 for SS


#define RST_PIN 49 // Use pin 49 for RST
String UID = "93 51 86 0F"; // Authorized UID
byte lock = 1; // Initialize as locked (1)

Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rfid(SS_PIN, RST_PIN);

unsigned long unlockTime = 0; // Variable to store the time when the


door was unlocked
const unsigned long autoLockDelay = 10000; // 10 seconds delay for
auto-lock (in milliseconds)

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
}

// Check if a new card is present


if (!rfid.PICC_IsNewCardPresent())
return;
if (!rfid.PICC_ReadCardSerial())
return;

// Scanning the card


lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scanning");
Serial.print("NUID tag is : "); // Begin serial output for UID

String ID = ""; // Initialize an empty string to store the UID


for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); // Add
leading zero if needed
Serial.print(rfid.uid.uidByte[i], HEX); // Print UID byte in
hexadecimal
ID += String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
ID += String(rfid.uid.uidByte[i], HEX);
lcd.print("."); // Show progress on LCD
delay(300); // Simulate scanning delay
}
Serial.println(); // End the line after printing the UID
ID.toUpperCase(); // Convert the ID string to uppercase

// Check if the scanned UID matches the authorized UID


if (ID.substring(1) == UID && lock == 0) {
servo.write(70); // Lock the door
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is locked");
delay(1500);
lcd.clear();
lock = 1; // Update lock status
} else if (ID.substring(1) == UID && lock == 1) {
servo.write(160); // Unlock the door
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is open");
delay(1500);
lcd.clear();
lock = 0; // Update lock status
unlockTime = millis(); // Record the time the door was unlocked
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong card!");
delay(1500);
lcd.clear();
}

rfid.PICC_HaltA(); // Halt communication with the card


}

You might also like