Arduino RC522 MIFARE Classic Write and Read: Wiring, Code and Simulator
To build this Arduino RC522 MIFARE Classic Write and Read project you need an Arduino Uno and an RC522 RFID reader. Authenticates sector 1 with the factory key A, reads block 4, writes a tap counter to it and reads it back. Make the 7 connections in the wiring table, upload the Arduino C++ code, and tap the white card a few times (take it away in between): the count is stored on the card. The NFC sticker fails authentication: it is not a MIFARE Classic.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | RC522 RFID reader | 13.56 MHz RFID/NFC reader on SPI: 3.3V, GND, SDA (chip select) and RST to any pins, SCK/MOSI/MISO to the board’s SPI pins. Tap a card from the Modules tab (or set "Card on the reader"): a white MIFARE Classic card, a blue key fob, or an NTAG213 sticker. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno 3.3V | RC522 RFID reader 3.3V |
| 2 | Arduino Uno GND | RC522 RFID reader GND |
| 3 | Arduino Uno pin 10 | RC522 RFID reader SDA |
| 4 | Arduino Uno pin 9 | RC522 RFID reader RST |
| 5 | Arduino Uno pin 13 | RC522 RFID reader SCK |
| 6 | Arduino Uno pin 11 | RC522 RFID reader MOSI |
| 7 | Arduino Uno pin 12 | RC522 RFID reader MISO |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add an RC522 RFID reader from the parts palette.
- Wire 1. Connect Arduino Uno 3.3V to RC522 RFID reader 3.3V.
- Wire 2. Connect Arduino Uno GND to RC522 RFID reader GND.
- Wire 3. Connect Arduino Uno pin 10 to RC522 RFID reader SDA.
- Wire 4. Connect Arduino Uno pin 9 to RC522 RFID reader RST.
- Wire 5. Connect Arduino Uno pin 13 to RC522 RFID reader SCK.
- Wire 6. Connect Arduino Uno pin 11 to RC522 RFID reader MOSI.
- Wire 7. Connect Arduino Uno pin 12 to RC522 RFID reader MISO.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Tap the white card a few times (take it away in between): the count is stored on the card. The NFC sticker fails authentication: it is not a MIFARE Classic.
Arduino C++ code
// MIFARE Classic: authenticate with the factory key, write a block, read it back.
// Tap the white card or the blue fob (MIFARE Classic 1K). The NFC sticker is an NTAG, not Classic.
#include <SPI.h>
#include <MFRC522.h>
MFRC522 rfid(10, 9);
MFRC522::MIFARE_Key key;
const byte BLOCK = 4; // sector 1, block 0
unsigned int taps = 0;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; // factory key A
Serial.println(F("Tap a MIFARE Classic card."));
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
MFRC522::StatusCode s = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, BLOCK, &key, &rfid.uid);
if (s != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(rfid.GetStatusCodeName(s));
} else {
byte buf[18];
byte size = sizeof(buf);
// What is stored now, then write a new message.
if (rfid.MIFARE_Read(BLOCK, buf, &size) == MFRC522::STATUS_OK) {
Serial.print(F("Block 4 was: "));
for (byte i = 0; i < 16; i++) Serial.write(buf[i] >= 32 && buf[i] < 127 ? buf[i] : '.');
Serial.println();
}
taps++;
byte msg[16];
snprintf((char *)msg, 16, "Tapped %u times", taps);
for (byte i = strlen((char *)msg); i < 16; i++) msg[i] = ' ';
s = rfid.MIFARE_Write(BLOCK, msg, 16);
Serial.print(F("Write: "));
Serial.println(rfid.GetStatusCodeName(s));
size = sizeof(buf);
if (rfid.MIFARE_Read(BLOCK, buf, &size) == MFRC522::STATUS_OK) {
Serial.print(F("Block 4 now: "));
for (byte i = 0; i < 16; i++) Serial.write(buf[i]);
Serial.println();
}
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
What to try
Tap the white card a few times (take it away in between): the count is stored on the card. The NFC sticker fails authentication: it is not a MIFARE Classic.
Frequently asked questions
How do I wire the RC522 MIFARE Classic Write and Read project on an Arduino Uno?
Make 7 connections: Arduino Uno 3.3V to RC522 RFID reader 3.3V; Arduino Uno GND to RC522 RFID reader GND; Arduino Uno pin 10 to RC522 RFID reader SDA; Arduino Uno pin 9 to RC522 RFID reader RST; Arduino Uno pin 13 to RC522 RFID reader SCK; Arduino Uno pin 11 to RC522 RFID reader MOSI; Arduino Uno pin 12 to RC522 RFID reader MISO.
What parts do I need for this Arduino project?
An Arduino Uno and an RC522 RFID reader.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
Authenticates sector 1 with the factory key A, reads block 4, writes a tap counter to it and reads it back.
What is the default key of a MIFARE Classic card?
FF FF FF FF FF FF for both key A and key B in every sector trailer. Authenticate a sector with it before reading or writing its blocks.
Why does PCD_Authenticate fail with a timeout?
The key is wrong, the card moved, or it is not a MIFARE Classic (an NTAG sticker has no sector keys). A wrong key gets no answer, so the reader’s timer runs out.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator