Arduino RFID

Integrate Arduino with RFID Module MFRC522 using Single Digital – Quick and Simple

RFID or radio frequency identification devices are type of technology that use radio tagging to identify the right card and open / close door or do the action accordingly, commonly used in security systems.

RFID module MFRC522 allows the use of RFID with Arduino very easily, although its quite simple and easy but this guide is specially targeted for beginners so what different here is that a beginner can understand that code very easily, As we are going to use only single digit to compare in equation (that will be security risk) but still very easy to work in hobby project and understand the function of RFID.

To start with RFID is simple technology that you have might seen that when you swipe cards a door opens:

Integrate Arduino with RFID Module:

Now you might have also seen previously that RFID module should be connected in some assigned pins as below:

Arduino UNO, NANO and MEGA with RFID RC522 Module Pins connection

Why it should be connected with these pins??
Well the reason is these are called SPI communication pins and RC522 communicate with SPI communication with Arduino.

So when we want to connect this module we need to connect it with Arduino we use SPI communication pins.

Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by Microcontrollers for communicating with one or more peripheral devices quickly over short distances

Now second part is coding, so gotechies got your back here as well.

To check what is the Decimal code of your cards, you can simply start with this code below:

#include <SPI.h>
/* Include the RFID library */
#include <RFID.h>

/* Define the DIO used for the SDA (SS) and RST (reset) pins. */
#define SDA_DIO 9
#define RESET_DIO 8
/* Create an instance of the RFID library */
RFID RC522(SDA_DIO, RESET_DIO); 

void setup()
{ 
  Serial.begin(9600);
  /* Enable the SPI interface */
  SPI.begin(); 
  /* Initialise the RFID reader */
  RC522.init();
}

void loop()
{
  /* Has a card been detected? */
  if (RC522.isCard())
  {
    /* If so then get its serial number */
    RC522.readCardSerial();
    Serial.println("Card detected:");
    for(int i=0;i<5;i++)
    {
    Serial.print(RC522.serNum[i],DEC);
    Serial.print(" ");
    //Serial.print(RC522.serNum[i],HEX); //to print card detail in Hexa Decimal format
    }
    Serial.println();
    Serial.println();
  }
  delay(1000);
}

Now to run this code above, you need two libraries
SPI and RFID

You can download these Libraries down here:

RFID: https://drive.google.com/file/d/1W0nj5R2-_2aQrSv_ud9IpYGw7h_2z42d/view?usp=sharing

SPI: https://drive.google.com/file/d/1ld-kZuPUT07OldtV1lED0CFQFdpFXvvv/view?usp=sharing

Now that you have got your code uploaded, open serial monitor & set baud rate to 9600

Swipe the card and check the number of your cards,

Note down your first digits of cards & let`s move to our second code and upload the code for conditions accordingly

/* Include the standard Arduino SPI library */
#include <SPI.h>
/* Include the RFID library */
#include <RFID.h>

/* Define the DIO used for the SDA (SS) and RST (reset) pins. */
#define SDA_DIO 9
#define RESET_DIO 8
/* Create an instance of the RFID library */
RFID RC522(SDA_DIO, RESET_DIO); 

void setup()
{ 
  Serial.begin(9600);
  /* Enable the SPI interface */
  SPI.begin(); 
  pinMode(19,OUTPUT);
  /* Initialise the RFID reader */
  RC522.init();
}

void loop()
{
  /* Has a card been detected? */
  if (RC522.isCard())
  {
    /* If so then get its serial number */
    RC522.readCardSerial();

   

 int code=RC522.serNum[0];

 if (code==241)
 {
  digitalWrite(19,HIGH);
 }

  if (code==49)
 {
   digitalWrite(19,LOW);
  
 }
    Serial.println();

  }
  delay(1000);
}

Now after uploading the code above and replace 241 and 49 in conditions with your own card numbers you will be able to check when cards are swiped and conditions are true, using these conditions you can apply any changes or take any actions accordingly,

Hopefully this tutorial helped you understanding the operation of RFID RC522 module. Keep following go techies more 🙂

Leave a Comment

Your email address will not be published.