Interfacing RFID-RC522 With Arduino MEGA a Simple Sketch

103,447

47

88

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

About: Dreamer...

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

Attachments

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

10 People Made This Project!

Recommendations

  • Electronics Contest

    Electronics Contest
  • Home and Garden Contest

    Home and Garden Contest
  • Tinkercad to Fusion 360 Challenge

    Tinkercad to Fusion 360 Challenge

88 Comments

0
CharlesMsovella
CharlesMsovella

Question 7 months ago on Step 4

hello there , i would love to build a access system that includes the RFID Technology , any adviceto that

0
Johnson Selva
Johnson Selva

Answer 7 months ago

What kind of access tech you wish to build... I can help you with both Arduino based and python...

0
CWZ
CWZ

4 years ago

create job Johnson. Its working perfect. Thanks for sharing it.

0
Johnson Selva
Johnson Selva

Reply 4 years ago

Thanks For Your Comment Buddy...

0
minhphuoc
minhphuoc

12 months ago

Thank you Johnson,
Hope you can post more example whích more complicated

0
Johnson Selva
Johnson Selva

Reply 11 months ago

Yep will post more soon

0
Lushalini
Lushalini

Question 11 months ago

Hi brother...why this error showing...can u help me to solve this

A3541B17-B02C-4578-9E0F-C7D09C614746.jpeg
0
Johnson Selva
Johnson Selva

Answer 11 months ago

That's because you are including the header file that doesn't exist on your project folder. You declared the header queue.h on your program but it's not in your project folder. If you have the file in the folder make sure the spelling is right. The program is also case sensitive. Renaming the header file will work too..

0
rohitsharma261199
rohitsharma261199

1 year ago

Hi, I am facing difficulty to install the library. Can u please help me to install it

0
Johnson Selva
Johnson Selva

Reply 1 year ago

Can you tell me what error you are getting during library install?

0
rohitsharma261199
rohitsharma261199

Reply 1 year ago

No brother it is not showing in the library section. Can u please tell me the location where I can paste the library file?

0
Johnson Selva
Johnson Selva

Reply 1 year ago

Extract the zip file and then copy paste the extracted folder on MyDocuments->Arduino->Library folder

If its not helping ... Take screenshot and upload it here. I will take a look😁

0
rohitsharma261199
rohitsharma261199

Reply 1 year ago

Thank u brother. Now it is working.

0
AmjadFayad
AmjadFayad

2 years ago

Hi,
I do everything is correct with the connection but i still dont recive any thing in the monitor screen.
0
Johnson Selva
Johnson Selva

Reply 1 year ago

Check the board rate...
Serial.begin(9600);

the same should be in serial monitor.

0
Rjmlondon
Rjmlondon

Question 1 year ago on Step 4

Hi, can you please explain how to set up multiple RFID readers, say 4, using uno, 522 rfid type. Please include if any hardware required to make all 4 rfid work together. I have set this up 1 works fine, 2 or more all stop working. Thanks

0
Johnson Selva
Johnson Selva

Answer 1 year ago

may be using dedicated lines for signal will work. i'm not sure about that either... let me try it for you and back to soon..

0
Beny110
Beny110

Question 3 years ago on Introduction

hi,
Can you help me how to connect 4 rfid senzors? i have arduino mega adk but when i looking for how to connect and which code i need use i didnt find it or it didnt function. (sorry for my english)

0
Johnson Selva
Johnson Selva

Answer 3 years ago

Actually you can't connect 4 at once on Mega as it has only two SPI on a single board. actually i'm not sure 3 or 2 SPI in Mega search for SPI on MEGA hope it helps sorry for late response