English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

RFID Radio Frequency Identification Module

The RFID module integrates an RC522 RFID chip and transfers data over I2C. The I2C address is 0x28, and the operating frequency is 13.56 MHz. It supports card reading, writing, identification, logging, and RF card encoding and authorization.

Hardware Connection

Modules that communicate over I2C can be connected to the Basic default I2C pins, PIN21 (SDA) and PIN22 (SCL).

Example Program

This example reads the UID of compatible cards and displays it in hexadecimal on the screen and serial monitor. The serial baud rate is 115200.

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#include <M5Unified.h>
#include <Wire.h>
#include "MFRC522_I2C.h"

constexpr uint8_t RFID_ADDR = 0x28;
constexpr int SDA_PIN = 21;
constexpr int SCL_PIN = 22;

constexpr uint8_t RFID_RST_PLACEHOLDER = 0xFF;

MFRC522_I2C mfrc522(
    RFID_ADDR,
    RFID_RST_PLACEHOLDER,
    &Wire
);

bool readerOK = false;

bool checkI2C() {
  Wire.beginTransmission(RFID_ADDR);
  return Wire.endTransmission() == 0;
}

bool initReaderSoftware() {
  if (!checkI2C()) {
    return false;
  }

  // CommandReg = SoftReset
  mfrc522.PCD_WriteRegister(mfrc522.CommandReg, 0x0F);
  delay(50);

  const uint32_t start = millis();

  while (mfrc522.PCD_ReadRegister(mfrc522.CommandReg) & 0x10) {
    if (millis() - start >= 500) {
      return false;
    }
    delay(1);
  }

  const byte version =
      mfrc522.PCD_ReadRegister(mfrc522.VersionReg);

  if (version == 0x00 || version == 0xFF) {
    return false;
  }

  mfrc522.PCD_WriteRegister(mfrc522.TxModeReg, 0x00);
  mfrc522.PCD_WriteRegister(mfrc522.RxModeReg, 0x00);
  mfrc522.PCD_WriteRegister(mfrc522.ModWidthReg, 0x26);

  mfrc522.PCD_WriteRegister(mfrc522.TModeReg, 0x80);
  mfrc522.PCD_WriteRegister(mfrc522.TPrescalerReg, 0xA9);
  mfrc522.PCD_WriteRegister(mfrc522.TReloadRegH, 0x03);
  mfrc522.PCD_WriteRegister(mfrc522.TReloadRegL, 0xE8);

  mfrc522.PCD_WriteRegister(mfrc522.TxASKReg, 0x40);
  mfrc522.PCD_WriteRegister(mfrc522.ModeReg, 0x3D);

  const byte txControl =
      mfrc522.PCD_ReadRegister(mfrc522.TxControlReg);

  mfrc522.PCD_WriteRegister(
      mfrc522.TxControlReg,
      byte(txControl | 0x03)
  );

  return true;
}

void setup() {
  M5.begin(M5.config());
  Serial.begin(115200);
  delay(100);

  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.setTextSize(2);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.setCursor(10, 10);
  M5.Display.println("RFID");

  Wire.begin(SDA_PIN, SCL_PIN, 100000);

  readerOK = initReaderSoftware();

  M5.Display.setCursor(10, 45);

  if (!readerOK) {
    Serial.println("RFID initialization failed.");
    Serial.println(
        "Check power, GND, SDA=21, SCL=22, address 0x28,"
        " and the reader reset circuit."
    );

    M5.Display.setTextColor(TFT_RED, TFT_BLACK);
    M5.Display.println("Reader init failed");
    return;
  }

  const byte version =
      mfrc522.PCD_ReadRegister(mfrc522.VersionReg);

  Serial.printf("MFRC522 version: 0x%02X\n",
                static_cast<unsigned int>(version));
  Serial.println("Place a compatible card...");

  M5.Display.println("Place a card...");
}

void loop() {
  M5.update();

  if (!readerOK) {
    delay(100);
    return;
  }

  if (!mfrc522.PICC_IsNewCardPresent() ||
      !mfrc522.PICC_ReadCardSerial()) {
    delay(50);
    return;
  }

  Serial.print("Card UID: ");

  M5.Display.fillRect(
      0, 80, M5.Display.width(), 80, TFT_BLACK
  );
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.setCursor(10, 80);
  M5.Display.println("UID:");
  M5.Display.setCursor(10, 110);

  for (byte i = 0; i < mfrc522.uid.size; ++i) {
    const unsigned int value = mfrc522.uid.uidByte[i];
    Serial.printf("%02X ", value);
    if (i == 7) {
      M5.Display.setCursor(10, 135);
    }

    M5.Display.printf("%02X ", value);
  }

  Serial.println();

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();

  delay(500);
}

After uploading the program, you should see the following result.

Page Tools
PDF
On This Page