Random Nerd Tutorials
Shares

Arduino Time Attendance System with RFID

Shares

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.

First, watch the video intro below


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 in 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).

  • 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

cloud-download

The following table shows the reader pinout for a future reference:

PIN WIRING TO ARDUINO UNO
SDA Digital 10
SCK Digital 13
MOSI Digital 11
MISO Digital 12
IRQ Don’t connect
GND GND
RST Digital 9
3.3V 3.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 figure below – it works with micro SD card.

There are different models from different suppliers, but they all work in a similar way, 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

SD card module pinout

The following table shows the SD card module pinout for a future reference:

PIN WIRING TO ARDUINO UNO
VCC 3.3V
CS Digital 4
MOSI Digital 11
CLK Digital 13
MISO Digital 12
GND GND

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 figure below.

2) A new window pops up. Select FAT32, press Start to initialize the formatting process and follow the onscreen instructions.

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 File> Examples > 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.

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:

PIN WIRING TO ARDUINO UNO
SCL A5
SDA A4
VCC 5V (check your module datasheet)
GND GND

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.

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.

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 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 hour. These variables will save the hour a certain UID tag was read. The following variables hold the check in hour and the check in minute.

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

Finally you attribute the pin numbers to the LEDs and buzzer.

// 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 readRFID() 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 logCard() 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 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 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.

Wrapping up

In this project you’ve learned how to use an RFID card reader and the SD card module with Arduino. You can modify this project to your own needs or you can use the functions created here in other projects that require data logging or reading RFID tags.

You can take this project further and add a display to give extra feedback to the user. You can associate a name with each UID and display the user name when the tag is read. You may want to take a look at some tutorials with the Arduino using displays:

This is an excerpt from our course “Arduino Step-by-step Projects“. If you like Arduino and you want to make more projects, we recommend downloading our course: Arduino Step-by-step Projects.


Learn ESP32 with Arduino IDE

This our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registrations are open, so SIGN UP NOW »

Leave a Comment:

Add Your Reply

⏰ Registrations are open ⏰
Sign up for "Learn ESP32 with Arduino IDE" Course