Hereβs a complete example of how to trigger a relay using RFID-RC522 and Arduino Nano when a specific RFID card is detected.
Hardware Needed
- Arduino Nano
- RFID-RC522 module (MFRC522)
- Relay module (5V)
- Jumper wires
- RFID card/tag
Wiring Diagram
RFID-RC522 β Arduino Nano (SPI)
| RC522 Pin | Arduino Nano Pin |
| --------- | ------------------- |
| SDA (SS) | D10 |
| SCK | D13 |
| MOSI | D11 |
| MISO | D12 |
| IRQ | Not connected |
| GND | GND |
| RST | D9 |
| 3.3V | 3.3V β οΈ (IMPORTANT) |
Important:
RC522 works on 3.3V, NOT 5V!Relay β Arduino Nano
| Relay Pin | Arduino Nano Pin | | --------- | ---------------- | | VCC | 5V | | GND | GND | | IN | D3 |Install Library
In Arduino IDE:
- Sketch β Include Library β Manage Libraries
- Search: MFRC522
- Install: MFRC522 by Miguel Balboa
Library: MFRC522
Arduino Code (Trigger Relay When Card Matches)
#include
#include
#define SS_PIN 10
#define RST_PIN 9
#define RELAY_PIN 3
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Replace with your card UID
byte authorizedUID[4] = {0xDE, 0xAD, 0xBE, 0xEF};
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.println("Scan your RFID card...");
}
void loop() {
// Look for new card
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select card
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.print("UID tag: ");
bool authorized = true;
for (byte i = 0; i < 4; i++) {
Serial.print(mfrc522.uid.uidByte[i], HEX);
Serial.print(" ");
if (mfrc522.uid.uidByte[i] != authorizedUID[i]) {
authorized = false;
}
}
Serial.println();
if (authorized) {
Serial.println("Access Granted!");
digitalWrite(RELAY_PIN, HIGH); // Relay ON
delay(3000); // 3 seconds
digitalWrite(RELAY_PIN, LOW); // Relay OFF
} else {
Serial.println("Access Denied!");
}
mfrc522.PICC_HaltA();
}
How to Get Your Card UID
Upload this simple code first:
#include
#include
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Scan card...");
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent()) return;
if (!mfrc522.PICC_ReadCardSerial()) return;
Serial.print("UID: ");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i], HEX);
Serial.print(" ");
}
Serial.println();
}
Open Serial Monitor, scan card, then replace:
byte authorizedUID[4] = {0xXX, 0xXX, 0xXX, 0xXX};
How It Works
- RC522 reads RFID card UID
- Arduino compares UID with authorized UID
- If match β relay turns ON
- If not β relay stays OFF