Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO RFID Reader with MFRC522 Module

Image of Arduino UNO RFID Reader with MFRC522 Module

Circuit Documentation

Summary

This document provides a detailed overview of a circuit that integrates an Arduino UNO microcontroller with an RFID-RC522 module. The Arduino UNO serves as the main controller, while the RFID-RC522 module is used for reading RFID tags. The circuit is designed to read and identify RFID tags and display the tag information via the Arduino's serial interface.

Component List

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  • Purpose in Circuit: Acts as the main controller to interface with the RFID module and process the RFID tag data.

RFID-RC522

  • Description: A low-cost RFID module that operates at 13.56 MHz.
  • Pins: VCC (3.3V), RST, GND, IRQ, MISO, MOSI, SCK, SDA
  • Purpose in Circuit: Reads RFID tags and communicates the tag data to the Arduino UNO.

Wiring Details

Arduino UNO

  • 3.3V: Connected to VCC (3.3V) of RFID-RC522
  • GND: Connected to GND of RFID-RC522
  • D5: Connected to RST of RFID-RC522
  • D10: Connected to SDA of RFID-RC522
  • D11: Connected to MOSI of RFID-RC522
  • D12: Connected to MISO of RFID-RC522
  • D13: Connected to SCK of RFID-RC522

RFID-RC522

  • VCC (3.3V): Connected to 3.3V of Arduino UNO
  • GND: Connected to GND of Arduino UNO
  • RST: Connected to D5 of Arduino UNO
  • SDA: Connected to D10 of Arduino UNO
  • MOSI: Connected to D11 of Arduino UNO
  • MISO: Connected to D12 of Arduino UNO
  • SCK: Connected to D13 of Arduino UNO

Code Documentation

Arduino UNO Code

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

// Init array that will store new NUID
byte nuidPICC[3];

void setup() {
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }

  Serial.println(F("This code scan the MIFARE Classsic NUID."));
  Serial.print(F("Using the following key:"));
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}

void loop() {

  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }

  if (rfid.uid.uidByte[0] != nuidPICC[0] ||
    rfid.uid.uidByte[1] != nuidPICC[1] ||
    rfid.uid.uidByte[2] != nuidPICC[2] ||
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
  
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  }
  else Serial.println(F("Card read previously."));

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

/**
 * Helper routine to dump a byte array as hex values to Serial.
 */
void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

This code initializes the RFID-RC522 module and continuously scans for new RFID tags. When a new tag is detected, it prints the tag's unique identifier (UID) in both hexadecimal and decimal formats to the serial monitor. The code also includes helper functions to format the UID for display.