This blog post shows a simple example on how to use the MFRC522 RFID reader. I’ll do a quick overview of the specifications and demonstrate a project example using an Arduino.
Description
RFID means radio-frequency identification. RFID uses electromagnetic fields to transfer data over short distances. RFID is useful to identify people, to make transactions, etc…
You can use an RFID system to open a door. For example, only the person with the right information on his card is allowed to enter. An RFID system uses:
- tags attached to the object to be identified, in this example we have a keychain and an electromagnetic card. Each tag has his own identification (UID).
- two-way radio transmitter-receiver, the reader, that send a signal to the tag and read its response.
Specifications
- Input voltage: 3.3V
- Price: approximately 3$ (check best price on Maker Advisor)
- Frequency: 13.56MHz
Library download
Here’s the library you need for this project:
- Download the RFID library here created by miguelbalboa
- Unzip the RFID library
- Install the RFID library in your Arduino IDE
- Restart your Arduino IDE
Pin wiring
Pin | Wiring to Arduino Uno |
SDA | Digital 10 |
SCK | Digital 13 |
MOSI | Digital 11 |
MISO | Digital 12 |
IRQ | unconnected |
GND | GND |
RST | Digital 9 |
3.3V | 3.3V |
Caution: You must power this device to 3.3V!
Circuit
Reading Data from a RFID tag
After having the circuit ready, go to File > Examples > MFRC522 > DumpInfo and upload the code. This code will be available in your Arduino IDE (after installing the RFID library).
Then, open the serial monitor. You should see something like the figure below:
Approximate the RFID card or the keychain to the reader. Let the reader and the tag closer until all the information is displayed.
This is the information that you can read from the card, including the card UID that is highlighted in yellow. The information is stored in the memory that is divided into segments and blocks as you can see in the previous picture.
You have 1024 bytes of data storage divided into 16 sectors and each sector is protected by two different keys, A and B.
Write down your UID card because you’ll need it later.
Upload the following code.
/*
*
* All the resources for this project: https://randomnerdtutorials.com/
* Modified by Rui Santos
*
* Created by FILIPEFLOP
*
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "BD 31 15 2B") //change here the UID of the card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println();
delay(3000);
}
else {
Serial.println(" Access denied");
delay(3000);
}
}
In the piece of code above you need to change the if (content.substring(1) == “REPLACE WITH YOUR UID”) and type the UID card you’ve written previously.
Demonstration
Now, upload the code to your Arduino and open the serial monitor.
Approximate the card you’ve chosen to give access and you’ll see:
If you approximate another tag with another UID, the denial message will show up:
I hope you found this tutorial useful.
Share this post with a friend that also likes electronics!
You can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog.
Thanks for reading,
-Rui Santos
Very Nice!!! Thank you. I love $4.00 devices!!
These are great, you should definitely get these modules!
Thanks for reading,
Rui
Hint: test the distances by changing the project to use a RFID in format of rice.
Hi Rui,
What is the purpose of the lcd ?
Thanks
It’s not an LCD, it’s the Arduino IDE serial monitor to see what’s printing in the serial port
Thanks for the response,
I was talking about the library… I believe we can erase the following
lines:
#include
#include
#include <—- this lcd library
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
LiquidCrystal lcd(6, 7, 5, 4, 3, 2); <————-this line too
Thanks
Wow. I have all the components needed in this tutorial.
Let me implement this.
Thanks.
Let me know the results!
Is there a way to monitor signal strength on this reader?
I don’t think that feature is implemented in the library…
Not bad.
Using an Arduino Uno is a little bit overkill.
A Nano (5$ on eBay) probably does the job.
Yes, you can definitely use the Arduino Nano!
Yep, but need to remove string concatenation of string in code… There is only 2KB of ram on the Nano 😉
The Uno and Nano are almost identical when talking about hardware performance. Processor speed, SRAM, Flash are all the same. Only real difference is size and nano has 2 extra analog inputs. You could deffinatly use the Nano but I dont know how an uno would be overkill.
Very nice tutorial.
But this #include is for lcd
and this LiquidCrystal lcd(6, 7, 5, 4, 3, 2); for lcd to run
So, Rui, if we use on lcd, we can’t write “Approximate your card to the reader…”) I guess
Its larger than 16 Character. Instead we can write “Identifying”(example).
Anyway…………..I love your posts. I like this post. Your all tutorials are the best for me. I also love ultrasonic hcsr04 tutorial which makes me to see how HCSR04 works with v = s/t and how to get 1cm= 29.2 microsecond. Thank you very much Rui. I will be always your audience.
Pretty cool. Worked just like you explained it.
Thanks!
Thanks for this great post. I will order parts and try it soon.
That’s perfect! Thanks for sharing, If we can also display the welcome greetings and access denied message in a 16*2 display lcd instead of serial monitor this will be more useful 🙂 , That must be just few more wires and few more lines of code but it will help readers more 🙂
Thanks,
Subash
Hi Rui, Thank you for another excellent demo, could you explain how to add extra tags if possible to this code. I have tried several ways in the “if” string but no cigar. Thanks again if could help.
Regards
Geoff
Something like this should work:
if (content.substring(1) == “BD 31 15 2B” || content.substring(1) == “YOUR_OTHER_TAG_HERE” )
this worked for me just delete the ‘)’ and add a ‘:’ then your next key tag or card:
if (content.substring(1) == “3E F5 4B D3″;”99 1D 8C BB”) //change here the UID of the card/cards that you want to give access
{
fiz isso mas ai ele não bloqueia mais nenhum cartão
if (content.substring(1) == “59 25 08 A4”) //change here the UID of the card/cards that you want to give access
{
Serial.println(“Authorized access”);
Serial.println();
delay(3000);
}
if (content.substring(1) == “07 5B BF 3A”) //change here the UID of the 2 card/cards that you want to give access
{
Serial.println(“Authorized access”);
Serial.println();
delay(3000);
}
else {
Serial.println(” Access denied”);
delay(3000);
}
}
I got this to work for anoter card. one card one keychain
jus change the uids to match yours
Thanks for complete details, I did it in 1st attempt.
how to I hook a Relay to trigger when Authorized Card is triggered and it should have a short beep to show that card is accepted.
I’m glad it worked.
Search for buzzer with arduino. You’ll find good tutorials to help you complete that addicional feature
Sir i tried it but the code gives me an error.. said that ‘class MFRC522’ has no member named ‘PCD_Init’… how can i fixed this error ? please can you guide me …
You need to install the proper library in your Arduino IDE as I show in the blog post
It’s working. Thank you randomnerd for this tutorial! 🙂
Nice and very helpful work ,,…..
Thanx..
You’re welcome!
Thanks for reading,
Rui
Thank You for this post.. Was of Great Help..
Especially the code..
Good good great!
My private ID card works :))))
Thank you for good job!
Thanks for very helpful program
Thanks 🙂
Hello, my projects involves 3 RFID modules, and i want to ask if tagID ==”XX XX XX XX” then do something. So same as what you’re doing but with 3 readers. I have separate codes for both but i can’t seem to merge them. Help?
Hi.
How are you doing the merge?
A series of if statements should work for your example.
im want use this in a arduino nano, can you tell me how to change the pin number 13 to different one?
thanks
Hi Rob.
The MFRC522 rfid reader uses SPI communication protocol.
Pin 13 is one of the SPi pins. So, you need to use that pin.
Regards,
Sara 🙂
For several hours I have been looking for a way like this, thanks, very helpful to me who just learned about arduino and rfid.
You’re welcome Albaab. I’m glad you found this tutorial helpful!
Regards,
Rui
Thank you for your article
You’re welcome Ranger. Thanks for reading!
I got his up and running on a WeMos mini first try (Thanks) with one card but can’t figure out how to add a second card???
Hi Crhis.
You need to add a second if statement to check the UUID of the other card:
if (content.substring(1) == “BD 31 15 2B”) //change here the UID of the card/cards that you want to give access
I hope this helps.
Regards.
I’ve tried this but no luck, it gives me an access denied on the first card then and the second card it runs several lines of the UID in the serial monitor…
}
Serial.println();
Serial.print(“Message : “);
content.toUpperCase();
if (content.substring(1) == “49 48 61 4D”) //change here the UID of the card/cards that you want to give access
if (content.substring(1) == “59 C6 D5 4D”) //change here the UID of the card/cards that you want to give access
{
Serial.println(“Authorized access”);
Serial.println();
delay(3000);
}
Worked great even for the Mega 2560 Board – just make sure you change the pins from 10 to 53 and 9 to 5. Really enjoyed this small project. Thanks.
Hi! I have a problem with my RFID reader,
after connecting it to my Arduino and testing the example of dumpinfo
my serial monitor shows a message : ..counterfeit chip”
and does not react for an ID tag nor the card
does it mean my module will not work with the arduino?
Thanks!!
Hi Iza.
I’ve never faced that error.
But it seems that it will not work :\
Regards,
Sara 🙂
Thanks
Hi Sara, great tutorial. I wanted to add multiple RFID sensors on different readers and wanted to make sure only one sensor activates a specific reader. Is there a relatively easy way to code this? I figure I’d have to list them in a certainorder somewhere, but can’t quite navigate my way through it.
Hi Wes.
What do you mean by multiple RFID sensors on different readers?
Do you want to have several MFRC522 RFID modules connected to the Arduino at the same time?
Regards,
Sara 🙂
So sorry, that was my mistake to phrase it like that. I did indeed mean I wanted multiple RFID modules connected to one Arduino.
Hi Wes.
The RFID module communicates via SPI.
So, you need to know how to connect multiple SPI bus.
Each SPI device’s CS pin must be connected to a different pin on your Arduino. By setting one of those pins LOW you select which device is active on the SPI bus.
We don’t have a specific tutorial about that, but we have a project that uses two SPI devices at the same time: one is the RFID reader and the other is an SD card module.
You can take a look at the code to better understand how to handle everything:
https://randomnerdtutorials.com/arduino-time-attendance-system-with-rfid/
Additionally, there’s also a discussion on the Arduino forum that might help:
forum.arduino.cc/index.php?topic=519774.0
Regards,
Sara 🙂
hello Rui Santos i was not connecting to the arduino uno i was connection to WEMOS-minPro board.But after i run the code it shown nothing can you help ?
I’ve never tried this code in that board, so I don’t have any information on that subject.
Thanks for this Tut!
I’m using a sainsmart rfid sensor with a Kumna UNO.
I’ve installed the library you mention and loaded the DumpInfo sketch.
I’ve wired it all up per your diag. above but get the following Serial Monitor output after uploading the sketch:
Firmware Version: 0x0 = (unknown)
13:41:42.294 -> WARNING: Communication failure, is the MFRC522 properly connected?
13:41:42.362 -> Scan PICC to see UID, SAK, type, and data blocks…
Any thoughts on what would cause the firmware version 0x0 to occur?
Best Regards,
Hi Dennis.
After searching for a while it may be a bad connection.
Can you follow the suggestions on these threads and see if you got it working?
https://github.com/miguelbalboa/rfid/issues/101
https://github.com/miguelbalboa/rfid/issues/299
I hope this helps.
Regards,
Sara 🙂
I have a similar problem, but my error code is Firmware Version: 0xEE = (unknown). there is no output when the tag is placed on the reader
Hi there,
Thank you for this tutorial.
You’re welcome! 🙂
Hi there I am experiencing an issue where when I open the serial monitor on the example screen dump code it says underneath the Firmware version “WARNING: Communication failure, is the MFRC522 properly connected?”
Hi.
That means that your Arduino was not able to establish an SPI communication with the RFID reader.
It can be one of the following problems:
– it is not wired properly
– you’re not using a suitable power supply
– the RFID reader is broken
Please make sure that everything is wired correctly and you’re using a suitable power supply.
Regards,
Sara
Hi thank you for writing this tutorial i need this so much but i have a big problme!!
how can this code work on arduino prefectly but not on ESP32?????
PLEASE HELP ME i need to upload this code on ESP32
Hi.
It is probably the library that is not compatible or the wiring is not correct.
Take a look at this library, I think it might help: https://github.com/blmhemu/ESP32-MFRC522
Regards,
Sara
RC522 -> ESP32
SDA(SS) -> GPIO21,
SCK -> GPIO18,
MOSI -> GPIO23,
MISO -> GPIO19,
IRQ -> –,
GND -> GND,
RST -> GPIO17,
3v3 -> 3v3
Hello how to write print card id serial to notepad or to input text?
Hi.
You can save your card info in a .txt file using a microSD card reader.
For example, take a look at this project and see if it helps: https://randomnerdtutorials.com/arduino-time-attendance-system-with-rfid/
Regards,
Sara
how do i remap all the pin to work in a nano becouse it does not have a pin 13
Hi Daniel.
The Arduino nano does have a pin 13.
Search for “Arduino nano pinout”. And you’ll see the location of pin 13.
Regards,
Sara
Thank you, everything worked great. How can I incorporate It to trigger a relay when access is authorized?
2021, still a great resource, thanks for taking the time.
Using Arduino Uno, I’m on the verge of giving up. I keep getting 0x12 counterfeit chip. I have tried with something 7 cards now, from 3 or four different sellers. I even tried rc522 mini module too, just because then I knew it wasn’t the exactly same.
I have tried primarily with Unos, but several. I think I also tried from a WeMos D1 Mini.
I have triple checked wiring every time of course. Does anyone have any good advice?
February, 2021. I´m just starting my studies on Arduino and your site was a really great source of knowledge. Thank you!
That’s great!
Well, this is my first RFID project. I managed to get everything setup, But it is not reading my cards??? Just sits there with the message:
Approximate your card to the reader…
Is this a connection problem?????
love your posts easy to follow, keep up the great work.
i have a nano r3 hooked up to a mfrc522 an i can read the card and key fob, but when i try to run your code to grant access to a card i get an error;
“error compiling to arduino nano”
i aw an earlier post where you said to lessen the content in the file but what do i take out to make it work.
any assistance you can provide will be greatly appreciated.
thank you
wow i just had a ID10T error i got it now i had only copied half of the code works great when everything gets copied correctly, already got four cards linked to one unit. now to get the lock built and install on my entrance gate hope the rest works as good as the rfid reader and arduino nano r3 do together.
Great!
Hi.
Can you provide more details about the error?
Regards,
Sara
i am using a arduino nano r3 and i had only copied half of the code and pasted it in arduino ide and upon trying to verify/compile the sketch it would say in the error log that thre was an “expected end on the start up” i will replicate it and will copy the results and post here so that you may see what was going on.
Hi,
Thanks for the tutorial. Just one question. I added lines into the if statement to control a lock with a relay. It works the first time but then stops an d won’t read any more ids. I used println(“Done”) to check if the code hangs anywhere but it seems to work through ok. When I take out the 2 digitalwrite commands it goes back to reading ids again.
if (content.substring(1) == “8xxxx”||”xx”) //change here the UID of the card/cards that you want to give access
{
Serial.println(“Authorized access”);
Serial.println();
digitalWrite(RELAY_PIN, HIGH); // unlock the door
delay(5000);
digitalWrite(RELAY_PIN, LOW); // lock the door
delay(5000);
Serial.println(“Done”);
any idea why?
Thanks,
}
Hi I have Arduino uno R3 And i cont woke ket it to work on RFID ring 125 khz thanks thys
Hi, I was successful to incorporate the MFR522 into ESP32 that sends the UID to an Android app via Bluetooth. I need help to send the UID via serial port to a laptop using PHP to retrieve MySQL data. I don’t want to send the UID over wifi as the ESP32 will be used in different locations and I don’t want to have to set up the ssid every time – USB serial would be the most convenient. Any ideas?
RF522 is very cheap and very usefull thing. We can make many smart things like secure door lock, Personalised sweetch and many more. I’l try to do counterclock for working Hour measure. but i don’t known how to put the uid to the variable and send this uid to the database (mysql) with ESP32 or NodeMCU. Any ideas? Any help?
Thank you so much! This was very simple and clear.
Worked first time.
Arduino: 1.8.18 (Windows 10), Board: “Arduino Uno”
DumpInfo:107:16: error: redefinition of ‘MFRC522 mfrc522’
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
^
C:\Users\nick\AppData\Local\Temp\arduino_modified_sketch_50484\DumpInfo.ino:15:9: note: ‘MFRC522 mfrc522’ previously declared here
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
^~~~~~~
C:\Users\nick\AppData\Local\Temp\arduino_modified_sketch_50484\DumpInfo.ino: In function ‘void setup()’:
DumpInfo:109:6: error: redefinition of ‘void setup()’
void setup() {
^~~~~
C:\Users\nick\AppData\Local\Temp\arduino_modified_sketch_50484\DumpInfo.ino:17:6: note: ‘void setup()’ previously defined here
void setup()
^~~~~
C:\Users\nick\AppData\Local\Temp\arduino_modified_sketch_50484\DumpInfo.ino: In function ‘void loop()’:
DumpInfo:119:6: error: redefinition of ‘void loop()’
void loop() {
^~~~
C:\Users\nick\AppData\Local\Temp\arduino_modified_sketch_50484\DumpInfo.ino:26:6: note: ‘void loop()’ previously defined here
void loop()
^~~~
Multiple libraries were found for “MFRC522.h”
Used: C:\Users\nick\OneDrive\Documents\Arduino\libraries\MFRC522
Not used: C:\Users\nick\OneDrive\Documents\Arduino\libraries\MFRC522-spi-i2c-uart-async
exit status 1
redefinition of ‘MFRC522 mfrc522’
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
this is what i get i am getting
Is there any code that can help me make the RFID scanner RC522 work with the C++ in Keil studio, using Nucleo board F429ZI (comes with STM 32 microcontroller)
Hi.
I’m sorry, but we don’t have any tutorials about that subject.
Regards,
Sara
Hello
I wanted help with a project using RFID and esp32 wroom to display the RFID output via Bluetooth onto my smartphone.can you please help me with the code for the same as I keep getting error when I combine the code for RFID tag detection and Bluetooth connection to smartphone
Can i know how to connect multiple RFID reader to a arduino uno r3 with breed board. The wires setup.
for multiple UIDs, use:
if (content.substring(1) == “UID #1” || content.substring(1) == “UID #2” ) {
{
Serial.println(“Authorized access”);
Serial.println();
delay(3000);
}
}
the last “}” is a space before “else”
Do you happen to know the protocol for car keys that have those microchips in them? Thanks!