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.
Modules that communicate over I2C can be connected to the Basic default I2C pins, PIN21 (SDA) and PIN22 (SCL).
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.
#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.