Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino RFID Door Lock System with Alarm and Access Control

Image of Arduino RFID Door Lock System with Alarm and Access Control

Circuit Documentation

Summary of the Circuit

This circuit is designed to control a door locking system using RFID technology. It features an Arduino UNO microcontroller interfaced with an RFID-RC522 module to read RFID tags. The system grants or denies access based on the tag presented. A green LED indicates authorized access, while a red LED and a buzzer signal unauthorized access. The system also includes a relay to control a magnetic lock, which secures the door. A 5V adapter powers the Arduino, and a separate 12V power supply operates the relay and magnetic lock. The system has an alarm feature that activates after multiple failed access attempts and can lock down for a specified duration as a security measure.

Component List

Arduino UNO

  • Microcontroller board based on the ATmega328P
  • Used as the main controller for the RFID door lock system

RFID-RC522

  • RFID reader/writer module
  • Interacts with RFID tags for access control

LED: Two Pin (green)

  • Indicates authorized access when lit

LED: Two Pin (red)

  • Indicates unauthorized access or an alarm condition when lit

Buzzer

  • Emits sound for feedback on access attempts

5V Adapter

  • Provides power to the Arduino UNO

Magnetic Lock

  • Electromagnetic locking mechanism for the door

12V Relay

  • Electrically operated switch that controls the magnetic lock

12V Power Supply

  • Provides power to the relay and magnetic lock

Wiring Details

Arduino UNO

  • 3.3V connected to RFID-RC522 VCC (3.3V)
  • GND connected to 5V Adapter GND, RFID-RC522 GND, green LED anode, red LED anode, and buzzer GND
  • Vin connected to 5V Adapter 5V
  • D13 connected to RFID-RC522 SCK
  • D12 connected to RFID-RC522 MISO
  • D11 connected to RFID-RC522 MOSI
  • D10 connected to RFID-RC522 SDA
  • D5 connected to red LED cathode
  • D4 connected to green LED cathode
  • D3 connected to 12V Relay IN
  • D2 connected to buzzer PIN

RFID-RC522

  • RST not connected
  • IRQ not connected

Green LED

  • cathode connected to Arduino UNO D4
  • anode connected to Arduino UNO GND

Red LED

  • cathode connected to Arduino UNO D5
  • anode connected to Arduino UNO GND

Buzzer

  • PIN connected to Arduino UNO D2
  • GND connected to Arduino UNO GND

5V Adapter

  • AC In 1 and AC In 2 not connected
  • 5V connected to Arduino UNO Vin
  • GND connected to Arduino UNO GND

Magnetic Lock

  • S connected to 12V power supply +
  • GND connected to 12V Relay NO

12V Relay

  • NO connected to Magnetic Lock GND
  • COM connected to 12V power supply -
  • NC not connected
  • DC+ connected to 12V power supply +
  • DC- connected to 12V power supply -
  • IN connected to Arduino UNO D3

12V Power Supply

  • + connected to 12V Relay DC+ and Magnetic Lock S
  • - connected to 12V Relay COM and DC-

Documented Code

/*
 * Ce sketch Arduino contrôle un système de verrouillage de porte utilisant RFID.
 * Le système lit les tags RFID et déverrouille la porte si le tag est autorisé.
 * Un buzzer émet des sons différents pour les tags acceptés et rejetés.
 * Deux exemples de tags RFID sont utilisés pour le contrôle d'accès.
 * Après 5 tentatives d'échecs, une alarme se déclenche pendant 1 minute.
 * Si après l'alarme, il y a encore 3 tentatives échouées, le système se bloque pendant 1 heure.
 */

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

#define RST_PIN 9
#define SS_PIN 10
#define RED_LED_PIN 5
#define GREEN_LED_PIN 4
#define RELAY_PIN 3
#define BUZZER_PIN 2
#define MAX_ATTEMPTS 5
#define ADDITIONAL_ATTEMPTS 3
#define ALARM_DURATION 60000 // 1 minute in milliseconds
#define LOCK_DURATION 3600000 // 1 hour in milliseconds

MFRC522 mfrc522(SS_PIN, RST_PIN);

// Exemples de tags RFID autorisés
byte authorizedUIDs[][4] = {
  {0xDE, 0xAD, 0xBE, 0xEF},
  {0xCA, 0xFE, 0xBA, 0xBE}
};
const int numAuthorizedUIDs = sizeof(authorizedUIDs) / sizeof(authorizedUIDs[0]);

int failedAttempts = 0;
int additionalFailedAttempts = 0;
bool isLocked = false;
unsigned long lockStartTime = 0;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Assurez-vous que le relais est éteint initialement
}

void loop() {
  if (isLocked) {
    if (millis() - lockStartTime >= LOCK_DURATION) {
      isLocked = false;
      additionalFailedAttempts = 0;
    } else {
      return; // Le système est bloqué
    }
  }

  if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  if (isAuthorized(mfrc522.uid.uidByte, mfrc522.uid.size)) {
    grantAccess();
    failedAttempts = 0; // Reset failed attempts on successful access
    additionalFailedAttempts = 0; // Reset additional failed attempts
  } else {
    denyAccess();
    failedAttempts++;
    if (failedAttempts >= MAX_ATTEMPTS) {
      triggerAlarm();
      failedAttempts = 0; // Reset failed attempts after alarm
      additionalFailedAttempts++;
      if (additionalFailedAttempts >= ADDITIONAL_ATTEMPTS) {
        lockSystem();
      }
    }
  }

  mfrc522.PICC_HaltA();
}

bool isAuthorized(byte *uid, byte uidSize) {
  if (uidSize != 4) return false;
  for (int i = 0; i < numAuthorizedUIDs; i++) {
    if (memcmp(uid, authorizedUIDs[i], 4) == 0) {
      return true;
    }
  }
  return false;
}

void grantAccess() {
  digitalWrite(GREEN_LED_PIN, HIGH);
  digitalWrite(RED_LED_PIN, LOW);
  digitalWrite(RELAY_PIN, HIGH);
  tone(BUZZER_PIN, 1000, 200); // Bip court pour tag accepté
  delay(5000); // Garder la porte déverrouillée pendant 5 secondes
  digitalWrite(RELAY_PIN, LOW);
  digitalWrite(GREEN_LED_PIN, LOW);
}

void denyAccess() {
  digitalWrite(RED_LED_PIN, HIGH);
  digitalWrite(GREEN_LED_PIN, LOW);
  tone(BUZZER_PIN, 500, 1000); // Bip long pour tag rejeté
  delay(1000);
  digitalWrite(RED_LED_PIN, LOW);
}

void triggerAlarm() {
  unsigned long startTime = millis();
  while (millis() - startTime < ALARM_DURATION) {
    tone(BUZZER_PIN, 2000); // Alarme sonore
    digitalWrite(RED_LED_PIN, HIGH); // LED rouge allumée
    delay(500);
    noTone(BUZZER_PIN);
    digitalWrite(RED_LED_PIN, LOW);
    delay(500);
  }
}

void lockSystem() {
  isLocked = true;
  lockStartTime = millis();
  Serial.println("Système bloqué pendant 1 heure en raison de tentatives d'accès échouées.");
}

This code is responsible for controlling the RFID door lock system. It initializes the RFID reader and sets up the pins for the LEDs, relay, and buzzer. The main loop checks for new RFID cards, verifies if they are authorized, and then grants or denies access accordingly. It also handles the alarm and system lock features based on the number of failed attempts.