0
0
AutocadHow-ToBeginner · 4 min read

How to Use RFID Reader RC522 with Arduino: Simple Guide

To use the RFID RC522 reader with Arduino, connect the reader's SPI pins to the Arduino SPI pins, include the MFRC522 library in your sketch, and use its functions to detect and read RFID tags. Initialize the reader in setup() and check for new cards in loop() to get the tag's UID.
📐

Syntax

Here is the basic syntax to initialize and read from the RC522 RFID reader using the MFRC522 library:

  • #include <SPI.h>: Includes SPI communication library.
  • #include <MFRC522.h>: Includes RFID reader library.
  • MFRC522 mfrc522(ssPin, rstPin);: Creates an RFID reader object with specified Slave Select and Reset pins.
  • mfrc522.PICC_IsNewCardPresent(): Checks if a new RFID card is near.
  • mfrc522.PICC_ReadCardSerial(): Reads the card's unique ID (UID).
arduino
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
  SPI.begin();           // Initialize SPI bus
  mfrc522.PCD_Init();    // Initialize MFRC522 reader
  Serial.begin(9600);    // Start serial communication
}

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

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  // UID is available in mfrc522.uid.uidByte array
}
💻

Example

This example shows how to read the UID of an RFID tag and print it to the Serial Monitor.

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
  Serial.begin(9600);   // Initialize serial communications
  SPI.begin();          // Initialize SPI bus
  mfrc522.PCD_Init();   // Initialize MFRC522
  Serial.println("Place your RFID card near the reader...");
}

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

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  Serial.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();

  mfrc522.PICC_HaltA(); // Stop reading
}
Output
Place your RFID card near the reader... Card UID: 04 A3 1B 2C 5D
⚠️

Common Pitfalls

Common mistakes when using the RC522 with Arduino include:

  • Incorrect wiring of SPI pins (MOSI, MISO, SCK, SS, RST).
  • Not initializing SPI before the RFID reader.
  • Forgetting to install or include the MFRC522 library.
  • Not calling mfrc522.PICC_HaltA() after reading a card, which can cause repeated reads.
  • Using wrong Slave Select (SS) or Reset (RST) pin numbers in code.

Always double-check wiring and pin definitions in your code.

arduino
/* Wrong way: Missing SPI.begin() and wrong SS pin */
#include <MFRC522.h>

#define SS_PIN 5  // Wrong pin
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  // SPI.begin(); // Missing SPI initialization
  mfrc522.PCD_Init();
}

void loop() {
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    Serial.println("Card detected");
    mfrc522.PICC_HaltA();
  }
}

/* Right way: Correct SPI and pins */
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
}

void loop() {
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    Serial.println("Card detected");
    mfrc522.PICC_HaltA();
  }
}
📊

Quick Reference

Here is a quick reference for connecting and programming the RC522 with Arduino:

Component PinArduino PinDescription
SDA (SS)10Slave Select pin for SPI communication
SCK13SPI Clock
MOSI11Master Out Slave In
MISO12Master In Slave Out
RST9Reset pin for RC522
3.3V3.3VPower supply (3.3 volts)
GNDGNDGround

Key Takeaways

Connect RC522 SPI pins correctly to Arduino SPI pins (SDA to 10, SCK to 13, MOSI to 11, MISO to 12, RST to 9).
Include and initialize the MFRC522 library and SPI in your Arduino sketch.
Use PICC_IsNewCardPresent() and PICC_ReadCardSerial() to detect and read RFID tags.
Call PICC_HaltA() after reading a card to avoid repeated reads.
Check wiring and pin definitions carefully to avoid common connection errors.