Anda di halaman 1dari 9

Tutorial Module RFID Reader RC522

Posted by Tutorials 79 Comments

RFID (Radio Frequency IDentification) readers are currently receiving a great deal of
attention in identification systems, their use ranges from security systems, personnel access,
identification and logistics of products, such as electric door keys, among other applications.
Its operating principle is to pass a TAG, near an RFID reader, the TAG has the ability to send
information to the reader. This information can be from a simple code or a whole information
packet stored in the Tag's memory.

The TAGs come in different models, the most common are in cards and key chains, but they
also come as stickers and are even embedded in some products. The Tags internally have
an antenna and a microchip, responsible for carrying out the entire communication process,
the energy is obtained from the radiofrequency signal, which although the energy in the
signal is small, it is enough to make the microchip work, this is the reason why it is necessary
to bring them closer to a small distance usually less than 10 cm. But there are active Tags,
which incorporate batteries, these have a range of several meters.

RFID module RC522


It is the one that we will use in this tutorial, this module works as RFID Tag Reader and
Recorder.
This module uses a modulation and demodulation system of 13.56MHz, frequency that
currently uses RFID technology.

The module communicates through SPI, so it can be implemented with any microcontroller
with SPI interface, such as an Arduino.

Well, let's start putting together our project:

1. Connection between the RFID module and Arduino

Module Arduino Arduino


RC522 Uno, Nano Mega
Module Arduino Arduino
RC522 Uno, Nano Mega

SDA
10 53
(SS)

SCK 13 52

MOSI eleven 51

MISO 12 fifty

Not Not
IRQ
connected connected

GND GND GND

RST 9 9

3.3V 3.3V 3.3V


As seen in the connection, the module works with a voltage of 3.3V, so the logical part should
also work with the same voltage level, for testing and testing can be connected directly to the
Arduino pins (TTL level 5V ), but it is recommended to use voltage level converters .

2. Programming of Module RC522: Reading of the identification


code

In order to work the Module in Arduino it is necessary to download its corresponding library,
which we will use will be one of the most common ones, the one developed by Miguel
Balboa.
RFID library
Once downloaded, we import the library to our Arduino IDE, with this we are ready to
program.

Below is a sketch to read the identification code of our Tags

#include < SPI .h>


#include < MFRC522 .h>

#define RST_PIN 9 // Pin 9 for reset of RC522


#define SS_PIN 10 // Pin 10 for SS (SDA) of RC522
MFRC522 mfrc522 (SS_PIN, RST_PIN); // We create the object for the RC522

void setup () {
Serial . begin (9600); // We start the
SPI serial communication . begin (); // We start the SPI Bus
mfrc522. PCD_Init (); // We start the MFRC522
Serial . println ( "UID reading" );
}

void loop () {
// We check if there are new cards present
if ( mfrc522.PICC_IsNewCardPresent ())
{
// We select an
if card ( mfrc522.PICC_ReadCardSerial ())
{
// We send your
Serial UID serially . print ( "Card UID:" );
for ( byte i = 0; i <mfrc522.uid. size ; i ++) {
Serial . print (mfrc522.uid.uidByte [i] <0x10? "0" : "" );
Serial . print (mfrc522.uid.uidByte [i], HEX );
}
Serial . println ();
// We
finish reading the current card mfrc522. PICC_HaltA ();
}
}
}
As you can see it is easy to understand, but let's explain the functions related to the
RC522 module

Instanciar the RC522

#define RST_PIN 9 // Pin 9 for reset of RC522


#define SS_PIN 10 // Pin 10 for SS (SDA) of RC522
MFRC522 mfrc522 (SS_PIN, RST_PIN);

It is necessary to use the library MFRC522.h, it is only necessary to specify the pins
Reset and SDA (SS) of the module, the other pins work with the SPI pins of the Arduino.

Start the RC522

SPI . begin (); // We start the SPI Bus


mfrc522. PCD_Init (); // We start the MFRC522

This function starts and configures the RC522 for later reading, it is only necessary to call
it once so it is usually called in void setup ()

See if there is a card present

mfrc522. PICC_IsNewCardPresent ()

This function returns true or false depending on whether a card is present near the
RC522 module.

Select a card for reading

mfrc522. PICC_ReadCardSerial ()

This function is called when we want to communicate with a card, it returns a true value if
it manages to select a card for reading, otherwise it returns a false value.

Get the size of the identification code

mfrc522.uid. size

We return the size in bytes of the identification code of the selected card.

Read the identification code


mfrc522.uid.uidByte

To access the identification code it is advisable to index the variable for example
mfrc522.uid.uidByte [0] for the byte in the initial position.

Finish reading

mfrc522. PICC_HaltA ();

With this function we indicate that we have finished reading the present card and
IsNewCardPresent () will return false for this card as long as it is not removed. If after
removing the card, if the card is entered again it is considered as a new card.

In the sketch, the identification code is sent through the serial port, in order to visualize the
code it is necessary to open the Serial Monitor of the Arduino IDE.

3. A Simple Application: Access control using RFID


Well, seen the previous example, now apply it for an access control, which can be applied in
an electric door, safe, ignition system, disable alarms, etc.

For this example we are using 4 Tags of which only two must have access to the system. To
implement this we started from the previous sketch and we only added a few more lines at
the end of the program to compare the codes and determine if they have access or not.

The Sketch is shown below.

#include < SPI .h>


#include < MFRC522 .h>

#define RST_PIN 9 // Pin 9 for reset of RC522


#define SS_PIN 10 // Pin 10 for SS (SDA) of RC522
MFRC522 mfrc522 (SS_PIN, RST_PIN); /// We create the object for the RC522

void setup () {
Serial . begin (9600); // We start the
SPI serial communication . begin (); // We start the SPI Bus
mfrc522. PCD_Init (); // We start the MFRC522
Serial . println ( "Access control:" );
}

byte ActualUID [4]; // store the Tag code read


byte User1 [4] = {0x4D, 0x5C, 0x6A, 0x45}; // user code 1
byte User2 [4] = {0xC1, 0x2F, 0xD6, 0x0E}; // user code 2
void loop () {
// We check if there are new cards present
if ( mfrc522.PICC_IsNewCardPresent ())
{
// We select an
if card ( mfrc522.PICC_ReadCardSerial ())
{
// We send your
Serial UID serially . print (F ( "Card UID:" ));
for ( byte i = 0; i <mfrc522.uid. size ; i ++) {
Serial . print (mfrc522.uid.uidByte [i] <0x10? "0" : "" );
Serial . print (mfrc522.uid.uidByte [i], HEX );
CurrentUID [i] = mfrc522.uid.uidByte [i];
}
Serial . print ( "" );
// compare the UID to determine if it is one of our users
if (compareArray (ActualUID, User1))
Serial . println ( "Access granted ..." );
else if (compareArray (ActualUID, User2))
Serial . println ( "Access granted ..." );
else
Serial . println ( "Access denied ..." );

// We
finish reading the current card card mfrc522. PICC_HaltA ();

}
}

// Function to compare two vectors


boolean compareArray ( byte array1 [], byte array2 [])
{
if (array1 [0]! = array2 [0]) return ( false );
if (array1 [1]! = array2 [1]) return ( false );
if (array1 [2]! = array2 [2]) return ( false );
if (array1 [3]! = array2 [3]) return ( false );
return ( true );
}

And the result we can observe when passing the Tags and visualize it in the serial monitor.

Instead of sending it serially the confirmation can be programmed to activate a digital output,
which can be connected to an electrical sheet, an alarm, etc. It will depend on the particular
application you are working on.

It is good to clarify that although it meets the objective, it is not a 100% sure way, anyone
who knows the subject could clone the cards. For greater security it is necessary to work with
the blocks of the internal memory of the TAG, being able to configure a key for the reading, in
addition one could work with a code greater than the 4 bytes of the UID, even it could work
with several data. (The reading and writing of memory blocks will be seen in a next tutorial)
You can purchase the materials used in this tutorial in our store
RFID module RC522
RFID NFC Tag 13.56 MHz
Arduino Uno R3
Dupont cable, female to male 20cm x 20Und