arduino rfid security system board

Arduino RFID Reader RC522 + Access Control System

I just received my RC522 RFID reader and made this simple Arduino access control system that uses the reader, a buzzer for the alarm and a relay shield for the security system. The relay can be used to open a door lock and if you need a louder alarm then you can replace the small buzzer with a high power siren.

I will not talk about the RFID reader because you can find a lot of information about it on the web, but I will talk about the Arduino sketch which is very important. So, let’s start by watching this 7 minutes video to see the entire system in action:

Arduino RFID Schematic + some photos

Now let’s see the code:

* source:
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 10
* RST: Pin 9

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9


int startAlarm = false;
int resetAlarm = 2;
int relay = 7;
int alarm = 8;

int serNum[5];

int cards[][5] = {
  {209,128,106,69,126}, // card 1
  {101,220,213,229,137} // card 2

bool access = false;

void setup(){

    pinMode(resetAlarm, INPUT);
    pinMode(relay, OUTPUT);
    pinMode(alarm, OUTPUT);
    digitalWrite(relay, HIGH); // or LOW if you have a regular relay
    attachInterrupt(0, reset_alarm, LOW);

void loop(){
            for(int x = 0; x < sizeof(cards); x++){
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] != cards[x][i]) {
                      access = false;
                  } else {
                      access = true;
              if(access) break;
          startAlarm = false;
          digitalWrite(relay, LOW); // HIGH with regular relay     
       } else {
           Serial.println("Not allowed!"); 
           startAlarm = true; 
            digitalWrite(relay, HIGH);  // LOW with regular relay        
    if(startAlarm) {
       digitalWrite(alarm, HIGH); 
    } else {
      digitalWrite(alarm, LOW); 


void reset_alarm(){
    startAlarm = false;

First off all extract the RFID library and move the folder inside the Libraries folder where you’ve installed Arduino software. Now connect the RC522 reader to the board as you can see on the first lines in the sketch:

  • MOSI -> pin 11
  • MISO -> pin 12
  • SCK -> pin 13
  • SS (or SDA) -> pin 10
  • RST -> pin 9
  • 3.3V -> 3.3V
  • GND -> GND

The resetAlarm integer is the Arduino pin where we connect the button that is used the reset the alarm. The relay is connected to pin 7 and the active buzzer to pin 8.

On the next lines you need to change the cards codes with the ones you have. You’ll have to read the codes in the serial monitor then decide which ones you want to use to allow access in your room (watch the video to see how I’ve made it). You can use only one card or as many as you want, but add the comma after each array of elements.

The boolean access is set to false and is used to store the value true or false when we read the tags (or cards). If you go to line 63 you can see a for loop where we check to see if the values we stores in the cards array are the same as the ones we read from our RFID reader and if are the same then we set access variable to true.

Now, on line 76 if access is true we print “Welcome” in the console, set startAlarm to false so it doesn’t turn on, and activate the relay. If is false then we setAlarm to true and let the relay in its initial state.

On line 87 we check to see if the startAlarm is set to true and if so then we turn on the buzzer. If is false then do nothing.

We also have a resetAlarm button that is used to turn off the buzzer and the relay will not be activated. We might as well use a good RFID tag to stop it but now the relay will be activated.

This project is very simple and can be a good starting point to develop more complicated applications where you use the RC522 RFID reader. Stay tuned because I will try to make some new and interesting systems based on this reader.


Join the conversation!

Error! Please fill all fields.
  • noelkuckgmail-com

    My card has a hexadecimal UID number. How can I utilize your code which shows a decimal UID code?

  • vrana01free-fr

    Is it possible to use more than one RFID reader on the same Arduino ?
    I want to control the access of my portal from the street, from my garden and from my house.

  • wonggiloxxx95gmail-com

    Hello. Can you help me? I want modify this code for different application. But I have a problem. How to modify this code if I want output relay will be actived after card is placed on the reader (serNum is true), and if I take the card from the reader (reader not detected card) the relay will be not actived. So how to modifed your code. I’m so sorry my english is so bad. Thank you very much for your code. ^_^

  • rmantn1992gmail-com

    Each time that I want to use from RFID I’m worried about the RFID’s tag security, we use from Tag’s ID to authenticate the right tag and unlock the security system, so if someone steal my tag and clone the data to a new card what going to happening to our security system? In this case someone else have oue sycurity tag, is it true?

    • P. Marian

      The same is true if they steal your keys and you have to change the lock. In this case you have to change the tag and tag ID in the code.

  • Killer800

    How to change the id card to other block andress,,for example i want to change it to block adress 4 or 5

  • Miyak James

    Hi, I downloaded “code_lock.rar” which happens to contain lcd.c which is a 4bit interface; having failed many a times at initailizing LCD in 4bit mode. Sir am requesting permission to use it in projects.

  • Abhi

    it works…. thank u

  • Nooruddin

    the rfid.h liberary is not downloading well and it’s not working to compile .showing library error .plz kindly send me the library .

  • Plug56uk

    Help I need to keep my girlfriend out of my room !!
    ( joke )

    Thank you for sharing your project, it is very good.

    I am new to writing sketches and would grateful if you could guide me.

    I have down loaded the sketch and the library and followed the You tube video. But I can not get the sketch to run. I have spent a lot of time on this project and have learned many bits of information. I read in a book ” programming in arduino ” by simon monk, that a contributed libraries need a keyword file.
    I have looked in the .h and the ccp file and can not see ” keywords ”

    A bit lost, hope you can help
    Thank you

  • Mosfet

    Library doesn’t work. Why? :/

Looking for the latest from TI?