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

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

5 People Made This Project!

Recommendations

  • Cardboard Challenge

    Cardboard Challenge
  • Epilog X Contest

    Epilog X Contest
  • Safe and Secure Challenge

    Safe and Secure Challenge

58 Discussions

0
None
Beny110

Question 18 days 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
None
Johnson SelvaKamranK30

Answer 4 months ago

Sorry for late...

I Hope the below code works. let me know...

/*

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 card=228; //use first three digit of your card instead of 228

/* Create an instance of the RFID library */

RFID RC522(SDA_DIO, RESET_DIO);

void setup()

{

Serial.begin(9600);

SPI.begin();

RC522.init();

}

void loop()

{

byte i=0;

byte j=0;

byte k=0;

int ID;

if (RC522.isCard())

{

RC522.readCardSerial();

Serial.print(RC522.serNum[i],DEC);

ID=RC522.serNum[0];

Serial.println(" ");

if(card==ID)

{

digitalWrite(13,HIGH);

}

else

{

digitalWrite(13,LOW);

}

Serial.println();

Serial.println();

}

delay(1000);

}

0
None
kenny.frouinsti2d

Question 10 months ago

Hello,

Do you have a tutorial on how to write on cards with your Library, because we found the write function but we don't know how to use it.

Thank you.

Kenny FROUIN

2 answers
0
None
HazyHazy

Question 11 months ago on Step 1

Hi Johnson,
Could you plz advise on how to connect cables with the RFID module.Do we need to solder them directly onto the module or solder the pin headers first?

Thanks
Hazy

1 answer
0
None
Johnson SelvaHazyHazy

Answer 10 months ago

Dude don't solder the wire directly pin goes first or you will face lot of problems... sorry for late reply...

0
None
NelmanV

1 year ago

Why my RFID Scanner can't detect the RFID Scanner?

1 reply
0
None
Johnson SelvaNelmanV

Reply 1 year ago

Of your reader can't read your card this may be bocause of connection problem make sure you plugged in the pins correctly and check out your power supply... If power supply exceeds 3.3v it may kill the readers IC...

0
None
WilliI1

1 year ago

can i change D50, D51, and D52 to other digital pin? what should i change on the source code? thanks

1 reply
0
None
Johnson SelvaWilliI1

Reply 1 year ago

No you can't if you change the pin numbers RFID will not work as it needs master slave connection....

0
None
امجدع

1 year ago

thanks , but how could be store the read card number to simple Visual Basic 6. text box in simple form? thanks