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 program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

59 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

  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.

  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

  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

  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.

  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?

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

  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.

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

  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.

  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!!

  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.

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

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

  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 ?

  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,

  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?”

    • 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

  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

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.