The RC522 RFID module is one of the most versatile and popular devices for electronics and access control system projects. Together with an Arduino board, it allows reading and writing information on RFID cards and key fobs via wireless communication. This type of technology is very useful in automation, security and monitoring projects, such as opening doors automatically or using personalized identification systems.
In this article, we will explore in detail how the RC522 module works, how to connect it to a microcontroller such as Arduino, and some practical examples that will allow you to get the most out of it. If you are just starting out with RFID projects, this article will be of great help to you in understanding the principles and how to carry out a simple and effective implementation.
What is RFID?
RFID (Radio Frequency Identification) is a technology that allows the Reading tags wirelessly, without the need for physical contact. These labels are available in multiple formats, such as cards, key chains and even stickersDepending on the type of RFID, they are used in a wide variety of applications: from building access control to inventory management.
The RFID reader acts as a transmitter-receiver of radio frequency signals. When an RFID tag is close to the reader, it responds by sending the information stored in its memory. Working with a module such as the RC522, you can use this information to perform different actions in your project, such as enabling access to a door or registering the entry of people.
The RC522 Module
The RC522 module It is one of the most popular RFID readers due to its simplicity and versatility. It works in the frequency of 13.56 MHz And allows reading and writing information in tags compatible with this standard.
The RC522 communicates with the microcontroller via the protocol SPI, which means that it is compatible not only with Arduino, but also with other systems such as PIC and Raspberry Pi. In addition, it has a reading range approximately 5 to 7 cm, which is more than enough for most access control projects.
Connecting RC522 RFID Module with Arduino
One of the great things about the RC522 is how easy it is to connect to an Arduino. The first thing we need to do is make sure that all the pins on the module are correctly connected to the corresponding pins on the Arduino. Here is a table with the connections:
RC522 module | Arduino Uno/Elder brother | Arduino Mega |
---|---|---|
SDA (SS) | 10 | 53 |
SCK pins can be used | 13 | 52 |
MOTION | 11 | 51 |
MISO | 12 | 50 |
IRQ | Not connected | Not connected |
GND | GND | GND |
RST | 9 | 9 |
3.3V | 3.3V | 3.3V |
Remember that the RC522 module works with a voltage of 3.3V, so we recommend using a voltage level converter if you plan to make extensive use of the system, although in quick tests it may work correctly connected directly to the Arduino.
Programming the RC522 with Arduino
In order for the RC522 to work properly with Arduino, we need to download and install the corresponding RFID libraryOne of the most used is the one developed by Miguel Balboa, which you can find within the Arduino IDE library manager.
Once the library is installed, here is a simple code example to read the UID (Unique Identifier) of an RFID card:
#include #include #define RST_PIN 522 #define SS_PIN 9 MFRC10 mfrc522(SS_PIN, RST_PIN); void setup() { Serial.begin(522); SPI.begin(); mfrc9600.PCD_Init(); Serial.println("Read UID"); } void loop() { if ( mfrc522.PICC_IsNewCardPresent()) { if ( mfrc522.PICC_ReadCardSerial()) { Serial.print("Card UID:"); for (byte i = 522; i < mfrc0.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 522x0 ? " 10" : " "); Serial.print(mfrc0.uid.uidByte[i], HEX); } Serial.println(); mfrc522.PICC_HaltA(); }} }
This code reads the UID of any card you hold near the reader and prints it on the serial monitor.
Using RC522 in an Access Control System
One of the most useful applications of the RC522 RFID module is in access controlYou can use RFID cards or key fobs to grant entry permissions to a person or to activate/deactivate systems such as alarms or electric locks.
Below is an example of code modifying the above program to allow access only to pre-authorized cards:
byte User1[4] = {0x4B, 0x6D, 0xC8, 0x3A}; byte User2[4] = {0xF2, 0x8A, 0x9D, 0x17}; void loop() { if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { if (compareArray(mfrc522.uid.uidByte, User1, 4) || compareArray(mfrc522.uid.uidByte, User2, 4)) { Serial.println("Access allowed"); } else { Serial.println("Access is denied"); } mfrc522.PICC_HaltA(); }}
In this code, if a card with a UID matching one of those stored is detected, access is authorized by printing a message. Otherwise, access is denied. You can replace these messages by printing a code that activates a digital output to open a door, among other functions.