Repository navigation
Expand file tree
/
Copy pathrfid_security_system.ino
More file actions
87 lines (68 loc) · 1.8 KB
/
Copy pathrfid_security_system.ino
File metadata and controls
87 lines (68 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include "RTClib.h"
#include <Servo.h>
#define SS_PIN 10
#define RST_PIN 9
#define LED_PIN 3
#define SERVO_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN);
RTC_DS3231 rtc;
Servo doorServo;
// Replace with your actual authorized UID
byte authorizedUID[4] = {0xDE, 0xAD, 0xBE, 0xEF};
void setup() {
Serial.begin(9600);
SPI.begin();
Wire.begin();
mfrc522.PCD_Init();
rtc.begin();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
doorServo.attach(SERVO_PIN);
doorServo.write(0); // Locked position
Serial.println("RFID Smart Door Lock System Ready...");
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent()) return;
if (!mfrc522.PICC_ReadCardSerial()) return;
DateTime now = rtc.now();
bool accessGranted = true;
for (byte i = 0; i < 4; i++) {
if (mfrc522.uid.uidByte[i] != authorizedUID[i]) {
accessGranted = false;
break;
}
}
Serial.print("Card UID: ");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.print("Date & Time: ");
Serial.print(now.day());
Serial.print("/");
Serial.print(now.month());
Serial.print("/");
Serial.print(now.year());
Serial.print(" ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
if (accessGranted) {
Serial.println("Access Granted ✅");
digitalWrite(LED_PIN, HIGH);
doorServo.write(90); // Unlock (rotate servo)
delay(3000); // Keep unlocked for 3 seconds
doorServo.write(0); // Lock again
digitalWrite(LED_PIN, LOW);
} else {
Serial.println("Access Denied ❌");
}
Serial.println("--------------------------");
mfrc522.PICC_HaltA();
}