Security Access using MFRC522 RFID Reader with Arduino

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).

tags

  • two-way radio transmitter-receiver, the reader, that send a signal to the tag and read its response.

readerSpecifications

Library download

Here’s the library you need for this project:

  1. Download the RFID library here created by miguelbalboa
  2. Unzip the RFID library
  3. Install the RFID library in your Arduino IDE
  4. 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

Mifare_bb

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:

serial monitor1

Approximate the RFID card or the keychain to the reader. Let the reader and the tag closer until all the information is displayed.

serial monitor2

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);
  }
} 

View raw code

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:

serial monitor3

If you approximate another tag with another UID, the denial message will show up:

serial monitor4

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



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

93 thoughts on “Security Access using MFRC522 RFID Reader with Arduino”

      • 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

        Reply
    • 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.

      Reply
  1. 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.

    Reply
  2. 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

    Reply
  3. 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

    Reply
      • 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
        {

        Reply
    • 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

      Reply
  4. 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.

    Reply
  5. 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?

    Reply
    • 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 🙂

      Reply
  6. For several hours I have been looking for a way like this, thanks, very helpful to me who just learned about arduino and rfid.

    Reply
    • 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.

      Reply
      • 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);
        }

        Reply
  7. 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.

    Reply
  8. 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!!

    Reply
  9. 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.

    Reply
    • 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 🙂

      Reply
        • 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 🙂

          Reply
  10. 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 ?

    Reply
  11. 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,

    Reply
  12. 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?”

    Reply
    • 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

      Reply
  13. 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

    Reply
    • 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

      Reply
  14. 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?

    Reply
  15. February, 2021. I´m just starting my studies on Arduino and your site was a really great source of knowledge. Thank you!

    Reply
  16. 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?????

    Reply
  17. 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

    Reply
    • 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.

      Reply
      • 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.

        Reply
  18. 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,

    Reply
  19. 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?

    Reply
  20. 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?

    Reply
  21. 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

    Reply
  22. 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)

    Reply
  23. 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

    Reply
  24. 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”

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.