RFID Door Lock with Arduino

Configurare noua (How To)

Situatie

In this detailed guide, you’ll learn how to build an RFID door lock system using an Arduino Uno and an MFRC522 RFID reader. The system reads RFID cards or keyfobs, checks authorization, and controls a solenoid lock or servo latch to grant access. It’s perfect for home automation, maker projects, or educational demonstrations.

Solenoid Lock Wiring

Arduino D7 → Gate of MOSFET (via 100Ω resistor)
MOSFET Source → GND (common)
MOSFET Drain → Solenoid negative terminal
Solenoid positive → +12V
12V supply GND → Arduino GND
Flyback diode across solenoid (1N4007, cathode to +12V)

Optional Servo Connection

Servo Signal → D7 (PWM)
Servo V+ → 5V external supply
Servo GND → Common GND

Arduino Code — Basic Version

This version uses a hard-coded list of authorized RFID tags.

/* RFID Door Lock - Basic Version */
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

#define RST_PIN 9
#define SDA_PIN 10
MFRC522 rfid(SDA_PIN, RST_PIN);

#define ACTUATOR_TYPE 0  // 0=Solenoid/Relay, 1=Servo
const int RELAY_PIN = 7;
const int LED_PIN = 4;
const int BUZZER_PIN = 5;
const int SERVO_PIN = 7;
const unsigned long UNLOCK_MS = 3000;
Servo lockServo;

void setup() {
  Serial.begin(115200);
  SPI.begin();
  rfid.PCD_Init();
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  if (ACTUATOR_TYPE == 0) {
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW);
  } else {
    lockServo.attach(SERVO_PIN);
    lockServo.write(0);
  }
  Serial.println("RFID door lock ready");
}

// Replace these with your own card UIDs
byte allowedUIDs[][4] = {
  {0xDE, 0xAD, 0xBE, 0xEF},
  {0x11, 0x22, 0x33, 0x44}
};
const int allowedCount = sizeof(allowedUIDs)/4;

bool uidAllowed(byte *uid, byte uidSize) {
  if (uidSize != 4) return false;
  for (int i=0;i<allowedCount;i++) {
    bool match = true;
    for (int j=0;j<4;j++) if (allowedUIDs[i][j] != uid[j]) { match = false; break; }
    if (match) return true;
  }
  return false;
}

void unlockAction() {
  Serial.println("UNLOCK!");
  digitalWrite(LED_PIN, HIGH);
  tone(BUZZER_PIN, 1000, 150);
  if (ACTUATOR_TYPE == 0) {
    digitalWrite(RELAY_PIN, HIGH);
    delay(UNLOCK_MS);
    digitalWrite(RELAY_PIN, LOW);
  } else {
    lockServo.write(90);
    delay(UNLOCK_MS);
    lockServo.write(0);
  }
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  if (!rfid.PICC_IsNewCardPresent()) return;
  if (!rfid.PICC_ReadCardSerial()) return;

  Serial.print("Card UID:");
  for (byte i=0;i<rfid.uid.size;i++) {
    Serial.print(" ");
    Serial.print(rfid.uid.uidByte[i], HEX);
  }
  Serial.println();

  if (uidAllowed(rfid.uid.uidByte, rfid.uid.size)) {
    Serial.println("Access granted");
    unlockAction();
  } else {
    Serial.println("Access denied");
    for (int i=0;i<2;i++) {
      tone(BUZZER_PIN, 600, 150);
      digitalWrite(LED_PIN, HIGH);
      delay(200);
      digitalWrite(LED_PIN, LOW);
      delay(100);
    }
  }

  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}

Advanced Version — Store Tags in EEPROM

This version allows adding and removing authorized cards dynamically through the Serial Monitor.

/* RFID Door Lock - EEPROM Version */
#include <SPI.h>
#include <MFRC522.h>
#include <EEPROM.h>

#define RST_PIN 9
#define SDA_PIN 10
MFRC522 rfid(SDA_PIN, RST_PIN);

#define RELAY_PIN 7
#define LED_PIN 4
#define BUZZER_PIN 5
#define UNLOCK_MS 3000
#define MAX_TAGS 20
#define UID_SIZE 4
#define EEPROM_START 0

void setup() {
  Serial.begin(115200);
  SPI.begin();
  rfid.PCD_Init();
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  Serial.println("RFID lock with EEPROM ready");
}

/* Helper functions omitted for brevity in this preview — see full code in guide */

Serial commands:

  • l — List stored tags
  • a — Add a new tag (scan after command)
  • r — Remove a tag
  • c — Clear all stored tags

Power Considerations

  • Use a separate 12V supply for the solenoid; common the grounds.
  • Add a large capacitor (470µF–2200µF) across the solenoid supply.
  • Always include a flyback diode to protect electronics.
  • If using a servo, power it from an external 5V supply.

Mechanical Installation

  • Ensure alignment between lock and strike plate.
  • Provide a manual override or emergency key.
  • Mount RFID reader within 5–10 cm of tag presentation area.
  • Keep metal objects away from the RC522 antenna.

Testing Steps

  1. Upload code and open Serial Monitor (115200 baud).
  2. Scan a card — its UID should appear.
  3. Add card to allowed list or EEPROM memory.
  4. Test actuator operation and timing.
  5. Confirm power supply stability under load.

Troubleshooting

Issue Possible Cause Fix
No response from reader Wrong wiring or 5V used Use 3.3V and correct SPI pins
Actuator not moving Power supply too weak or wrong driver Use proper MOSFET/relay and check GND
UIDs print gibberish Wrong Serial baud rate Match Serial.begin(115200)
Unstable lock Power noise Add capacitor or separate supply

Security Enhancements

  • Use MIFARE DESFire or NTAG cards for better encryption.
  • Add a keypad for two-factor access (RFID + PIN).
  • Implement attempt-limit lockout for brute-force protection.
  • Use metal enclosure and tamper switch for extra security.

Optional Upgrades

  • OLED/LCD display for user feedback
  • Wi-Fi / MQTT integration (ESP8266 or ESP32)
  • RTC + SD card logging for audit trail
  • Battery backup for power loss protection
  • Cloud dashboard or mobile control app.

Solutie

Tip solutie

Permanent

Voteaza

(1 din 2 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?