🔒 RFID Door Lock 🔒
Overview
RFID (Radio Frequency Identification) door lock system allows you to unlock a door using an RFID card or key fob. The system reads the unique ID from the RFID tag and compares it against a predefined list of authorized IDs. If a match is found, it activates an electronic lock (like a solenoid lock or relay).
What You Need..
- Arduino Board (like Arduino Uno or Nano)
- RFID Reader (like MFRC522)
- RFID Cards or Key Fobs (small tags that unlock the door)
- Servo Motor (to unlock the door)
- Breadboard and Jumper Wires
- Power Supply (to power the Arduino and servo)
- LED (optional, for a light indicator)
- Buzzer (optional, for sound alerts)
Basic Idea
You will use the RFID reader to scan a card. If the card is on the list of approved cards, it will unlock the door using a servo motor.
RFID Door Lock System Pin Connections
Component | Pin Name | Arduino Pin |
---|---|---|
RFID Reader (MFRC522) | SDA (SS) | 10 |
RFID Reader (MFRC522) | SCK | 13 |
RFID Reader (MFRC522) | MOSI | 11 |
RFID Reader (MFRC522) | MISO | 12 |
RFID Reader (MFRC522) | RST | 9 |
RFID Reader (MFRC522) | VCC | 3.3V |
RFID Reader (MFRC522) | GND | GND |
Servo Motor | Signal Pin | 8 |
Servo Motor | VCC | 5V |
Servo Motor | GND | GND |
Circuit Diagram
Step-by-Step Instructions:
1. Install Required Libraries
You need the MFRC522 library to interact with the RFID reader. Install it via the Arduino IDE Library Manager:- Open Arduino IDE.
- Go to
Sketch
> Include Library
> Manage Libraries
.
- Search for MFRC522 and install it.
Sketch
> Include Library
> Manage Libraries
.Arduino Code
#include <SPI.h>#include <MFRC522.h>#include <Servo.h>#define SS_PIN 10#define RST_PIN 9#define SERVO_PIN 8MFRC522 rfid(SS_PIN, RST_PIN);Servo myServo;// Define authorized RFID card UIDbyte authorizedUID[] = {0xDE, 0xAD, 0xBE, 0xEF}; // Example UID (Replace with your own UID)void setup() {Serial.begin(9600);SPI.begin();rfid.PCD_Init();myServo.attach(SERVO_PIN);myServo.write(0); // Start with the lock closedSerial.println("Place your card near the reader...");}void loop() {// Check for new RFID cardsif (!rfid.PICC_IsNewCardPresent()) {return;}// Select one of the cardsif (!rfid.PICC_ReadCardSerial()) {return;}// Print UID to Serial MonitorSerial.print("UID tag : ");for (byte i = 0; i < rfid.uid.size; i++) {Serial.print(rfid.uid.uidByte[i], HEX);Serial.print(" ");}Serial.println();// Check if the scanned UID matches the authorized UIDif (compareUID(rfid.uid.uidByte, authorizedUID, rfid.uid.size)) {Serial.println("Access Granted!");myServo.write(90); // Unlock the doordelay(3000); // Keep it unlocked for 3 secondsmyServo.write(0); // Lock the door again} else {Serial.println("Access Denied!");}rfid.PICC_HaltA(); // Stop reading}bool compareUID(byte *uid, byte *authorizedUID, byte uidSize) {for (byte i = 0; i < uidSize; i++) {if (uid[i] != authorizedUID[i]) {return false; // UID does not match}}return true; // UID matches}
3. Upload the Code
- Connect your Arduino board to your computer.
- Select the appropriate board and port in the Arduino IDE.
- Click on the upload button to upload the code to the Arduino.
4. Testing the System
- Power On: Connect your Arduino to a power supply.
- Place the RFID Card: Bring your RFID card or key fob close to the RFID reader.
- Observe the Output: Check the Serial Monitor for messages about access granted or denied. The servo should turn to unlock the door if the card is authorized.
Explanation of the Code
- Libraries: The code includes necessary libraries for SPI communication and the MFRC522 RFID module.
- Initialization: The RFID reader is initialized, and the servo is attached to a specific pin.
- Loop Function: Continuously checks for new RFID cards and reads their UID.
- UID Comparison: The scanned UID is compared against a predefined authorized UID array.
- Servo Control: If access is granted, the servo unlocks the door for a few seconds before locking again.
Additional Features
You can expand the system with additional features, such as:
- Multiple Authorized Users: Use an array to store multiple authorized UIDs.
- Buzzer Feedback: Add a buzzer that sounds when access is granted or denied.
- Logging: Keep track of access attempts by logging UIDs to an SD card.
- Mobile App Control: Use Bluetooth or Wi-Fi to control the lock remotely.
0 Comments