About Me

RF(Radio Frequency) based Attendances system using Ardunio chip

Arduino Time Attendance System with RFID

In this project, you’re going to build a time attendance system with MFRC522 RFID Reader and Arduino. When you swipe an RFID tag next to the RFID reader, it saves the user UID and time in an SD card. It also shows if you are late or in time accordingly to a preset hour and minute.
Project Overview
Before getting started it’s important to layout the project main features:
  • It contains an RFID reader that reads RFID tags;
  • Our setup has a real-time clock module to keep track of time;
  • When the RFID reader reads an RFID tag, it saves the current time and the UID of the tag in an SD card;
  • The Arduino communicates with the SD card using an SD card module;
  • You can set a check-in time to compare if you are on time or late;
  • If you are on time, a green LED lights up, if you are late, a red LED lights up;
  • The system also has a buzzer that beeps when a tag is read.

Parts Required



Here’s a list of the required components for this project:
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

MFRC522 RFID Reader

In this project, we’re using the MFRC522 RFID reader and that’s the one we recommend you to get (although this project may also be compatible with other RFID readers).
RFID means radio-frequency identification. RFID uses electromagnetic fields to transfer data over short distances and it’s useful to identify people, to make transactions, etc.
An RFID system needs tags and a reader:
  • Tags are attached to the object to be identified, in this example, we have a keychain and an electromagnetic card. Some stores also use RFID tags in their products’ labels to identify them. Each tag has its own unique identification (UID).
  • The reader is a two-way radio transmitter-receiver that sends a signal to the tag and reads its response.
The MFRC522 RFID reader works at 3.3V and it can use SPI or I2C communication. The library we’re going to use to control the RFID reader only supports SPI, so that’s the communication protocol we’re going to use.
To learn more about the RFID reader with the Arduino read: Security Access using MFRC522 RFID Reader with Arduino

Installing the MFRC522 library

This project uses the MFRC522.h library to control the RFID reader. This library doesn’t come installed in Arduino IDE by default, so you need to install it. Go to  Sketch > Include library > Manage libraries and search for MFRC522 or follow the next steps:
  1. Click here to download the MFRC522 library. You should have a .zip folder in your Downloads folder.
  2. Unzip the .zip folder and you should get RFID-master folder
  3. Rename your folder from RFID-master to RFID
  4. Move the RFID folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

MFRC522 RFID reader pinout

The following table shows the reader pinout for a future reference:
PINWIRING TO ARDUINO UNO
SDADigital 10
SCKDigital 13
MOSIDigital 11
MISODigital 12
IRQDon’t connect
GNDGND
RSTDigital 9
3.3V3.3V
Note: different Arduino boards have different SPI pins. If you’re using another Arduino board, check the Arduino documentation.

SD card module

When a tag is read, its UID and time are saved on an SD card so that you can keep track of check-ins. There are different ways to use an SD card with the Arduino. In this project we’re using the SD card module shown in the figure below – it works with a micro SD card.
There are different models from different suppliers, but they all work similarly, using the SPI communication protocol. To communicate with the SD card we’re going to use a library called SD.h, that comes already installed in Arduino IDE by default.
To learn more about the SD card module with the Arduino read: Guide to SD Card Module with Arduino

Download our Free eBooks and Resources

SD card module pinout

The following table shows the SD card module pinout for a future reference:
PINWIRING TO ARDUINO UNO
VCC3.3V
CSDigital 4
MOSIDigital 11
CLKDigital 13
MISODigital 12
GNDGND
Note: different Arduino boards have different SPI pins. If you’re using another Arduino board, check the Arduino documentation.

Preparing the SD card

The first step when using the SD card module with Arduino is formatting the SD card as FAT16 or FAT32. Follow the instructions below.
1) To format the SD card, insert it in your computer. Go to My Computer and right-click on the SD card. Select Format as shown in the figure below.
2) A new window pops up. Select FAT32, press Start to initialize the formatting process and follow the onscreen instructions.
Report this ad

Testing the SD card module

(This step is optional. This is an additional step to make sure the SD card module is working properly.)
Insert the formatted SD card in the SD card module.
Connect the SD card module to the Arduino as shown in the circuit schematics below or check the pinout table.
Note: depending on the module you’re using, the pins may be placed in a different location.

Uploading CardInfo sketch

To make sure everything is wired correctly and the SD card is working properly, in the Arduino IDE window go to FileExamples > SD > CardInfo.
Upload the code to your Arduino board. Make sure you have the right board and COM port selected.
Open the Serial Monitor at a baud rate of 9600 and the SD card information will be displayed. If everything is working properly you’ll see a similar message on the serial monitor.
Report this ad

RTC (Real Time Clock) module

To keep track of time, we’re using the SD1307 RTC module. However, this project works just fine with the DS3231, which is very similar. One main difference between them is the accuracy. The DS3231 is much more accurate than the DS1307. The figure below shows the SD1307 model.
The module has a backup battery installed. This allows the module to retain the time, even when it’s not being powered up.
This module uses I2C communication and we’ll use the RTCLib.h library to read the time from the RTC.
To learn more about the DS1307 real-time clock with the Arduino read: Guide for Real-Time Clock (RTC) Module with Arduino (DS1307 and DS3231)

RTC module pinout

The following table shows the RTC module pinout for a future reference:
Report this ad
PINWIRING TO ARDUINO UNO
SCLA5
SDAA4
VCC5V (check your module datasheet)
GNDGND
Note: different Arduino boards have different I2C pins. If you’re using another Arduino board, check the Arduino documentation.

Installing the RTCLib library

To install the RTCLib.h go to  Sketch > Include library > Manage libraries and search for RTCLib or follow the next steps:
  1. Click here to download the RTCLib library. You should have a .zip folder in your Downloads folder.
  2. Unzip the .zip folder and you should get RTCLib-master folder
  3. Rename your folder from RTCLib-master to RTCLib
  4. Move the RTCLib folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Schematics

The circuit for this project is shown in the circuit schematics below.
In this circuit there are 3.3V and 5V devices, make sure you wire them correctly. Also, if you’re using different modules, check the recommend voltage before powering the circuit. Wire one module at a time and follow the pinout tables if needed.
Report this ad
Here’s how your circuit should look like after assembling.

Code

Upload the following code to your Arduino. Make sure you have the right Board and COM Port selected.
/*
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */

#include <MFRC522.h> // for the RFID
#include <SPI.h> // for the RFID and SD card module
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC

// define pins for RFID
#define CS_RFID 10
#define RST_RFID 9
// define select pin for SD card module
#define CS_SD 4 

// Create a file to store the data
File myFile;

// Instance of the class for RFID
MFRC522 rfid(CS_RFID, RST_RFID); 

// Variable to hold the tag's UID
String uidString;

// Instance of the class for RTC
RTC_DS1307 rtc;

// Define check in time
const int checkInHour = 9;
const int checkInMinute = 5;

//Variable to hold user check in
int userCheckInHour;
int userCheckInMinute;

// Pins for LEDs and buzzer
const int redLED = 6;
const int greenLED = 7;
const int buzzer = 5;

void setup() {
  
  // Set LEDs and buzzer as outputs
  pinMode(redLED, OUTPUT);  
  pinMode(greenLED, OUTPUT);
  pinMode(buzzer, OUTPUT);
  
  // Init Serial port
  Serial.begin(9600);
  while(!Serial); // for Leonardo/Micro/Zero
  
  // Init SPI bus
  SPI.begin(); 
  // Init MFRC522 
  rfid.PCD_Init(); 

  // Setup for the SD card
  Serial.print("Initializing SD card...");
  if(!SD.begin(CS_SD)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // Setup for the RTC  
  if(!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while(1);
  }
  else {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  if(!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
}

void loop() {
  //look for new cards
  if(rfid.PICC_IsNewCardPresent()) {
    readRFID();
    logCard();
    verifyCheckIn();
  }
  delay(10);
}

void readRFID() {
  rfid.PICC_ReadCardSerial();
  Serial.print("Tag UID: ");
  uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + 
    String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
  Serial.println(uidString);
 
  // Sound the buzzer when a card is read
  tone(buzzer, 2000); 
  delay(100);        
  noTone(buzzer);
  
  delay(100);
}

void logCard() {
  // Enables SD card chip select pin
  digitalWrite(CS_SD,LOW);
  
  // Open file
  myFile=SD.open("DATA.txt", FILE_WRITE);

  // If the file opened ok, write to it
  if (myFile) {
    Serial.println("File opened ok");
    myFile.print(uidString);
    myFile.print(", ");   
    
    // Save time on SD card
    DateTime now = rtc.now();
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.println(now.minute(), DEC);
    
    // Print time on Serial monitor
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.println(now.minute(), DEC);
    Serial.println("sucessfully written on SD card");
    myFile.close();

    // Save check in time;
    userCheckInHour = now.hour();
    userCheckInMinute = now.minute();
  }
  else {
    Serial.println("error opening data.txt");  
  }
  // Disables SD card chip select pin  
  digitalWrite(CS_SD,HIGH);
}

void verifyCheckIn(){
  if((userCheckInHour < checkInHour)||((userCheckInHour==checkInHour) && (userCheckInMinute <= checkInMinute))){
    digitalWrite(greenLED, HIGH);
    delay(2000);
    digitalWrite(greenLED,LOW);
    Serial.println("You're welcome!");
  }
  else{
    digitalWrite(redLED, HIGH);
    delay(2000);
    digitalWrite(redLED,LOW);
    Serial.println("You are late...");
  }
}

Note: double-check that you have the needed libraries installed.

Importing libraries

The code starts by importing the needed libraries. The MFRC522 for the RFID reader, the SD for the SD card module and the RTClib for the RTC. You also include the SPI library for SPI communication with the RFID and SD card module.
#include <MFRC522.h> // for the RFID
#include <SPI.h>     // for the RFID and SD card module
#include <SD.h>      // for the SD card
#include <RTClib.h>  // for the RTC

Preparing RFID reader, SD card and RTC

Then, you define the pins for the RFID reader and the SD card module. For the RFID, the SCK pin (CS_RFID) is connected to pin 10 and the RST pin (RST_RFID) is connected to pin 9. For the SD card module, the Chip Select pin (CS_SD) is connected to pin 4.


// define pins for RFID
#define CS_RFID 10
#define RST_RFID 9
// define chip select pin for SD card module
#define CS_SD 4
You create a File called myFile to store your data.
File myFile;
Then, you create an instance for the RFID and for the RTC:
// Instance of the class for RFID
MFRC522 rfid(CS_RFID, RST_RFID);

// Instance of the class for RTC
RTC_DS1307 rtc;

Variables

You create a string variable uidString that holds the UID tags.
String uidString;
The following lines create variables to define the check-in time hour and minute. In this case, we’re defining the check-in the hour to 9h05m AM. You can change the check-in time by changing these values:
// Define check in time
const int checkInHour = 9;
const int checkInMinute = 5;
You also need to create variables to hold the user’s check-in an hour. These variables will save the hour a certain UID tag was read. The following variables hold the check-in an hour and the check-in a minute.
//Variable to hold user check in
int userCheckInHour;
int userCheckInMinute;
Finally, you attribute the pin numbers to the LEDs and buzzer.
Report this ad
// Pins for LEDs and buzzer
const int redLED = 6;
const int greenLED = 7;
const int buzzer = 5;

setup()

Next, in the setup() you set the LEDs and buzzer as outputs.
// Set LEDs and buzzer as outputs
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(buzzer, OUTPUT);
After that, each module is initialed.

Functions

In this code you create 3 functions: readRFID()logCard() and verifyCheckIn().
The read RFID() function reads the tag UID, saves it in the uidString variable and displays it on the serial monitor. Also, when it reads the tag, the buzzer makes a beep sound.
The log card() function creates a file on your SD card called DATA.txt. You can edit the name of the file, if you want, on the following line.
myFile=SD.open("DATA.txt", FILE_WRITE);
Then, it saves the uidString (that holds the UID of the tag) on the SD card and the current time.
myFile.print(uidString);
// Save time on SD card
DateTime now = rtc.now();
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(',');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
It also saves the user check In the hour and minute in the following variables for further comparison with the predefined check-in time.
userCheckInHour = now.hour();
userCheckInMinute = now.minute();
The verifyCheckIn() function simply compares the user check-in time with the predefined check-in an hour and gives feedback accordingly. If the user is late, the red LED lights up; if the user is on time, the green LED lights up.

loop()

After studying the created functions, the loop() is pretty straightforward to understand.
First, the code checks if an RFID tag was swiped. If yes, it will read the RFID UID, log the UID and the time into the SD card, and then it will give feedback to the user by lighting up one of the LEDs.

Grabbing the SD Card data

To check the data saved on the SD card, remove it from the SD card module and insert it on your computer.
Open the SD card folder and you should have a file called DATA.txt.
Open the file using a text editor. You’ll have something as follows:
Notice that each value is separated by commas. This makes it easier if you want to import this data to Excel, Google Sheets, or other data processing software.

Post a Comment

0 Comments