Arduino RFID Tutorial & Implementation, RC522 Module Read Example Project

In this Arduino RFID tutorial we are playing with the RFID-RC522 Module in order to implement an RFID tag scanner / reader. I walk you through an example which allows the user to create an array of “master” cards which would open the door. Other cards are read by the system and give an appropriate output as well, indicating that the card was not correct for this door.

RF ID Tags & Their Use

RFID Tags are extremely common. Chances are, your workplace is using such a system. That being said, you may find that you won’t be able to clone your workplace tag by following this tutorial. Sorry to disappoint. What you will learn however is a way to utilize these inexpensive tags in your next home project. I certainly wouldn’t recommend using it to secure your home, but it will keep the “less knowledgeable” intruder out.arduino rfid

Hardware & Software for the Project

You are going to need a few things for the project; you can find them through the following links:

The full program we will be implementing in this project is available here: Arduino LCD Menu. The Arduino IDE used is version 1.6.9.

The RFID Arduino Library

The library, originally developed by Miguel Balboa will be used in our program. It allows the user to read from and write to an RFID tag. It is a very extensive library which is currently being supported by the open source community. More info can be found through the link below:
Arduino RFID Library for MFRC522: Click Here.

RFID Reader Implementation

#include "MFRC522.h"
MFRC522 rfid(10, 9);
MFRC522::MIFARE_Key key;
int LED_Green = 2;
int LED_Red = 3;
int scanStateFlag = 0;
#define RFID_AccessCards 1
String RFID_Correct[RFID_AccessCards] = {"2c:d7:c3:a5"};
unsigned long RFID_ScanTime;

The code begins by introducing library specific calls which initialize our resder on pins 9 and 10. I’m using two LEDs for the status of the read; they are connected on pins 2 and 3. For demonstration purposes, I’m only defining a single “master” card. The ID is stored in a simple string inside of an array.

void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(LED_Green, OUTPUT);
pinMode(LED_Red, OUTPUT);
digitalWrite(LED_Green,HIGH);
digitalWrite(LED_Red,HIGH);
}

The setup() function initializes serial logging, SPI used for the RFID reader and the outputs for the LEDs.

void loop() {
processStateFlag();
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
return;
}else {
String strID = "";
for (byte i = 0; i < 4; i++) { strID += (rfid.uid.uidByte[i] < 0x10 ? "0" : "") + String(rfid.uid.uidByte[i], HEX) + (i!=3 ? ":" : ""); } for(int i = 0; i < RFID_AccessCards; i++) { int found = 0; if (strID == RFID_Correct[i]) { scanStateFlag = 1; found = 1; } if (found == 0) { scanStateFlag = 2; } } RFID_ScanTime = millis(); //12000 Serial.print("Card key: "); Serial.println(strID); rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } }

The loop function is slightly more involved. It's used to read the RFID tag and then cycle through the different cards stored in the system in order to find a matching card. If that card is found, it will flag the first flag. If not, the second one is flagged.

void processStateFlag() {
if(scanStateFlag == 1) {
if(millis() - RFID_ScanTime < 2000) { digitalWrite(LED_Red,HIGH); digitalWrite(LED_Green,LOW); }else { digitalWrite(LED_Green,HIGH); scanStateFlag = 0; } }else if(scanStateFlag == 2) { if(millis() - RFID_ScanTime < 2000) { digitalWrite(LED_Green,HIGH); digitalWrite(LED_Red,LOW); }else { digitalWrite(LED_Red,HIGH); scanStateFlag = 0; } }else { } }

Processing flags in this program is somewhat tricky. The user must be notified that their card was properly or incorrectly read, thus it's important to show both states. It is also important to make sure that we aren't interrupting the flow. In other words, if you scanned the wrong card, you shouldn't have to wait for the delay to finish. The above function accomplishes exactly that.

Conclusion - Arduino RFID Tag Reading & Processing

This program is fairly straight forward and simple to understand. You can implement several features such as EEEProm logging and an LCD Display menu which would compliment our project.

Link to FULL SOFTWARE: Click Here

Thank you for reading & watching,
- EEEnthusiast

Leave a Reply

Your email address will not be published.

CommentLuv badge