MFRC522 Library Cheap RC522 rfid Reader Door Unlock Project

[2017 Update] I am proud to present new project that has almost what i have dreamed.


I recently got a RC522 RFID Reader Module based on MFRC522 Datasheet

I just want create a RFID door unlock system for my apartment's door, Using miguelbalboa's RFID library which is great and module is cheap (around 5-10$). I am inspired project from Brett Martin (His project). And I just want to publish my workings because there are some old informations about MFRC522 Library around in forum . And I am going to try comment all code lines as much as I can.

YouTube Video

And here is the code and flow scheme. Code on GitHub And Since i am new to coding, so any help will be appreciated.

Work Flow |500x312

Now code does not fit here, please grab it from GitHub


[ x ] SD Card option for Data Storage

[ ] Web UI for Managing Remotely

[ ] Complete Embedded System with Ethernet Shield

*************** Updates *************************

23/07/2014 ***** Initial Upload Nothing Works, Updated GitHub link, Commented some lines

25/07/2014 ***** Massive code correction from Forum Member luisilva, Now code Works There are still issues with leds corrected Code is now more human readable (at least for newbies like me) compared to original code

26/07/2014 ***** Testing code for bugs, etc. So far, everthing works as expected Code optimized for BOTH Common Anode or Common Cathode Leds, or just use regular Leds Now it does not matter which led you are going to use, simply define what led you have

27/07/2014 ***** Buzzer code (not needed actually, added anyway) removed More information about project provided with code Now you can Exit Program Mode by scanning Master Card again if you entered accidently Corrected EEPROM code for cards which have only 4 byte UID

01/08/2014 ***** Some minor delay corrections, minor changes Relay Code. Code for controlling Relay not universal. You need to modify Relay Code for your needs.When in program mode led cycles through Red Green Blue I have been testing whole project for days, so far so good.

06/08/2014 ***** Refined Code - Version 1.0.0

10/08/2014 ***** 1.1.0 Modified Library to set receiver gain to maximum (48 db) Now module can scan cards from about 3 cm far (It was about 2 cm before). You can grab modified Library files from GitHub. Or modify your files. Now Library supports Antenna Gain Control For details follow this link:

12/08/2014 ***** 1.1.1 Added Wipe Mode code. By default its not enabled.

13/08/2014 ***** 1.1.2 Thanks to Forum Members, wipe mode is now matured and enabled by default.

17/08/2014 ***** 2.0.0 In this version, Master Card's UID no more need to be hard coded. It will be defined when first use.

27/08/2014 ***** 2.0.5 Minor Fixes - wipe mode led, checking if master card defined, etc.

02/09/2014 ***** Now a script can be used to remove access to known (UID) lost card. If you know UID of lost card you can delete it from EEPROM without having to wipe entire EEPROM. This will let you operate normally after reprogramming, known cards will be remain as they were. Script on GitHub.

01/11/2014 ***** 2.0.6 Minor Update - Program Mode will not exit until Master Card scanned again. It was quite taking time to program all cards, because we were exiting Program Mode whenever a card scanned.

11/11/2014 ***** 2.0.7 With latest update Library supports Gain Control. No need to modify library files anymore.

01/01/2015 ***** 2.0.8 Fix for those who have PICCs that have 7 byte UID. Actually this is a workaround not a complete fix.

30/05/2015 ***** 3.0 - 3.2 With starting version 3 I decided to develop more advanced system, and separated code for backward compatibility to users who want to use just old system. From now and on I am going to focus on developing an embedded system. Also reviewed old code and made small changes, Code now more RAM optimized, RFID hardware version check added, and made some minor code changes. Antenna Gain disabled for stability reasons (one report indicated that disabling antenna gain fixed an unstable run). Now there is SD Card option for data storage, like the old system program checks if master card is defined, master.dat file on sd card holds UID information of master card. Rest of the cards' UID information will be stored as XXXXXXXX.dat file where XXXXXXXX replaced by UID information of the card. This is simplest way i can guess to store information on SD card, it may change in future development. That's it for now.

04/06/2015 ***** 4.3b Web UI Lists UID list and Usernames if defined in UID files. Coded just snippet not actual release.

Looks Great! I've been looking for a code that does exactly this for a while now and this is by far the easiest to understand and use. I'm currently working on a battery powered system and wanted to know if it was possible to shut off the rc522 while the board is asleep and periodically check for a card. Also reading through the notes on github and 3.7 billion different UID's seems fairly secure especially considering most doors have a physical key. Anyway good work and i hope you continue your work with this code. XD


RC522 Chip can be POWERed-DOWN, Datasheet says about powering down the RC522 chip:

 Pin 6 : NRSTPD = reset and power-down input:
power-down: enabled when LOW; internal current sinks are switched off, the oscillator
is inhibited and the input pins are disconnected from the outside world
reset: enabled

Library already uses this pin as RST (Reset) Library says:

    byte _resetPowerDownPin;    // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low)

But I can't find any information about Sleep function in Library, I think power consumption very low, once I measured device's power consumption (UNO + RC552 + Relay) and my multimeter showed 0.05 Amps at 5v. I am going to use this code on Pro Mini, ordered some and waiting them for arrival, I'm going to measure again when I finish on hardware configuration.

8.6 Power reduction modes

8.6.1 Hard power-down

Hard power-down is enabled when pin NRSTPD is LOW. This turns off all internal current
sinks including the oscillator. All digital input buffers are separated from the input pins and
clamped internally (except pin NRSTPD). The output pins are frozen at either a HIGH or
LOW level.

8.6.2 Soft power-down mode

Soft Power-down mode is entered immediately after the CommandReg register’s
PowerDown bit is set to logic 1. All internal current sinks are switched off, including the
oscillator buffer. However, the digital input buffers are not separated from the input pins
and keep their functionality. The digital output pins do not change their state.
During soft power-down, all register values, the FIFO buffer content and the configuration
keep their current contents.
After setting the PowerDown bit to logic 0, it takes 1024 clocks until the Soft power-down
mode is exited indicated by the PowerDown bit. Setting it to logic 0 does not immediately
clear it. It is cleared automatically by the MFRC522 when Soft power-down mode is
Remark: If the internal oscillator is used, you must take into account that it is supplied by
pin AVDD and it will take a certain time (t osc ) until the oscillator is stable and the clock
cycles can be detected by the internal logic. It is recommended for the serial UART, to first
send the value 55h to the MFRC522. The oscillator must be stable for further access to
the registers. To ensure this, perform a read access to address 0 until the MFRC522
answers to the last read command with the register content of address 0. This indicates
that the MFRC522 is ready.

8.6.3 Transmitter power-down mode

The Transmitter Power-down mode switches off the internal antenna drivers thereby,
turning off the RF field. Transmitter power-down mode is entered by setting either the
TxControlReg register’s Tx1RFEn bit or Tx2RFEn bit to logic 0.

About security, I think that note scary and should be removed :), RFID basically identification system, you show your ID to Micro-controller and if it knows you opens you the door, simple. But since this code and the way of the whole project works can be expanded I think of two versions of code, advanced and simple.

hi if we lost a card how to remove it from eeprom? must delet all data or can reaad & modify data in a software! tank

This is main problem with this project i can think of. When a card lost, if you don't know its Unique ID then you need to wipe, and program remaining cards with master card. If you know its UID you can delete it from EEPROM. You can keep UID records somewhere else, I just give numbers to cards and matched their UIDs with their numbers on a Spreadsheet on my Computer, just in case if a card lost. I am going to code a script that does delete lost record from EEPROM check this page later for update. Thank you.

Thanks for answer I'm waiting to fix problem

You can use DeleteLostTag.ino

Wll done, Dear. I've got it. I've used it for the same project. but I'd like to alterate it in some point, I mean,

I would like to put a counter to the enabled access cards. for example, an accountant with 10 hits, and when it is equal to zero, just read this tag without activating the relay, after that, I can reactivate it with the counter equal to 10 again. using the master card.


i uploaded the code, how do i create a master card?

It looks if it is defined or not. if not waits for a card to be scanned, and that card will be master card.

i uploaded the code, then i scanned a card and looking at the serial monitor it says you shall not pass everytime i scan it. i even tried another card. do i need to enter the info of the card in the code somewhere? i dont know if it changes anything but im using a uno bored and a rc522 reader.

When you reset your arduino on serial monitor it should display master's card ID? Do you see that?

i just re uploaded the program to be sure it was correct. i turned on serial monitor and waved the card 1 time. it came up with you shall not pass and gave me the id of the card. i pressed the reset button on the board and tried again with no change. it also didnt display and data for a master card.

Interesting. Are you sure you are uploading latest version?

that must have been it. its working now. thanks! now i need to get it to open a servo and click on another relay. thanks for the help.

metallica21156: that must have been it. its working now. thanks! now i need to get it to open a servo and click on another relay. thanks for the help.


now i have a new issue im unsure if you can help. i switched boards to a mega 2560, i changed the define pins to the correct pins now used with the mega but im unable to get your program to complete the upload. it compiles fine but only uploads about 80%.

What happens then?

It wont complete the upload of the program. I havent tried it to see if it works but im sure it wont if its missing stuff.

Any error message?