Interfacing an RC522 RFID Card Reader with Arduino

1,241 views

Introduction

In today’s world, the ability to wirelessly transfer data between objects is becoming increasingly important. One of the technologies that make this possible is Radio Frequency Identification (RFID). RFID is used in various applications, from tracking inventory to access control.

In this article, we will explore how to interface an RC522 RFID reader with Arduino, an open-source hardware platform, to create innovative IoT projects. We will cover the basics of RFID technology, and the necessary hardware and software components, and provide sample code and project ideas to help you get started. So, if you’re interested in learning how to integrate RFID with Arduino, keep reading!

What is RC522 RFID Card Reader?

The RC522 RFID card reader is a type of RFID reader module that operates at a frequency of 13.56 MHz. It is designed to read and write data on RFID tags and cards and can be easily interfaced with popular development boards such as Arduino and Raspberry Pi. The RC522 module includes an RFID antenna, a power supply, and a control interface, and uses the SPI (Serial Peripheral Interface) protocol to communicate with the host device. It supports ISO/IEC 14443A/MIFARE mode, which is a widely used RFID communication protocol.

RC522 RFID Card Reader
RC522 RFID Card Reader

Hardware Components

To interface an RC522 RFID reader with Arduino, you’ll need the following hardware components to get started:

ComponentsValueQty
Arduino UNO1
USB CableType A to B1
RFID ReaderRC5221
Jumper Wires1
BreadboardMini1

RC522 RFID Pinout

RC522 RFID-Pinout
RC522 RFID-Pinout
Pin NumberPin NameDescription
1VccUsed to Power the module, typically 3.3V is used
2RSTReset pin – used to reset or power down the module
3GroundConnected to the Ground of the system
4IRQInterrupt pin – used to wake up the module when a device comes into range
5MISO/SCL/TxMISO pin when used for SPI communication acts as SCL for I2c and Tx for UART.
6MOSIMaster out slave in pin for SPI communication
7SCKSerial Clock pin – used to provide a clock source
8SS/SDA/RxActs as Serial input (SS) for SPI communication, SDA for IIC and Rx during UART

RC522 RFID Reader Arduino Circuit

Make connections according to the circuit diagram given below.

RFID-RC522-Arduino-Circuits

Wiring / Connections

ArduinoRFID Sensor
5VVCC
GNDGND
D10SS
D13SCK
D11MOSI
D12MISO
D5RST

Installing Arduino IDE

After building the circuit above, you need to install Arduino IDE software from its official website Arduino. If you don’t know here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default).

If you don’t know here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

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

constexpr uint8_t RST_PIN = 32;          // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN =  15;         // Configurable, see typical pin layout above

MFRC522_SPI spiDevice = MFRC522_SPI(SS_PIN, RST_PIN);
MFRC522 mfrc522 = MFRC522(spiDevice);  // Create MFRC522 instance

// Specify the wiring/pins in more detail.
//
// MFRC522_SPI spiDevice = MFRC522_SPI(SS_PIN, RST_PIN, PIN_SCK, PIN_MISO, PIN_MOSI);
// MFRC522 mfrc522 = MFRC522(spiDevice);  
//
// Or do much the same - but also specify the bus
//
// SPI spiBus = SPI(HSPI);
// spiBus.begin(PIN_SCK, PIN_MISO, PIN_MOSI);
// MFRC522_SPI spiDevice = MFRC522_SPI(SS_PIN, RST_PIN, spiBus);
// MFRC522 mfrc522 = MFRC522(spiDevice);  
//
// And if neeed - also change the bus settings.
//
// SPI spiBus = SPI(HSPI);
// SPISettings spiSettings = SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0))
// MFRC522_SPI spiDevice = MFRC522_SPI(SS_PIN, RST_PIN, spiBus, spiBusSettings);
// MFRC522 mfrc522 = MFRC522(spiDevice);

void setup() {
  Serial.begin(9600);   // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin(14, 12, 13);
  mfrc522.PCD_Init();   // Init MFRC522
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    Serial.println("Bad read (was card removed too quickly?)");
    return;
  }


  if (mfrc522.uid.size == 0) {
    Serial.println("Bad card (size = 0)");
  } else {
    char tag[sizeof(mfrc522.uid.uidByte) * 4] = { 0 };
    for (int i = 0; i < mfrc522.uid.size; i++) {
      char buff[5]; // 3 digits, dash and \0.
      snprintf(buff, sizeof(buff), "%s%d", i ? "-" : "", mfrc522.uid.uidByte[i]);
      strncat(tag, buff, sizeof(tag));
    };
    Serial.println("Good scan: ");
    Serial.println(tag); 
  };
  // disengage with the card.
  //
  mfrc522.PICC_HaltA();
}

Code Explanation

This is an Arduino code for interfacing an RC522 RFID Reader with an Arduino board. The code starts with defining the pins for the RC522 module and creating an instance of the MFRC522 class. The setup() function initializes the serial communication with the PC, initializes the SPI communication, and initializes the MFRC522 module. The loop() function continuously looks for new RFID cards and selects one of the cards if found. It then reads the UID and displays it on the serial monitor. Finally, it disengages with the card using the PICC_HaltA() function.

Applications

  • Access control systems
  • Asset tracking and management
  • Supply chain management
  • Inventory management
  • Smart lock systems
  • Time and attendance systems
  • Library management systems
  • Electronic toll collection systems
  • Vehicle tracking and identification systems
  • Animal identification and tracking systems
  • Waste management and recycling systems.

These are just a few examples of the many possible applications of the RC522 RFID Reader.

Conclusion

Interfacing an RC522 RFID reader with an Arduino is a straightforward and practical way to integrate RFID technology into your projects. By following the steps outlined in this article, you can build a reliable and cost-effective RFID system that can be used in a wide range of applications.