Skip to content
SensorLGPL-2.1v1.4.11

MFRC522

by GithubCommunity

This library drives the MFRC522 RFID/NFC reader chip over SPI, handling the register-level commands needed to detect a card, read its unique ID (UID), and read/write data blocks on Mifare Classic and Ultralight cards. `PICC_IsNewCardPresent()` and `PICC_ReadCardSerial()` are the two calls behind virtually every "scan a card" project. The catch that trips people up is the reset pin timing — the RC522 needs a hardware reset pulse via the RST pin before it will respond, which is why `mfrc522.PCD_Init()` must run after `SPI.begin()` in setup, not before. Mifare Classic cards also use per-sector authentication keys (default `FFFFFFFFFFFF`), which trips people up if they try reading a sector that was previously locked with a custom key.

Installation

Arduino IDE — Library Manager

Sketch → Include Library → Manage Libraries → search MFRC522

PlatformIO

lib_deps = miguelbalboa/MFRC522

Supported boards

Arduino UnoESP32STM32 Blue Pill

Examples

Read a card UID

The standard pattern behind every RFID access-control or attendance-tracking project.

#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()) return;
  Serial.print("UID: ");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
  mfrc522.PICC_HaltA();
}