/*
Wokwi | projects
HELP! me make rfid attendence system with
SKINNN Friday, November 28, 2025 9:24 AM
never mind done with it on my own
https://wokwi.com/projects/448874581428084737
*/
// -------------------- Libraries --------------------
#include <Wire.h> // For I2C Communication (RTC & LCD)
#include <RTClib.h> // For RTC Module (DS3231)
#include <LiquidCrystal_I2C.h> // For 16x2 I2C LCD
#include <Servo.h>
// -------------------- Pin Definitions --------------------
#define GRN_LED_PIN 7 // Digital Pin for Green LED (Access Granted)
#define RED_LED_PIN 6 // Digital Pin for Red LED (Access Denied)
#define BUZZER_PIN 5 // Digital Pin for 5V Buzzer
#define SERVO_PIN 2
// -------------------- Objects --------------------
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;
Servo servo;
// -------------------- Student Data --------------------
// Structure to hold student UID and Name
struct Student {
String uid;
String name;
};
// Define your authorized students here:
const Student authorizedStudents[] = {
{"BD 31 15 2B", "Student 1"},
{"C1 4A 92 1F", "Student 2"},
{"88 77 66 55", "Student 3"}
};
const int NUM_STUDENTS = sizeof(authorizedStudents) / sizeof(authorizedStudents[0]);
// -------------------- Functions --------------------
// Function to check if the scanned UID is authorized
String checkAuthorization(String inputUID) {
for (int i = 0; i < NUM_STUDENTS; i++) {
// Compare the input UID (case-insensitive) against the stored UIDs
if (inputUID.equalsIgnoreCase(authorizedStudents[i].uid)) {
return authorizedStudents[i].name; // Found a match, return the name
}
}
return ""; // No match found
}
void accessGranted(String studentName) {
DateTime now = rtc.now();
Serial.print("✅ Access Granted for: ");
Serial.print(studentName);
Serial.print(" at ");
char timeBuff[9];
snprintf(timeBuff, 9, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
Serial.println(timeBuff);
// Visual/Audio Feedback
digitalWrite(GRN_LED_PIN, HIGH);
tone(BUZZER_PIN, 1000, 200); // 1KHz beep for 200ms
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WELCOME");
lcd.setCursor(0, 1);
lcd.print(studentName);
servo.write(90);
delay(2000); // Keep message displayed for 2 seconds
servo.write(0);
digitalWrite(GRN_LED_PIN, LOW);
noTone(BUZZER_PIN);
}
void accessDenied(String uid) {
Serial.print("❌ Access Denied. UID: ");
Serial.println(uid);
// Visual/Audio Feedback
digitalWrite(RED_LED_PIN, HIGH);
tone(BUZZER_PIN, 500, 500); // 500Hz beep for 500ms
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WRONG ID!");
lcd.setCursor(0, 1);
lcd.print("ACCESS DENIED");
delay(2000); // Keep message displayed for 2 seconds
digitalWrite(RED_LED_PIN, LOW);
noTone(BUZZER_PIN);
}
void handleSerialInput() {
if (Serial.available() > 0) {
String inputUID = Serial.readStringUntil('\n');
inputUID.trim();
Serial.print("\n--- Card Scan Simulated --- ");
String studentName = checkAuthorization(inputUID);
if (studentName != "") {
accessGranted(studentName);
} else {
accessDenied(inputUID);
}
}
}
// -------------------- Main Logic --------------------
void setup() {
Serial.begin(9600);
Wire.begin();
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC! Please check I2C wiring (A4/A5).");
}
// OPTIONAL: Uncomment the line below ONCE to set the RTC time to the compile time.
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
servo.attach(SERVO_PIN);
// Initialize Pins
pinMode(GRN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(SERVO_PIN, OUTPUT);
servo.write(0);
// Set initial display
lcd.setCursor(0, 0);
lcd.print("RFID ATTENDANCE");
lcd.setCursor(4, 1);
lcd.print("Ready...");
Serial.println("\n--- WOKWI SIMULATION READY ---");
Serial.println("Enter a student UID (e.g., BD 31 15 2B) and press Enter to scan.");
delay(2000);
}
void loop() {
// 1. Handle Serial Input
handleSerialInput();
// 2. Display the clock and "SCAN CARD" prompt when the system is idle
char buffer[17];
DateTime now = rtc.now();
snprintf(buffer, 17, "%2d/%2d/%4d %2d:%02d", now.month(), now.day(), now.year(), now.hour(), now.minute());
lcd.setCursor(0, 0);
lcd.print(buffer);
// Display prompt on bottom line
lcd.setCursor(0, 1);
lcd.print(" SCAN CARD... ");
}