Build an RFID Door Lock with Arduino
Tap the right card and the door unlocks; tap a wrong card and it stays locked. This RFID project teaches SPI communication and is a favourite for final-year and school projects.
What You Need:
- 1x Arduino Uno R3
- 1x RFID-RC522 Kit (reader, card and tag)
- 1x SG90 Servo (as the lock latch) or a 5V Relay for a 12V solenoid lock
- 1x Green and red LEDs with 220Ω resistors
- Jumper Wires
1. Wiring (RC522 runs on 3.3V!)
- SDA → D10, SCK → D13, MOSI → D11, MISO → D12, RST → D9
- 3.3V → 3.3V (not 5V), GND → GND
- Servo signal → D3, green LED → D4, red LED → D5
2. Find Your Card ID
Install the MFRC522 library, upload the code, open Serial Monitor at 9600 and tap your card. Copy the printed ID into allowed.
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
MFRC522 rfid(10, 9);
Servo lock;
String allowed = "A1 B2 C3 D4"; // replace with your card ID
void setup() {
Serial.begin(9600);
SPI.begin(); rfid.PCD_Init();
lock.attach(3); lock.write(0);
pinMode(4, OUTPUT); pinMode(5, OUTPUT);
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
String id = "";
for (byte i = 0; i < rfid.uid.size; i++) {
if (rfid.uid.uidByte[i] < 0x10) id += "0";
id += String(rfid.uid.uidByte[i], HEX);
if (i < rfid.uid.size - 1) id += " ";
}
id.toUpperCase();
Serial.println(id);
if (id == allowed) {
digitalWrite(4, HIGH); lock.write(90); // unlock
delay(3000);
lock.write(0); digitalWrite(4, LOW); // lock again
} else {
digitalWrite(5, HIGH); delay(1000); digitalWrite(5, LOW);
}
rfid.PICC_HaltA();
}Add a DS3231 RTC later to log the time of each entry.
Need help? Stuck on wiring or code? Message us on WhatsApp at +880 1577-098376 and we will help you with your project.
