Attendance System With Arduino & Rfid Tag
This is a project with MFRC522 RFID Reader and Arduino.
Components and supplies
1
DS3231M - ±5ppm, I2C Real-Time Clock
1
Arduino UNO
1
RFID reader (generic)
1
Buzzer
1
Flash Memory Card, SD Card
1
LED (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Code
arduino
code
1#include <MFRC522.h> // for the RFID 2#include <SPI.h> // for the RFID and SD card module 3#include <SD.h> // for the SD card 4#include <RTClib.h> // for the RTC 5 6// define pins for RFID 7#define CS_RFID 10 8#define RST_RFID 9 9// define select pin for SD card module 10#define CS_SD 4 11 12// Create a file to store the data 13File myFile; 14 15// Instance of the class for RFID 16MFRC522 rfid(CS_RFID, RST_RFID); 17 18// Variable to hold the tag's UID 19String uidString; 20 21// Instance of the class for RTC 22RTC_DS1307 rtc; 23 24// Define check in time 25const int checkInHour = 9; 26const int checkInMinute = 5; 27 28//Variable to hold user check in 29int userCheckInHour; 30int userCheckInMinute; 31 32// Pins for LEDs and buzzer 33const int redLED = 6; 34const int greenLED = 7; 35const int buzzer = 5; 36 37void setup() { 38 39 // Set LEDs and buzzer as outputs 40 pinMode(redLED, OUTPUT); 41 pinMode(greenLED, OUTPUT); 42 pinMode(buzzer, OUTPUT); 43 44 // Init Serial port 45 Serial.begin(9600); 46 while(!Serial); // for Leonardo/Micro/Zero 47 48 // Init SPI bus 49 SPI.begin(); 50 // Init MFRC522 51 rfid.PCD_Init(); 52 53 // Setup for the SD card 54 Serial.print("Initializing SD card..."); 55 if(!SD.begin(CS_SD)) { 56 Serial.println("initialization failed!"); 57 return; 58 } 59 Serial.println("initialization done."); 60 61 // Setup for the RTC 62 if(!rtc.begin()) { 63 Serial.println("Couldn't find RTC"); 64 while(1); 65 } 66 else { 67 // following line sets the RTC to the date & time this sketch was compiled 68 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 69 } 70 if(!rtc.isrunning()) { 71 Serial.println("RTC is NOT running!"); 72 } 73} 74 75void loop() { 76 //look for new cards 77 if(rfid.PICC_IsNewCardPresent()) { 78 readRFID(); 79 logCard(); 80 verifyCheckIn(); 81 } 82 delay(10); 83} 84 85void readRFID() { 86 rfid.PICC_ReadCardSerial(); 87 Serial.print("Tag UID: "); 88 uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + 89 String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]); 90 Serial.println(uidString); 91 92 // Sound the buzzer when a card is read 93 tone(buzzer, 2000); 94 delay(100); 95 noTone(buzzer); 96 97 delay(100); 98} 99 100void logCard() { 101 // Enables SD card chip select pin 102 digitalWrite(CS_SD,LOW); 103 104 // Open file 105 myFile=SD.open("DATA.txt", FILE_WRITE); 106 107 // If the file opened ok, write to it 108 if (myFile) { 109 Serial.println("File opened ok"); 110 myFile.print(uidString); 111 myFile.print(", "); 112 113 // Save time on SD card 114 DateTime now = rtc.now(); 115 myFile.print(now.year(), DEC); 116 myFile.print('/'); 117 myFile.print(now.month(), DEC); 118 myFile.print('/'); 119 myFile.print(now.day(), DEC); 120 myFile.print(','); 121 myFile.print(now.hour(), DEC); 122 myFile.print(':'); 123 myFile.println(now.minute(), DEC); 124 125 // Print time on Serial monitor 126 Serial.print(now.year(), DEC); 127 Serial.print('/'); 128 Serial.print(now.month(), DEC); 129 Serial.print('/'); 130 Serial.print(now.day(), DEC); 131 Serial.print(' '); 132 Serial.print(now.hour(), DEC); 133 Serial.print(':'); 134 Serial.println(now.minute(), DEC); 135 Serial.println("sucessfully written on SD card"); 136 myFile.close(); 137 138 // Save check in time; 139 userCheckInHour = now.hour(); 140 userCheckInMinute = now.minute(); 141 } 142 else { 143 Serial.println("error opening data.txt"); 144 } 145 // Disables SD card chip select pin 146 digitalWrite(CS_SD,HIGH); 147} 148 149void verifyCheckIn(){ 150 if((userCheckInHour < checkInHour)||((userCheckInHour==checkInHour) && (userCheckInMinute <= checkInMinute))){ 151 digitalWrite(greenLED, HIGH); 152 delay(2000); 153 digitalWrite(greenLED,LOW); 154 Serial.println("You're welcome!"); 155 } 156 else{ 157 digitalWrite(redLED, HIGH); 158 delay(2000); 159 digitalWrite(redLED,LOW); 160 Serial.println("You are late..."); 161 } 162} 163
Code
arduino
code
1#include <MFRC522.h> // for the RFID 2#include <SPI.h> // for the RFID and SD card module 3#include <SD.h> // for the SD card 4#include <RTClib.h> // for the RTC 5 6// define pins for RFID 7#define CS_RFID 10 8#define RST_RFID 9 9// define select pin for SD card module 10#define CS_SD 4 11 12// Create a file to store the data 13File myFile; 14 15// Instance of the class for RFID 16MFRC522 rfid(CS_RFID, RST_RFID); 17 18// Variable to hold the tag's UID 19String uidString; 20 21// Instance of the class for RTC 22RTC_DS1307 rtc; 23 24// Define check in time 25const int checkInHour = 9; 26const int checkInMinute = 5; 27 28//Variable to hold user check in 29int userCheckInHour; 30int userCheckInMinute; 31 32// Pins for LEDs and buzzer 33const int redLED = 6; 34const int greenLED = 7; 35const int buzzer = 5; 36 37void setup() { 38 39 // Set LEDs and buzzer as outputs 40 pinMode(redLED, OUTPUT); 41 pinMode(greenLED, OUTPUT); 42 pinMode(buzzer, OUTPUT); 43 44 // Init Serial port 45 Serial.begin(9600); 46 while(!Serial); // for Leonardo/Micro/Zero 47 48 // Init SPI bus 49 SPI.begin(); 50 // Init MFRC522 51 rfid.PCD_Init(); 52 53 // Setup for the SD card 54 Serial.print("Initializing SD card..."); 55 if(!SD.begin(CS_SD)) { 56 Serial.println("initialization failed!"); 57 return; 58 } 59 Serial.println("initialization done."); 60 61 // Setup for the RTC 62 if(!rtc.begin()) { 63 Serial.println("Couldn't find RTC"); 64 while(1); 65 } 66 else { 67 // following line sets the RTC to the date & time this sketch was compiled 68 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 69 } 70 if(!rtc.isrunning()) { 71 Serial.println("RTC is NOT running!"); 72 } 73} 74 75void loop() { 76 //look for new cards 77 if(rfid.PICC_IsNewCardPresent()) { 78 readRFID(); 79 logCard(); 80 verifyCheckIn(); 81 } 82 delay(10); 83} 84 85void readRFID() { 86 rfid.PICC_ReadCardSerial(); 87 Serial.print("Tag UID: "); 88 uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + 89 String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]); 90 Serial.println(uidString); 91 92 // Sound the buzzer when a card is read 93 tone(buzzer, 2000); 94 delay(100); 95 noTone(buzzer); 96 97 delay(100); 98} 99 100void logCard() { 101 // Enables SD card chip select pin 102 digitalWrite(CS_SD,LOW); 103 104 // Open file 105 myFile=SD.open("DATA.txt", FILE_WRITE); 106 107 // If the file opened ok, write to it 108 if (myFile) { 109 Serial.println("File opened ok"); 110 myFile.print(uidString); 111 myFile.print(", "); 112 113 // Save time on SD card 114 DateTime now = rtc.now(); 115 myFile.print(now.year(), DEC); 116 myFile.print('/'); 117 myFile.print(now.month(), DEC); 118 myFile.print('/'); 119 myFile.print(now.day(), DEC); 120 myFile.print(','); 121 myFile.print(now.hour(), DEC); 122 myFile.print(':'); 123 myFile.println(now.minute(), DEC); 124 125 // Print time on Serial monitor 126 Serial.print(now.year(), DEC); 127 Serial.print('/'); 128 Serial.print(now.month(), DEC); 129 Serial.print('/'); 130 Serial.print(now.day(), DEC); 131 Serial.print(' '); 132 Serial.print(now.hour(), DEC); 133 Serial.print(':'); 134 Serial.println(now.minute(), DEC); 135 Serial.println("sucessfully written on SD card"); 136 myFile.close(); 137 138 // Save check in time; 139 userCheckInHour = now.hour(); 140 userCheckInMinute = now.minute(); 141 } 142 else { 143 Serial.println("error opening data.txt"); 144 } 145 // Disables SD card chip select pin 146 digitalWrite(CS_SD,HIGH); 147} 148 149void verifyCheckIn(){ 150 if((userCheckInHour < checkInHour)||((userCheckInHour==checkInHour) && (userCheckInMinute <= checkInMinute))){ 151 digitalWrite(greenLED, HIGH); 152 delay(2000); 153 digitalWrite(greenLED,LOW); 154 Serial.println("You're welcome!"); 155 } 156 else{ 157 digitalWrite(redLED, HIGH); 158 delay(2000); 159 digitalWrite(redLED,LOW); 160 Serial.println("You are late..."); 161 } 162} 163
Downloadable files
Schematics
Schematics
Schematics

Documentation
Code
Code
Code
Code
Code
Code
Comments
Only logged in users can leave comments