Introduction
RFID (Radio Frequency Identification) uses electromagnetic fields to identify and track tags. The RC522 module is an affordable way to add RFID to your projects.
Wiring (SPI)
| RC522 Pin | Arduino Uno Pin |
|---|---|
| SDA (SS) | 10 |
| SCK | 13 |
| MOSI | 11 |
| MISO | 12 |
| IRQ | Not Connected |
| GND | GND |
| RST | 9 |
| 3.3V | 3.3V |
Warning: RC522 operates at 3.3V. Connecting to 5V will damage it!
Libraries
Install MFRC522 by GithubCommunity in Library Manager.
The Code
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Scan your RFID card...");
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent() || !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();
}