Arduino Mega RC522 RFID Simulator: Wiring, Code and a Live Circuit
This is an RC522 (MFRC522) RFID reader wired to an Arduino Mega 2560 over SPI, running live with the MFRC522 library: open it in the editor, lay a card on the reader, and the serial monitor prints its UID.
Seven wires: 3.3 V, GND, the four SPI lines to the Arduino Mega’s SPI pins, and RST to pin 5.
- 5 V logic
- Library: MFRC522.h
- SS on pin 53
- 3.3 V module
Wiring the RC522 RFID to the Arduino Mega
Every connection in the circuit above, with the board’s own pin names.
| RC522 RFID pin | Arduino Mega pin | Why |
|---|---|---|
| 3.3V | 3.3V | The MFRC522 is a 3.3 V chip. Never 5 V on this pin. |
| GND | GND | Common ground. |
| SDA (SS) | pin 53 | Chip select. Labeled SDA on the module, but this is SPI, not I2C. |
| SCK | pin 52 | Arduino Mega’s SPI clock. |
| MOSI | pin 51 | Data from the board to the reader. |
| MISO | pin 50 | Data from the reader to the board. |
| RST | pin 5 | Reset, held high while the reader runs. |
| IRQ | not connected | The library polls instead. |
The module is powered from the Arduino Mega’s 3.3V pin, but the SPI lines come from 5 V pins. Many RC522 modules survive that, and countless Uno tutorials wire it so, but the chip is rated for 3.3 V logic; a level shifter on SCK, MOSI, SS and RST is the safe way on real hardware.
Pins to leave alone on the Arduino Mega
Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.
On a real bench
The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The RC522 RFID uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#include <SPI.h>
#include <MFRC522.h>
const int SS_PIN = 53; // SDA on the module, pin 53
const int RST_PIN = 5; // pin 5
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin(); // SCK 52, MISO 50, MOSI 51
rfid.PCD_Init();
Serial.println("Hold a card to the reader");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
delay(50);
return;
}
Serial.print("Card UID:");
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
rfid.PICC_HaltA(); // done with this card until it is lifted
}
SPI.begin() starts the Arduino Mega’s SPI bus on its own pins (SCK 52, MISO 50, MOSI 51), and MFRC522 rfid(SS_PIN, RST_PIN) adds the chip select and reset. PCD_Init() resets the reader and turns its antenna on.
PICC_IsNewCardPresent() asks whether a card has entered the field and PICC_ReadCardSerial() reads its UID, four or seven bytes. The sketch prints them in hex with a leading zero where needed, then PICC_HaltA() puts the card to sleep so it is reported once.
Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.
Common mistakes with the RC522 RFID on the Arduino Mega
Powering it from 5V
The 3.3V pin means it: 5 V there damages the MFRC522. If the module gets warm or reports a version of 0x00 or 0xFF, check this first.
Using the Uno’s SPI pins
Tutorials wire SCK, MISO and MOSI to 13, 12 and 11. On the Mega the hardware SPI pins are 52, 50 and 51 (and the ICSP header), so the reader never answers on the Uno pins.
Expecting to read bank cards
The RC522 reads 13.56 MHz MIFARE tags and cards. Contactless bank cards answer, but only with a random ID each time, and 125 kHz key fobs not at all.
Questions and answers
Can I simulate an RC522 RFID reader with an Arduino Mega 2560?
Yes. The circuit at the top of this page is an Arduino Mega 2560 with a RC522 RFID reader, running in your browser on a simulated ATmega2560. The simulated reader has four cards with fixed UIDs; click to lay one on the antenna. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Which pins does the RC522 use on the Arduino Mega?
SPI: SCK on pin 52, MISO on pin 50, MOSI on pin 51. SS and RST can be any digital pins; this page uses pin 53 and pin 5.
How do I make an access control lock?
Compare the UID bytes with a list of allowed cards, and drive a relay or a servo when one matches. Keep in mind that a UID alone is easy to copy, so it is a learning project, not a security one.
Can I write to the card?
The library has MIFARE_Write() for MIFARE Classic blocks after authentication. This simulated reader models reading UIDs only.
Build your own RC522 RFID project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.