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:
- Arduino UNO – read Best Arduino Starter Kits
- MFRC522 RFID reader + tags
- SD card module
- Micro SD card
- SD1307 RTC module
- 2x LEDs (1x red + 1x green)
- 2x 220 Ohm resistor
- Breadboard
- Jumper wires
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:
- Click here to download the MFRC522 library. You should have a .zip folder in your Downloads folder.
- Unzip the .zip folder and you should get RFID-master folder
- Rename your folder from
RFID-master to RFID - Move the RFID folder to your Arduino IDE installation libraries folder
- Finally, re-open your Arduino IDE
MFRC522 RFID reader pinout
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:
- Click here to download the RTCLib library. You should have a .zip folder in your Downloads folder.
- Unzip the .zip folder and you should get RTCLib-master folder
- Rename your folder from
RTCLib-master to RTCLib - Move the RTCLib folder to your Arduino IDE installation libraries folder
- 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.
/*
* 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 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.
how to send or save data to mysql server? thank you very much
Hi Abu,
Thanks for the project suggestion and I’ll try to make something like that in the future.
Regards,
Rui
Nice tutorial, an enhancement would be to do this with a ES8266 or ESP32 and send data direkt o a Database
Thanks for the project suggestion Rainer!
Good day. We have a project like that but inour project we have a Mobile application. May i ask how to conect it to Arduino?
Hi. We don’t have any specific project with that.
But we have some tutorials with Arduino and MIT App Inventor that can get you started build mobile applications to connect with Arduino.
You can get started with the following article:
https://randomnerdtutorials.com/getting-started-with-mit-app-inventor-2-and-arduino/
I hope this helps,
Regards,
Sara 🙂
good
There is a problem in the schematic and article. This will only work we connect a 330-ohm resistor from MISO of RFID to MISO of SD card, otherwise, we are getting the SD card initialization error.
Hi.
Our project worked just fine as it is (at least for us). Maybe you’re using another sd card module or something. But thank you for sharing your solution, it can help others with the same problem.
Regards,
Sara 🙂
Can you share your schema and code pls? i connect a 330-ohm resistor like you say and nothing happend just error to initialize the SD card
Can you plz tell me a code for RTC DS3231. This code is not working properly for me.
Hi.
What do you mean?
Do you get any errors on the serial monitor?
Yes its only say that SD card initialization done and nothing happens
same error for me also plz help
Hi! How do i directly send data to database? Thank you so much!
Unfortunately I don’t have any tutorials on that exact subject.
Thanks for asking. Regards,
Rui
HI,
I HAVE DS3231 RTC, KINDLY LET ME KNOW THE CHANGE WHICH I HAVE TO MAKE IN THIS CODE..OR KINDLY POST THE COMPLETE CODE WITH DS3231.
Hi.
I think you just need to replace line 27 on the code (github.com/RuiSantosdotme/Arduino-Projects/blob/master/code/25_Time_Attendance_System_with_RFID.ino) with the following:
RTC_DS3231 rtc;
I haven’t tested this. If it doesn’t work and you need more information on how to make it work with the DS3231 RTC, take a look at the following code:
github.com/adafruit/RTClib/blob/master/examples/ds3231/ds3231.ino
or this tutorial:
https://randomnerdtutorials.com/guide-for-real-time-clock-rtc-module-with-arduino-ds1307-and-ds3231/
Regards,
Sara
i have 5v sd card module , it is not working with rfid , kindly help
What do you mean by “it is not working with rfid”?
Regards,
Sara
Same problem, with 5v sd after “initialization done.” the RFID-module does not read the rfid tags
Hi, I have the same problem. Any solutions?
Im having the same issue. The serial monitor shows the SD card initializing but the RFID module will not read the UIDS. I also have 5v SD card and a 3.3 RFID card. Spent 2 days troubleshooting
Hi.
What are the exact components you are using?
The project works just fine for me. Maybe the newer modules are a bit different?
Hi,
i had se same problem with my project, it helped in my case to put a 330 ohm resistor between MISO RFID and MISO SD, as Surya Saini suggested
Hii, may I know how can I use LCD Display in this project?? please kindly reply
Hi.
This tutorial can be integrated with the LCD.
However, we don’t have any tutorial about that subject.
Regards,
Sara
Hi, thank you so much for sharing this project! My question is how much would it cost to buy all the required parts for this solution ?
Hi.
It really depends on where you get your parts from.
You can click the links on the Parts Required section and compare the prices on several stores like Amazon, ebay, aliexpress, Banggood, etc…
Or you can go to Maker Advisor tools to compare the prices.
Regards,
Sara 🙂
SD card initialization failed. is there a workaround without using a 330-ohm resistor?
Hi.
I’ve tested this project again and it works great with my SD card module.
Which SD card module are you using?
I’m using this one, as shown in this image: https://makeradvisor.com/wp-content/uploads/2017/11/micro-sd-card-module-img.jpg and it works great.
Maybe there are some incompatibilities with your SD card module.
Besides that error, are you getting other errors?
Regards,
Sara
I have the same situation, just show “initialization failed!” , somebody help me
Hi Andrew.
Which SD card module are you using?
Hi, I’m having problems to test the circuit. I am using a 5v SD card module, when I run de program the initializaction works but after that it doesn´t reads or whites anything. Do you know why?
Hi Giovanna.
Do you get any errors on the serial monitor?
Is your RFID reader working properly? If it doesn’t read properly, the microSD card will not be written.
I want to do something similar but just to know who is IN or OUT and time stamps. (In case of emergencies I need proper head count.)
I want to link 2 of these and be able to view logs remotely like ssh ir something. Can this be done with arduino or should I lean towards raspberry pi?
Hi.
It really depends on your project requirements. But taking into account what you’ve described, I would recommend a Raspberry Pi.
But it will depend on the functionalities you want in your project.
Regards,
Sara
Thanks for your sharing. I dont know why my RC522 not working together with SD card module.
What is the error that you are getting?
“Initializing SD card…initialization done” but RFID read nothing. RFID just read when i removed SD card module.
I use Catalex MicroSD card adapter, and it only works with 5V DC.
Hi all!
I was working on this project on my own, and after getting lost with the Slave Selection I decided to give a try to this one.
Here is my feedback:
1/ Problem: My SD card system sent me “initialisation failed” Solution: It worked after I formated the card and power it with 5V. Even if written 3.3V.
I have the CATALEX micro SD Card Adapter V1.0 11/01/13.
I heard that All SD Card modul can/should be plugged on 5V as they have stepdown integrated and may not work on 3.3V.
2/ I am using the DS3231 Problem: THere is not “is running” in the lib. So I replaced “RTC_DS1307 rtc;” by “RTC_DS3231 rtc;” and ” if(!rtc.isrunning()) {
Serial.println(“RTC is NOT running!”);
}” by /*if(!rtc.isrunning()) {
Serial.println(“RTC is NOT running!”);
}*/
3/ Problem, I get in void look (I added a srial print to see if i am in:
void loop() {
Serial.println(“Here 3”);
//look for new cards
and… I am in but my RFID module don’t detect my card. So I guess there is probable a problem with the Slave selection. I am Using a NanoV3.
I am stuck here. If you have a hint for me It’s welcome
Hi Elias.
Thank you for your feedback.
It can be helpful for other readers.
I think you’ve already found the answer for your problem. Thank you so much for sharing.
Regards,
Sara
Hey,
Ive the same problem. I’ve connected sd card to 5V but still “initialization failed!” and my rfid mfrs522 cant detect.
For RTClib.h it worked after changed to “RTC_DS3231 rtc.”
Hey,
I got it working by adding a 330Ohm between the SDCard Reader Miso and the Arduino 12 Pin. The RFID still is connected to 12 without Resistor.
Would be good to get feedbak from you all Nerds about this 🙂
Cheers!
hello ,
elias it’s work thank you
nerd 1
Hello,
I connected 3.3K resistor between SD card MISO and MISO of MEGA,but still not detecting card.
Your help will be appreciated!
welcome at the beginning forgive my English, it’s poor, I have read the projects posted on your website, they are all interesting and very well described, one of them – Arduino Time Attendance System with RFID == I was so interested that I decided to expand it a bit and added an oled display 1306, which shows the commands printed in the serial monitor, IDE, I changed it a bit and it works so that it only grants access to one IC card and blocks the rest, and I have a small problem here, I would like to add two or three IC cards that will have access and others not , but I don’t know how to add the remaining IC cards – {see the attached screen), I will be very grateful for your help, best regards Jerzy
I checked my wiring and code and everything is fine, but when test this I get this: ‘Initializing SD card….. initialization failed;”
What did I do wrong?
Hi.
Check if the microSD card is properly inserted on the slot.
Double-check the wiring.
Regards,
Sara
Hello , Im facing an error while uploading the code.
avrdude: ser_open(): can’t set com-state for “\.\COM5”
How can i resolve it?