Overview:
Traditional ways of doing attendance logging include manually writing attendance by pen and paper. In this tutorial, an automated way of logging the attendance is introduced using RC522 RFID Kit and RTC (Real-Time Clock) module along with other components such as RGB module and a buzzer interfaced with the Arduino.
Hardware:
Software:
- Arduino IDE
Application Discussion
An attendance system is used to log what day and time a user enters (or exits) an establishment. Thus, this project will make use of a Real-Time Clock (RTC) module in order to check the date and time the user taps in the RFID card or keychain. This project will also integrate an RFID reader to read the details (identity) of the user stored in the RFID keychain and card.
Using RFID technology simplifies attendance logging compared to the manual process. This project, however, is rather simplistic and can be improved further since this is only a small-scale version and may not be as reliable as commercially available ones.
The modules discussed in this tutorial will only be the RFID components and the RTC module.
For the discussion on the RGB module and the RGB color values, check this blogpost.
For the discussion on the buzzer check this blogpost.
RC522 RFID Kit
The RFID kit found in the Arduino Upgraded Starter Kit contains the RC522 RFID Reader, RFID keychain tag and card tag, and male header pins.
The RC522 RFID Reader is an effective yet simple module. It is used in scanning RFID tags like cards and keychains. It has 8 pins with the following connections:
PIN |
ARDUINO PIN |
SDA |
Digital 10 |
SCK |
Digital 13 |
MOSI |
Digital 11 |
MISO |
Digital 12 |
IRQ |
Do not connect |
GND |
GND |
RST |
Digital 9 |
3.3V |
3.3V |
*Note: The RC522 has an operating voltage between 2.5 to 3.3 V thus, should be powered using the 3.3V pin
**Note: Different Arduino boards have different SPI Pins, if you are not using an Arduino Uno, check this Arduino documentation.
RTC Module (DS1302)
The Real-Time Clock (RTC) module is used to keep track of date and time using an internal clock. The time will only be needed to be set once and the long-lasting battery will keep the mechanism running. The DS1302 module found in the Arduino Upgraded Starter Kit comes with a 3V Lithium-ion battery. The battery can last up to 5 years due to the DS1302 chip being very power efficient. The supply voltage for the RTC module is 5V.
Hardware Setup
Set Time for RTC Module
The RTC Module uses the I2C communication protocol similar to the LCD Module, this protocol is further discussed in this blogpost.
In using I2C, interfacing with the microcontroller becomes quite easy. Aside from the VCC and GND pins, only the CLK and DATA pins are required to interface with the Arduino (these pins are the SCL and SDA pins of the I2C communication). Finally, the RST pin is for RESET.
The Arduino specifically uses the pins A4 and A5 for the I2C protocol. However, with the help of a software library, any pin can be utilized. This library is discussed in the Software Setup section on this blogpost.
**Make sure to check the software setup first and install the library before proceeding to set the time for the RTC module.
Now that the library has been added to the Arduino IDE we can finally proceed with setting the time for the RTC module.
Upload the code below on the Arduino, this can also be found on the Github repository here.
This code makes use of the library mentioned in the Software Setup.
#include <virtuabotixRTC.h>
// Wiring construct: CLK =4 | DAT=7 | RST=8
// Change pin number according to the pins used
virtuabotixRTC myRTC(4, 7, 8); //Instance of the virtuabotixRTC class
void setup() {
Serial.begin(9600);
// Setup for the RTC
// Construct: setDS1302Time(seconds, minutes, hours, day of the week, day of the month, month, year)
// myRTC.setDS1302Time(10, 33, 23, 4, 23, 12, 2020); //Set to actual date/time
//Remove or comment function above once date/time is set. This is done only once
}
void loop() {
// This allows for the update of variables for time or accessing the individual elements.
myRTC.updateTime();
Serial.print("Current Date / Time: ");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
delay(1000);
}
Once the code is uploaded, open the serial monitor, the time is now set. Opening the serial monitor will show this similar output.
The time from the serial monitor is compared side by side by the time provided by my host computer.
*The setup code is also included in the main code, however, this portion specifically focuses on the DS1302 ONLY.
Set-up the Software
This project makes use of the libraries below:
- ArduinoRTCLibrary by chrisfryer78
- RFID MFRC522 by miguelbaboa
A guide on including libraries can be found here.
Finally, we can now upload the main code in our Arduino.
Code
The Arduino code can be found in Github here or you can copy the code below.
// for the RFID
#include
#include
#define SS_RFID 10 //Serial input pin
#define RST_RFID 9 //Reset pin
MFRC522 rfid(SS_RFID, RST_RFID); //Instance of the MFRC522 class
String uidString; // Set variable to hold card UID
// for the RTC
#include
// Wiring construct: CLK =4 | DAT=7 | RST=8
// Change pin number according to the pins used
virtuabotixRTC myRTC(4, 7, 8); //Instance of the virtuabotixRTC class
// for RGB module and buzzer || use PWM pins for RGB module
const int buz = 2;
const int blu_pin = 6;
const int red_pin = 5;
const int grn_pin = 3;
// for attendance time
const int atndceHour = 22;
const int atndceMin = 24;
//for user arrival time
int userArriveHour;
int userArriveMin;
void setup() {
//Set output pins
pinMode(red_pin, OUTPUT);
pinMode(grn_pin, OUTPUT);
pinMode(blu_pin, OUTPUT);
pinMode(buz, OUTPUT);
// Initialization
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
// Setup for the RTC
// Construct: setDS1302Time(seconds, minutes, hours, day of the week, day of the month, month, year)
//myRTC.setDS1302Time(10, 33, 23, 4, 23, 12, 2020); //Set to actual date/time
//Remove or comment function above once date/time is set. This is done only once
Serial.println("RTC is set!");
myRTC.updateTime();
datetime();
Serial.println();
}
void loop() {
//Continuously look for new cards
myRTC.updateTime();
RGB_color(0, 0, 255); //Blue
if(rfid.PICC_IsNewCardPresent()) {
datetime();
readRFID();
checkArrival();
}
delay(10);
}
void datetime(){
Serial.print("Current Date / Time: ");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
}
void readRFID() {
rfid.PICC_ReadCardSerial();
Serial.print("UID tag: ");
uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " +
String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
Serial.println(uidString);
// Buzzer sound when card is read
tone(buz, 2000);
delay(100);
noTone(buz);
Serial.print("Arrival Time: ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(": ");
//Store Arrival Time
userArriveHour = myRTC.hours;
userArriveMin = myRTC.minutes;
delay(100);
}
void checkArrival(){
if((userArriveHour < atndceHour)||((userArriveHour==atndceHour) && (userArriveMin <= atndceMin))){
RGB_color(0, 255, 0); // Green
delay(2000);
RGB_color(0, 0, 255); //Blue
Serial.println("You are just in time!");
Serial.println();
}
else{
RGB_color(255, 0, 0); // Red
delay(2000);
RGB_color(0, 0, 255); //Blue
Serial.println("Sorry, you arrived late");
Serial.println();
}
}
void RGB_color(int red_light_value, int green_light_value, int blue_light_value) { //RGB color set
analogWrite(red_pin, red_light_value);
analogWrite(grn_pin, green_light_value);
analogWrite(blu_pin, blue_light_value);
}
Code Breakdown
-
setup()
– In this function, the output pins are being set. Also, initialization and RTC set up is done. The RTC time can be set in this code too by removing the comments (//) from this code:
//myRTC.setDS1302Time(sec, min, hr, day of the week, day of the month, month, year);
-
loop()
– This function continuously looks for new cards, it will also call on 3 functions discussed below.
-
datetime()
– The date and time is printed on the Serial monitor whenever this function is called.
-
readRFID()
– Once the loop detects a new card, the readRFID function will check the UID tag of the RFID card or keychain being used. It will then store the hour and minute the card was swiped on the reader using the userArriveHour and userArriveMin variables.
-
checkArrival()
– Once the userArriveHour and userArriveMin has stored the time values, the checkArrival function then compares the latter values to the atndceHour and atndceMin values. The values in the atndceHour and atndceMin is the designated time the user can enter without being tardy.
-
RGB_color(int red_light_value, int green_light_value, int blue_light_value)
– This function is called when the RGB 3 color module is being set a certain color. This creates a shorter code when setting up which color you want to appear from the module. The values for different colors can be found on the application discussion.
– In this project the colors indicate the following: - BLUE - idle/reading
- GREEN - on time
- RED - late
Library functions used for RFID and RTC
- rfid.PCD_Init()
- rfid.PICC_IsNewCardPresent()
- rfid.PICC_ReadCardSerial()
- myRTC.updateTime()
- myRTC.timeunit
Demo Video
This video shows the following output in the serial monitor:
**In this example I set the atndnceHour to 22 and atndnceMin to 24 to show the two different instances when reading the RFID card
Conclusion
This project shows the possibility of using RFID technology in automating activities being done manually. In this case, this project presents an automated process in logging attendance using Arduino Uno. Discussions were also included on the utilized modules such as the RFID reader and RTC module.
This system is quite useful in presenting an overview on a simple attendance system. However, this cannot be used in larger-scale operations since this does not provide a saved history of users that are tapping in. Thus, the RFID cards/ keychain can be tapped repeatedly. Fortunately, this can still be improved by making use of an SD card module or storing data logs in a database.
For more fun projects and tutorials visit Createlabz here.
Reference
https://lastminuteengineers.com/how-rfid-works-rc522-arduino-tutorial/
https://www.14core.com/the-ds1302-real-time-clock-on-arduino/