MFRC522 is maybe the cheapest RFID Reader that we can find now. It uses a 13.56 MHz frequency And it is also easy to use. We can easily interface this reader with Arduino or raspberry. There are tons of libraries available out there. And one more, this tutorial can be the simplest tutorial on how to use this RFID reader with Arduino. If you need a tutorial for ESP8266 or NodeMCU you can find it here.
You can buy this device at the link below (affiliate links) :
MFRC522 RFID Reader Specification
Here’s the MFRC522 short specification :
- 13.56MHz RFID module
- Operating voltage: 2.5V to 3.3V
- Communication : SPI, I2C protocol, UART
- Maximum Data Rate: 10Mbps
- Read Range: 5cm
- Current Consumption: 13-26mA
- Power down mode consumption: 10uA (min)
How to use MFRC522 With Arduino?
It is simple, this reader uses SPI to interface with Arduino and uses 3.3V as a power source.
MFRC522 With Arduino Wiring Diagram
Connect MFRC522 RFID reader with Arduino like in the picture above. And you are ready to start the code.
MFRC522 RFID Reader Arduino Code
Before we start the code, I have to inform you that in this tutorial we will use two libraries. You can download the libraries at the link below.
After downloading the two libraries above, we can start the code. And here’s the example code of how to read the RFID tag and print that to the serial monitor.
#include <SPI.h> #include <RFID.h> #define SS_PIN 10 #define RST_PIN 9 RFID rfid(SS_PIN, RST_PIN); String rfidCard; void setup() { Serial.begin(9600); Serial.println("Starting the RFID Reader..."); SPI.begin(); rfid.init(); } void loop() { if (rfid.isCard()) { if (rfid.readCardSerial()) { rfidCard = String(rfid.serNum[0]) + " " + String(rfid.serNum[1]) + " " + String(rfid.serNum[2]) + " " + String(rfid.serNum[3]); Serial.println(rfidCard); } rfid.halt(); } }
Upload the code above and you should now see the RFID tag serial number on the serial monitor every time you tap the RFID tag.
And then I want to make a sample code if a serial tag with a certain number will make a buzzer blink three times and if the wrong tag is tapped it will trigger the long sound of the buzzer. This will simulate if we want to make for example RFID door lock or another project.
Add a 5V buzzer and connect the positive to pin 8 of Arduino and the ground pin to the ground of Arduino.
In this example, we will use a tag with the serial number “119 38 185 95” as the right tag. Or the key. You can edit this serial number in the code below.
#include <SPI.h> #include <RFID.h> #define SS_PIN 10 #define RST_PIN 9 RFID rfid(SS_PIN, RST_PIN); String rfidCard; void setup() { Serial.begin(9600); Serial.println("Starting the RFID Reader..."); SPI.begin(); rfid.init(); pinMode(8, OUTPUT); } void loop() { if (rfid.isCard()) { if (rfid.readCardSerial()) { rfidCard = String(rfid.serNum[0]) + " " + String(rfid.serNum[1]) + " " + String(rfid.serNum[2]) + " " + String(rfid.serNum[3]); Serial.println(rfidCard); if (rfidCard == "119 38 185 95") { digitalWrite(8, HIGH); delay(100); digitalWrite(8, LOW); delay(100); digitalWrite(8, HIGH); delay(100); digitalWrite(8, LOW); delay(100); digitalWrite(8, HIGH); delay(100); digitalWrite(8, LOW); delay(100); } else { digitalWrite(8, HIGH); delay(2000); digitalWrite(8, LOW); } } rfid.halt(); } }
Upload the code, and now you should hear three beeps from the buzzer if you tap the right tag. And will hear a long beep if you tap the wrong tag.
And here’s the complete video tutorial on how to use the RC522 RFID Reader with Arduino.