pcbway




RC522 with arduino,RFID with Arduino,RC522 arduino,Arduino rc522Hello friends, hope you all are fine and having fun with your lives. Today’s post is about interfacing of RFID module RC522 with Arduino. RC522 is very simple yet effective module. It is an RFID module and is used for scanning RFID cards. Its a new technology and is expanding day by day. Now-a-days it is extensively used in offices where employees are issued an RFID card and their attendance is marked when they touch their card to rfid reader. We have seen it in many movies that when someone places ones card over some machine then door opens or closes. In short, its a new emerging technology which is quite useful.

I recently get a chance to work on a project in which I have to use RFID reader to scan cards. In this project I have used it for for student attendance so I thought to share it on our blog so that other engineers could get benefit out it.

Let’s first have a little introduction of RFID and then we will look into how to interface RC522 with Arduino. RFID is the abbreviation of Radio frequency identification. RFID modules use electromagnetic fields to transfer data between card and the reader. Different tags are attached to objects and when we place that object in front of the reader, the reader reads that tags.Another benefit of RFID is that it doesn’t require to be in a line of sight to get detected. As in barcode, the reader has to be in the line of sight to the tag and then it can scan but in RFID there’s no such restriction. So, let’s get started with Interfacing of RFID RC522 with Arduino.

Interfacing of RFID RC522 with Arduino.

Now let’s start with the interfacing of RFID RC522 with Arduino. There are many different RFID modules available in the market. The RFID module, which I am gonna use in this project, is RFID-RC522. Its quite easy to interface and works pretty fine. This module has total 8 pins as shown in the below figure:

RC522 with arduino,RFID with Arduino,RC522 arduino,Arduino rc522

  • SDA
  • SCK
  • MOSI
  • MISO
  • IRQ
  • GND
  • RST
  • 3.3V

It normally works on SPI protocol, when interfaced with Arduino board. Interfacing of Arduino and RC522 module is shown in below figure:

RC522 with arduino,RFID with Arduino,RC522 arduino,Arduino rc522

  • The pin configuration is as follows:

RC522 with arduino,RFID with Arduino,RC522 arduino,Arduino rc522

  • Now, I suppose that you have connected your RFID module with Arduino as shown in above figure and table, which is quite simple. You just need to connect total 7 pins, IRQ is not connected in our case.
  • Now next step is the coding, so first of all, download this Arduino library for RFID RC522 module.

Note:

  • Its a third party library, we haven’t designed it, we are just sharing it for the engineers.

Download Arduino Library for RFID




  • Now coming to the final step. Upload the below code into your Arduino UNO.

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9
#define SS_PIN          10
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup()
{

SPI.begin();
mfrc522.PCD_Init();
}

void loop() {
RfidScan();
}

void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? ” 0″ : ” “);
Serial.print(buffer[i], HEX);
}
}

void RfidScan()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
return;

if ( ! mfrc522.PICC_ReadCardSerial())
return;
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
}

  • Now using this code, you can read the RFID no of your card quite easily. Now the main task is to use that number and distinguish them so for that I changed the dump_byte_array function a little, which is given below:

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9
#define SS_PIN          10
MFRC522 mfrc522(SS_PIN, RST_PIN);
int RfidNo = 0;

void setup()
{

SPI.begin();
mfrc522.PCD_Init();
}

void loop() {
RfidScan();
}

void dump_byte_array(byte *buffer, byte bufferSize)
{
Serial.print(“~”);
if(buffer[0] == 160){RfidNo = 1;Serial.print(RfidNo);}
if(buffer[0] == 176){RfidNo = 2;Serial.print(RfidNo);}
if(buffer[0] == 208){RfidNo = 3;Serial.print(RfidNo);}
if(buffer[0] == 224){RfidNo = 4;Serial.print(RfidNo);}
if(buffer[0] == 240){RfidNo = 5;Serial.print(RfidNo);}
Serial.print(“!”);
while(1){getFingerprintIDez();}
}

void RfidScan()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
return;

if ( ! mfrc522.PICC_ReadCardSerial())
return;
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
}

  • Now using the first code I get the card number for all RFID cards and then in second code I used these numbers and place the check now when first card will be placed it will show 1 on the serial port and so on for other cards.
  • So, what you need to do is to use the first code and get your card number and then place them in second code and finally distinguish your cards.
  • Quite simple and easy to work.
  • Hope I have explained it properly but still if you get any problem ask me in comments.

31 Comments

  1. Hi, I keep getting the errors Arduino: 1.6.6 (Windows 10), Board: “Arduino/Genuino Uno”

    RFID_01:22: error: stray ‘\342’ in program

    Serial.print(buffer[i] < 0x10 ? � 0" : � “);

    ^

    RFID_01:22: error: stray '\200' in program

    RFID_01:22: error: stray '\235' in program

    RFID_01:22: error: missing terminating " character

    C:\Users\skiidoggy\Documents\Arduino\RFID_01\RFID_01.ino: In function 'void loop()':

    RFID_01:16: error: 'RfidScan' was not declared in this scope

    RfidScan();

    ^

    C:\Users\skiidoggy\Documents\Arduino\RFID_01\RFID_01.ino: In function 'void dump_byte_array(byte*, byte)':

    RFID_01:23: error: expected ':' before 'Serial'

    Serial.print(buffer[i], HEX);

    ^

    RFID_01:23: error: expected ')' before ';' token

    Serial.print(buffer[i], HEX);

    ^

    exit status 1
    stray '\342' in program

    • I had the same problem and found that there is an issue with the font or something during the copy/paste process in regards to the double quotes. So, just remove the 4 double quotes and type them again in your code which should resolve that problem!

    • This is caused by using Cut and Paste on the code, to following characters are incorrectly copied:
      Serial.print(“!”);
      should be
      Seria.print(“!”);

      Note the ” ” the ones copied are unicode characters and cause the error ‘\342’

  2. Is it possible to connect multiple readers to a single arduino? If so how can that be done. I can get one to work just find, once I introduce another I get no response.

    • Hi,

      Yes you canconnect multiple RFID with Arduino. In this case you need to add a virtual SPI port. Add me on Skype and we will discuss it in detail. My skype id is theeenggprojects.

      Thanks.

  3. Hi! First of all, thanks for the project! I’m trying to start ‘playing’ with Arduino and this RFID module was the chosen one to be the first. I’ve uploaded the code to my UNO but I’m not able to see the card numbers anywhere. Tried to use the Serial Monitor, but nothing happens. How/Where can I see this? Thanks in advance!

    • You should use the first code given in the article. This code will give you the RFID value of card in your Serial Monitor. In the second code, place that card value in the code and it will place the check.

  4. in setup I added
    Serial.begin(9600); // vince added
    Otherwise I got nothing out.

    Also made additional changes to help readablity

    void dump_byte_array(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
    // Serial.print(buffer[i] < 0x10 ? ” 0″ : ” “);
    Serial.print(buffer[i] println
    }
    Serial.println(); // vince added
    }

    void RfidScan()
    {
    if ( ! mfrc522.PICC_IsNewCardPresent())
    return;

    if ( ! mfrc522.PICC_ReadCardSerial())
    return;
    Serial.println(“MFRC55 buffer”); // vince added
    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    }

  5. Hi, i have this error message; can you help me?, please

    Arduino:1.8.3 (Windows 10), Tarjeta:”Arduino/Genuino Uno”

    C:\Users\Juan Figueroa!\Documents\Arduino\LECTOR\lector2\lector2.ino: In function ‘void dump_byte_array(byte*, byte)’:

    lector2:29: error: ‘getFingerprintIDez’ was not declared in this scope

    while(1){getFingerprintIDez();}

  6. unable to find you on skype
    exit status 1
    ‘getFingerprintIDez’ was not declared in this scope
    what to do for this

  7. Arduino: 1.8.4 (Windows 10), Board: “Arduino/Genuino Uno”

    C:\Users\admin\Documents\Arduino\sketch_aug31a\sketch_aug31a.ino:2:21: fatal error: MFRC522.h: No such file or directory

    #include

    ^

    compilation terminated.

    exit status 1
    Error compiling for board Arduino/Genuino Uno.

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    this error is coming while verifying this code, can u help me with this

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment