/*
RFID Attendance System
Wokwi | questions
https://wokwi.com/projects/475693332032906241
Obed Mbuva — 11:36 AM 9/20/26
help me do coding
*/
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
const int BUZZ_PIN = 5;
const int GRN_LED_PIN = 6;
const int RED_LED_PIN = 7;
const int RFID_RST_PIN = 8;
const int RFID_CS_PIN = 9;
const int SD_CS_PIN = 10;
struct User {
String uid;
String name;
};
User users[] = {
{"04:01:02:03", "Obed Mbuva"}, // set as an attribute in diagram.json
{"11:22:33:44", "Mr. Green"},
{"55:66:77:88", "Mrs. Yellow"},
{"aa:bb:cc:dd", "Miss. Red"},
{"c0:ff:ee:99", "Mr. Coffee"}
};
int totUsers = sizeof(users) / sizeof(users[0]);
unsigned long prevTime = 0;
MFRC522 rfid(RFID_CS_PIN, RFID_RST_PIN);
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool checkCard() {
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
return true;
} else {
return false;
}
}
String findName(String uid) {
for (int i = 0; i < totUsers; i++) {
if (users[i].uid == uid) return users[i].name;
}
return "";
}
void showSplash() {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("RFID System");
lcd.setCursor(5, 1);
lcd.print("V1.0");
delay(3000);
lcd.clear();
}
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
rtc.begin();
lcd.init();
lcd.backlight();
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GRN_LED_PIN, OUTPUT);
pinMode(BUZZ_PIN, OUTPUT);
Serial.print("Registered users found: ");
Serial.println(totUsers);
showSplash();
}
void loop() {
char buffer[16];
String name = "";
//if (!rfid.PICC_IsNewCardPresent()) return;
//if (!rfid.PICC_ReadCardSerial()) return;
if (checkCard()) {
//Serial.println("Here");
String uid;
for (byte i = 0; i < rfid.uid.size; i++) {
uid += rfid.uid.uidByte[i] < 0x10 ? "0" : "";
uid += String(rfid.uid.uidByte[i], HEX);
if (i != rfid.uid.size - 1) uid += ':';
}
Serial.println(uid);
name = findName(uid);
if (name == "") {
lcd.clear();
lcd.print("Card Not");
lcd.setCursor(0, 1);
lcd.print("Registered");
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GRN_LED_PIN, LOW);
tone(BUZZ_PIN, 440, 500);
//delay(1500);
//return;
} else {
lcd.clear();
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GRN_LED_PIN, HIGH);
tone(BUZZ_PIN, 880, 500);
Serial.println(name);
lcd.print(name);
}
rfid.PICC_HaltA();
}
if (millis() - prevTime >= 1000) {
DateTime now = rtc.now();
// In the complete version, the user's current attendance state
// is checked: first scan = Time In; next scan = Time Out.
// The stored Time In and Time Out are then used to calculate
// the total duration and write the daily report.
snprintf(buffer, 16, "%2d:%02d:%02d ", now.hour(), now.minute(), now.second());
//lcd.clear();
//lcd.print(name);
lcd.setCursor(0, 1);
lcd.print(buffer);
prevTime = millis();
}
//delay(1500);
}Loading
mfrc522
mfrc522