Connect Arduino to RFID Module RC522 to Read Key Cards and Control LED
Connect Arduino to RFID Module RC522 to Read Key Cards and Control LED
In this lesson we will walk through wiring an RC522 RFID module to an Arduino UNO, reading the UID from a key card through the Serial Monitor, and then using that UID to turn an LED on or off as a simple access control example.
What You Will Need
- Arduino UNO R3 with USB cable
- RFID Module RC522 13.56MHz with Key Card
- Red LED 5mm
- Resistor 220 Ohm
- Breadboard (830 points)
- Jumper wires (M-M, M-F, F-F)
Wiring Arduino to RFID Module RC522
The RC522 module communicates with Arduino over the SPI protocol. Here are the connections:
| Arduino UNO | RFID Module RC522 |
|---|---|
| 3.3V | VCC |
| GND | GND |
| D9 | RST |
| D10 | SDA |
| D11 | MOSI |
| D12 | MISO |
| D13 | SCK |
The SPI pins (MISO, MOSI, SCK) follow the standard layout; only SDA and RST are reassignable if needed.
Installing the MFRC522 Library
In the Arduino IDE, go to Sketch > Include Library > Manage Libraries, search for MFRC522, and click Install.
Reading a Card UID via Serial Monitor
Wire the circuit as shown above and upload this sketch:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Serial.println("RFID Ready - tap a card to read UID");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent()) return;
if (!rfid.PICC_ReadCardSerial()) return;
Serial.print("UID Dec: ");
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i]);
if (i < rfid.uid.size - 1) Serial.print(", ");
}
Serial.println();
rfid.PICC_HaltA();
}
Open the Serial Monitor (baud 9600) and tap a key card on the reader. You will see a series of numbers, for example Dec: 182, 7, 177, 29, 29. Copy this UID — you will need to put it into the next sketch.
[image: Serial Monitor output showing UID decimal values 182, 7, 177, 29, 29 with the RFID Ready prompt above it]
Controlling an LED with a Known UID
Next, add a red LED with a 220Ω series resistor connected to pin D2 (positive lead through the resistor to D2, negative lead to GND).
| Arduino UNO | LED |
|---|---|
| D2 | Anode (via 220Ω) |
| GND | Cathode |
[image: Circuit diagram adding a red LED and 220Ω resistor to Arduino pin D2 on the breadboard]
Upload the sketch below and replace the UID bytes on lines 22–27 with the values you recorded from the Serial Monitor:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
#define LED_PIN 2
MFRC522 rfid(SS_PIN, RST_PIN);
// Replace this array with your card's UID
byte authorizedUID[] = {182, 7, 177, 29, 29};
bool isAuthorized = false;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // LED off by default
Serial.println("Ready");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent()) return;
if (!rfid.PICC_ReadCardSerial()) return;
isAuthorized = true;
for (byte i = 0; i < rfid.uid.size; i++) {
if (rfid.uid.uidByte[i] != authorizedUID[i]) {
isAuthorized = false;
break;
}
}
if (isAuthorized) {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED ON");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("Not authorized");
}
rfid.PICC_HaltA();
}
Open the Serial Monitor and tap the card whose UID you stored. The LED will light up. If you tap a different card, the LED stays off. To support multiple authorized cards, you can expand the logic to compare against a list of UIDs instead of a single array.
[image: Test setup — tapping the saved key card against the RC522 reader causes the red LED to light up, with Serial Monitor showing LED ON]
Wrap-up
This lesson covered wiring the RC522 RFID module to Arduino using SPI, reading a card UID through the Serial Monitor, and using that UID in an if check to toggle an LED. The core steps are: wire the circuit, install the library, read and note the UID, paste the UID into the control sketch, and expand from there — for example replacing the LED with a relay to drive a motor, or adding more cards to an authorized list.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย