A Three-Step Arduino RFID Guide to Building and Coding Your First Project Today

by ElectroScope Archive in Circuits > Arduino

38 Views, 2 Favorites, 0 Comments

A Three-Step Arduino RFID Guide to Building and Coding Your First Project Today

Arduino RFID Guide Thumbnail.jpg

Arduino is a game-changer for DIY electronics. Partner it with RFID, and voila! You unlock the ability to build smart, contactless systems like access control, attendance trackers, and inventory management tools, all from your workspace.

This is a complete Arduino RFID guide, where you’ll learn to connect and configure the RC522 RFID reader with Arduino, and also write code to read RFID tags.

Supplies

Arduino Board (Uno, Nano, Mega, or any compatible board with SPI pins)

Make sure your board has MOSI, MISO, SCK, and SS pins available.

RC522 RFID Reader Module

Operates at 3.3V and uses the MFRC522 chip. Includes antenna and onboard circuitry.

RFID Tags or Cards

MIFARE Classic cards or compatible RFID keyfobs to test reading and writing.

Jumper Wires

Male-to-male or male-to-female dupont wires for connections between Arduino and the RFID module.

Power Source

USB cable for Arduino power, or external 7-12V adapter/battery pack (if not using USB).

Note: Always connect the RC522 VCC pin to Arduino’s 3.3V pin, do not use 5V.

Computer with Arduino IDE Installed

Download the latest Arduino IDE from arduino.cc for coding and uploading.

MFRC522 Arduino Library (to be installed manually as explained in Step 2)

Optional Tools:

Soldering iron (for permanent connections), multimeter (for debugging).

The Configuration

Wiring-Table-instructables.jpg

The RC522 RFID module has 8 pins and supports multiple communication protocols like SPI, I2C, and UART. For Arduino projects, SPI is the most commonly used protocol.

Let’s connect the module by following these steps.

1 — Power Connections

Connect the VCC pin (supply voltage between 2.5V to 3.3V) to the Arduino’s 3.3V pin. Then, connect the GND pin to the Arduino’s GND.

2 — SPI communication pins

  1. Connect SDA (also called SS) to Arduino’s pin 10 (Slave Select).
  2. Connect SCK (Clock) to pin 13.
  3. Connect MOSI (Master Out Slave In) to pin 11.
  4. Connect MISO (Master In Slave Out) to pin 12.

3 — Control pins

  1. Connect RST (Reset pin) to Arduino’s pin 9.
  2. The IRQ pin (interrupt signal to indicate RFID tag detection) is optional and can be left unconnected.


Note: Use the 3.3V pin to power the RC522, not 5V. The IRQ pin is optional and can be left unconnected.

Install the Library and Test Sample Code

Data Dump.jpg

The MFRC522 Arduino library by Miguel André Balboa is required to work with the RC522 module. This library isn’t available in the Arduino Library Manager, so you’ll need to download and install it manually:

  1. Download the MFRC522 Library
  2. Open Arduino IDE
  3. Go to: Sketch → Include Library → Add .ZIP Library
  4. Select the downloaded ZIP file to install it

(Alternatively, you can extract the ZIP contents directly into your Arduino libraries folder.)

5. Upload the DumpInfo Example

Once the library is installed, you can test your setup using the built-in example code:

  1. Go to: File → Examples → MFRC522 → DumpInfo
  2. Upload the sketch to your Arduino
  3. Open the Serial Monitor (set baud rate to 115200)
  4. Scan an RFID tag near the RC522 reader

The serial monitor will display data like Card UID, PICC type, and memory block contents.

You can also directly copy and upload the code below from the DumpInfo example:

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
Serial.begin(115200);
while (!Serial);
SPI.begin();
mfrc522.PCD_Init();
delay(4);
mfrc522.PCD_DumpVersionToSerial();
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
if (!mfrc522.PICC_IsNewCardPresent()) return;
if (!mfrc522.PICC_ReadCardSerial()) return;
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}


If the output shows a data dump, your module is working perfectly.

Writing Data to the RFID Tag

Data Written.jpg

Now that we have dumped the factory data from the tag, let’s see how we can write some data to it. Use the following Arduino code to write and then read back data on the RFID tag.


#include <SPI.h> // Include the SPI library
#include <MFRC522.h> // Include the MFRC522 RFID library

#define RST_PIN 9 // Reset pin (can be changed to any digital pin)
#define SS_PIN 10 // Slave Select pin (can be changed to any digital pin)

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
MFRC522::MIFARE_Key key; // Create a MIFARE_Key struct to hold the card key

byte data1[14] = {"Circuit-Digest"}; // First data to write to the tag
byte data2[12] = {"Jobit-Joseph"}; // Second data to write to the tag
byte readbackblock[18]; // Array to hold data read from the tag

void setup() {
Serial.begin(115200); // Initialize serial communication
SPI.begin(); // Initialize SPI bus
mfrc522.PCD_Init(); // Initialize MFRC522 card
Serial.println("Scan a MIFARE Classic card");

// Prepare the security key for read/write operations (default key: 0xFF x6)
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}

void loop() {
// Look for new cards; if none found, exit loop
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}

// Read the card serial; if failed, exit loop
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}

Serial.println("Card detected. Writing data...");

// Write the data arrays to blocks 1 and 2 of the tag
writeBlock(1, data1);
writeBlock(2, data2);

Serial.println("Reading data from the tag...");

// Read and print data from block 1
readBlock(1, readbackblock);
Serial.print("Read block 1: ");
for (int j = 0; j < 14; j++) {
Serial.write(readbackblock[j]);
}
Serial.println();

// Read and print data from block 2
readBlock(2, readbackblock);
Serial.print("Read block 2: ");
for (int j = 0; j < 12; j++) {
Serial.write(readbackblock[j]);
}
Serial.println();
}

// Write data to a specific block
int writeBlock(int blockNumber, byte arrayAddress[]) {
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; // Trailer block holds sector access bits

// Check if block number is a trailer block (not writable)
if (blockNumber > 2 && (blockNumber + 1) % 4 == 0) {
Serial.print(blockNumber);
Serial.println(" is a trailer block: Error");
return 2;
}

// Authenticate using the key and trailer block
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("Authentication failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3;
}

// Write data to the block
status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("Data write failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4;
}

Serial.print("Data written to block ");
Serial.println(blockNumber);
return 0;
}

// Read data from a specific block
int readBlock(int blockNumber, byte arrayAddress[]) {
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3;

// Authenticate before reading
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("Authentication failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3;
}

byte bufferSize = 18;
status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &bufferSize);
if (status != MFRC522::STATUS_OK) {
Serial.print("Data read failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4;
}

Serial.println("Data read successfully");
return 0;
}


How does it work?

  1. The program initializes the SPI bus and RFID reader, sets up a default key (0xFF x6) for authentication.
  2. The loop() waits for a new RFID card. When detected, it writes two data arrays to block 1 and block 2 of the tag.
  3. After writing, it reads the same blocks and prints the data to the serial monitor.
  4. The writeBlock() function checks if the block is writable (not a trailer block), authenticates, and writes data.
  5. The readBlock() function authenticates and reads data from the specified block.

Once uploaded, open the Serial Monitor at 115200 baud, scan your RFID tag, and watch the data written and read-back data printed on the screen.