Introduction: Interfacing RFID-RC522 With Arduino MEGA a Simple Sketch

Hai there in this tutorial i'm going to help you with interfacing RFID-RC522 with Arduino Mega 2560 to read the RFID and Display the Data on the Serial Monitor. so you can able to extend it on your own


You Need:

  1. Arduino Mega or Arduino Uno (I used Mega)
  2. RFID-RC522
  3. 7 male to female jumper wires
  4. Some ID cards (optional)
  5. RFID Library (Must , Link Below)

Then Download the Below library and Add it to your Arduino IDE by clicking Sketch->Include Library-> Add .Zip Library in file menu

Step 1: Physical Connection Detail

simply connect the arduino with RFID-RC522 as shown in the above image .

Warning : supply only 3.3V otherwise module will burn out

Pin Out for Uno/Nano and Mega

RC522 MODULE Uno/Nano MEGA
SDA D10 D9 SCK D13 D52 MOSI D11 D51 MISO D12 D50 IRQ N/A N/A GND GND GND RST D9 D8 3.3V 3.3V 3.3V

Step 2: Simple Code to Read and Print RFID Tags Value

Copy the Below code then upload it on your Arduino

/*
PINOUT: RC522 MODULE Uno/Nano MEGA SDA D10 D9 SCK D13 D52 MOSI D11 D51 MISO D12 D50 IRQ N/A N/A GND GND GND RST D9 D8 3.3V 3.3V 3.3V */ /* 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(); /* 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(RC522.serNum[i],HEX); //to print card detail in Hexa Decimal format } Serial.println(); Serial.println(); } delay(1000); }



Step 3: Simple Code for Super Market Application Using RFID

Copy the Below code then upload it on your Arduino. in the below the total purchase value will incremented when reading the card first time then decremented when reading the same for second time...


/*

PINOUT:

RC522 MODULE Uno/Nano MEGA SDA D10 D9 SCK D13 D52 MOSI D11 D51 MISO D12 D50 IRQ N/A N/A GND GND GND RST D9 D8 3.3V 3.3V 3.3V

*

* Include the standard Arduino SPI library */ #include /* Include the RFID library */ #include

/* Define the DIO used for the SDA (SS) and RST (reset) pins. */ #define SDA_DIO 9 #define RESET_DIO 8 int productname[5]={228,18,37,75,24}; int product[5]={100,120,230,125,70}; int token[5]={0,0,0,0,0}; int Total; /* 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() { /* Temporary loop counter */ byte i=0; byte j=0; byte k=0; int ID;

/* Has a card been detected? */ if (RC522.isCard()) { /* If so then get its serial number */ RC522.readCardSerial(); Serial.print(RC522.serNum[i],DEC);

//Serial.println("Card detected:");

/* Output the serial number to the UART */ ID=RC522.serNum[0]; //Serial.print(ID); Serial.println(" "); for(i=0;i<5;i++) { if(productname[i]==ID) { Serial.println("Total Purchase"); if(token[i]==0) { Total=Total+product[i]; token[i]=1; } else { Total=Total-product[i]; token[i]=0; } Serial.println(Total); break; } else if(i==5) { Serial.println("Access Denied"); break; } } Serial.println(); Serial.println(); } delay(1000); }

Step 4: Conclusion.,

I would like to thank you for reading my tutorial. I would appreciate if you found it useful and drop a like (favorite) or ask me anything as it keeps me motivated to do these instructables. feel free to ask any questions that you need to know...

Happy Coding Arduino...

Comments

author
WadahT made it! (author)2017-05-15

Hi, How can I pass the RFID serial to a variable to use it in an " IF STATEMENT "??

author
Johnson Selva made it! (author)Johnson Selva2017-05-15

use array to store value like below ..

//its not complete code its just an example

int productname[5]={0,0,0,0,0};

void setup()

{

}

void loop()

{

for(int i=0;i<5;i++)

{

productname[i]=RC522.serNum[i];

}

}

author
Johnson Selva made it! (author)Johnson Selva2017-05-15

or create a integer as below

int value;

value=RC522.serNum[1];

author
StephenL160 made it! (author)2017-05-04

Hi,

I thnik I have problem with my RFID librairies.

I can read the RC522 FW bur I can't detected the card.

And i don't see the RFID in orange on my including function.

Can you help me ?

author
Johnson Selva made it! (author)Johnson Selva2017-05-04

sure will you able to send me screenshot of the code and clear photo of your connection . so i may able to help you easily...

author
StephenL160 made it! (author)StephenL1602017-05-05

Sure,

Thank's for you'r help.

file.jpegfile3.jpegfile4.jpegfile6.jpegRFID.jpg
author
Johnson Selva made it! (author)Johnson Selva2017-05-05

brother it seems the library is not getting initialized for you because of version incompatibility. just try "Arduino 1.6.11 IDE" and let me know if it works. it seems everything is good on your side :-(

author
StephenL160 made it! (author)StephenL1602017-05-08

I try to used the V1.6.11 of IDE, no result.

I'm trying to re-associated all the way with other pin on my arduino Mega.
50=MISO
51=MOSI
52=SCK
53=SS (SDA) an on other test on the 21pin (SDA)
5=RS
But it's the same problem.

I have reset to zero my computer, no result again.

author
Johnson Selva made it! (author)Johnson Selva2017-05-09

OMG honestly i can't able to figure it out whats going on...it may be problem with hardware i think but i'm not sure sorry dude

author
StephenL160 made it! (author)StephenL1602017-05-09

No problem;

I will try to make better connections and I will try other cards. I order a shield PN532 and one arduino UNO to see what happened

I'll keep you informed

author
Johnson Selva made it! (author)Johnson Selva2017-05-09

Sure bro Gud luck on that...

author
KamranS11 made it! (author)2017-04-12

Sir, I did all the steps but Card is not getting detected

author
Johnson Selva made it! (author)Johnson Selva2017-04-12

Will you able to send me the screenshot and your connection photo.... ???

author
KamranS11 made it! (author)KamranS112017-04-12

yes I will.

author
Johnson Selva made it! (author)Johnson Selva2017-04-13

mail me everything at joshstaivin@gmail.com with Subject as Arduino

author
KamranS11 made it! (author)KamranS112017-04-13

I have sent kindly check it

author
KamranS11 made it! (author)KamranS112017-04-13

woooh !!! ho gya ... Thanks ... I had Arduino problem... I changed the board and it worked

author
Johnson Selva made it! (author)Johnson Selva2017-04-18

Well done bro...

author
sarmadgulzar made it! (author)2017-03-01

It works. Thank you boy :)

A.jpgB.jpg
author
Johnson Selva made it! (author)Johnson Selva2017-03-01

Awesome work brother. glad you make it perfectly... :-D

author
sarmadgulzar made it! (author)2017-03-01

Awesome :)

author
Johnson Selva made it! (author)Johnson Selva2017-03-01

Tanx Bro...

author
FF9559 made it! (author)2017-02-24

It's work perfectly for me

IMG_2767.JPG
author
Johnson Selva made it! (author)Johnson Selva2017-02-24

nice work dude. I'm Glad you made it ... :-D :-D

author
FF9559 made it! (author)2017-02-24

Thanks for this tuto

Do you know if RC522 can Read / Write NFC Tag ?

author
Johnson Selva made it! (author)Johnson Selva2017-02-24

I think we can only read data and cant write anything...

author
FF9559 made it! (author)FF95592017-02-24

Ok thanks

And do you know if when you buy NFC Tag, is there a unique uuid inside ?

author
Ajo T William made it! (author)2017-02-01

Can we connect two or more RFID in a single Arduino board

author
Johnson Selva made it! (author)Johnson Selva2017-02-01

yes you can but i did not tried it before but its possible i think by using master slave connection in Arduino Mega....

author
fear_knight made it! (author)2016-11-07

Thanks the system works.

But having a hard time reading the ID cards and NFC tags, not much prob with the key tags. Any way to increase the range?

author
Johnson Selva made it! (author)Johnson Selva2016-11-08

Both NFC and ID cards works fine for me...

what range did you mean brother tag ID value ?? if yes try changing loop value as for(int i=0;i<10;i++) instead of i<5 . . .

author
fear_knight made it! (author)fear_knight2016-11-09

Not the length of the value, the rfid reading range. For example you have to make the RC522 touch to the NFC to scan, but it can read it from about 2 cm distance in miguelbalboa's code. If you can fix this, your code will be perfect bro.

author
Johnson Selva made it! (author)Johnson Selva2016-11-11

Actually the range that you mean, it doesn't depend on the code. it only depends on the hardware you use.

try suitable module other than RFID-RC522 if any available in the market...

author
DIY Hacks and How Tos made it! (author)2016-10-23

Cool. I really need to try out using RFID readers with some of my future Arduino projects. I think that they could be really useful.

author
Johnson Selva made it! (author)Johnson Selva2016-10-23

Tanx dude ask me if you need anything...