Index
Introduction
The MFRC522 RFID module is a popular RFID reader module used with Arduino to read RFID tags. It communicates via SPI, enabling quick data exchange and making it suitable for security and access control projects. Here’s a guide on setting up the MFRC522 RFID module with Arduino and reading RFID tag data.
Required Components
- Arduino Board (e.g., Arduino Uno)
- MFRC522 RFID module, RFID tags
- Jumper Wires
- Breadboard (optional)
Pinout
Circuit Diagram / Wiring
- RFID SENSOR VCC → 5V (Arduino)
- RFID SENSOR GND → GND (Arduino)
- RFID SENSOR SDA → Pin D10 (Arduino)
- RFID SENSOR SCK → Pin D13 (Arduino)
- RFID SENSOR MOSI → Pin D11 (Arduino)
- RFID SENSOR MISO → Pin D12 (Arduino)
- RFID SENSOR IRQ → Pin D9 (Arduino)
- RFID SENSOR RST → Pin D8 (Arduino)
Programming With Arduino
- Go to the “Libraries” tab on the left-hand side of the screen.
- Click on the “Library Manager” button (book icon) at the top of the Libraries tab.
- In the Library Manager window, type “MFRC522” in the search bar, locate the MFRC522 library, and click on the “Install” button next to it.
- Copy and paste the provided code into a new sketch in the Arduino IDE:
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 8
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
// Define UIDs for object 1 and object 2
byte object1UID[] = {0x83, 0x4D, 0x4C, 0xC5}; // UID of object 1
byte object2UID[] = {0xE5, 0xF6, 0x07, 0x08}; // UID of object 2 (change as needed)
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
delay(4); // Optional delay
Serial.println(F("Scan PICC to see UID..."));
}
void loop() {
// Reset the loop if no new card present on the sensor/reader.
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// Print the detected UID in a readable format
Serial.print("Card UID: ");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i], HEX);
if (i < mfrc522.uid.size - 1) {
Serial.print(" ");
}
}
Serial.println(); // New line after printing UID
// Check the UID of the scanned card
if (compareUID(mfrc522.uid.uidByte, object1UID)) {
Serial.println("Object 1 detected!");
}
else if (compareUID(mfrc522.uid.uidByte, object2UID)) {
Serial.println("Object 2 detected!");
}
else {
Serial.println("Unknown object detected!");
}
// Halt the PICC
mfrc522.PICC_HaltA();
}
// Function to compare two UIDs
bool compareUID(byte *uid1, byte *uid2) {
for (byte i = 0; i < 4; i++) { // Adjust the number of bytes based on your UID length
if (uid1[i] != uid2[i]) {
return false;
}
}
return true;
}
Explanation
- The code uses the MFRC522 library to interface with an RFID reader.
- It defines two unique UIDs for two different RFID cards to detect them.
- In the
setup()
function, it initializes the Serial Monitor and the RFID reader. - The
loop()
function checks for new cards, reads their UIDs, and compares them with predefined UIDs. - Depending on the detected UID, it prints messages indicating which object is detected or if an unknown object is scanned.
Testing and Troubleshooting
- Ensure the connections (SDA, SCK, MOSI, MISO, RST, and GND) between the MFRC522 module and Arduino are secure and correctly configured.
- Verify that the power supply is adequate; the MFRC522 should be powered with 3.3V, not 5V, to avoid damage.
- If the module does not respond, check the SPI library installation in your Arduino IDE and ensure the correct board is selected.
- Use the Serial Monitor to check for any error messages or debugging information that can help identify issues during the scanning process.