I recently get a chance to work on a project in which I have to use RFID reader to scan cards. In this project I have used it for for student attendance so I thought to share it on our blog so that other engineers could get benefit out it.
Let's first have a little introduction of RFID and then we will look into how to interface RC522 with Arduino. RFID is the abbreviation of Radio frequency identification. RFID modules use electromagnetic fields to transfer data between card and the reader. Different tags are attached to objects and when we place that object in front of the reader, the reader reads that tags.Another benefit of RFID is that it doesn't require to be in a line of sight to get detected. As in barcode, the reader has to be in the line of sight to the tag and then it can scan but in RFID there's no such restriction. So, let's get started with Interfacing of RFID RC522 with Arduino.
You should also read:
Now let's start with the interfacing of RFID RC522 with Arduino. There are many different RFID modules available in the market. The RFID module, which I am gonna use in this project, is RFID-RC522. Its quite easy to interface and works pretty fine. This module has total 8 pins as shown in the below figure:
#include <SPI.h> #include <MFRC522.h> #define RST_PIN 9 #define SS_PIN 10 MFRC522 mfrc522(SS_PIN, RST_PIN); void setup() { SPI.begin(); mfrc522.PCD_Init(); } void loop() { RfidScan(); } void dump_byte_array(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], HEX); } } void RfidScan() { if ( ! mfrc522.PICC_IsNewCardPresent()) return; if ( ! mfrc522.PICC_ReadCardSerial()) return; dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); }
#include <SPI.h> #include <MFRC522.h> #define RST_PIN 9 #define SS_PIN 10 MFRC522 mfrc522(SS_PIN, RST_PIN); int RfidNo = 0; void setup() { SPI.begin(); mfrc522.PCD_Init(); } void loop() { RfidScan(); } void dump_byte_array(byte *buffer, byte bufferSize) { Serial.print("~"); if(buffer[0] == 160){RfidNo = 1;Serial.print(RfidNo);} if(buffer[0] == 176){RfidNo = 2;Serial.print(RfidNo);} if(buffer[0] == 208){RfidNo = 3;Serial.print(RfidNo);} if(buffer[0] == 224){RfidNo = 4;Serial.print(RfidNo);} if(buffer[0] == 240){RfidNo = 5;Serial.print(RfidNo);} Serial.print("!"); while(1){getFingerprintIDez();} } void RfidScan() { if ( ! mfrc522.PICC_IsNewCardPresent()) return; if ( ! mfrc522.PICC_ReadCardSerial()) return; dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); }