กลับหน้าหลัก
views
Connect Arduino to RFID Module RC522 to Read Key Cards and Control LED
Last updated on

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)
Photo of all components laid out on a desk — Arduino UNO, RC522 module, red LED, 220Ω resistor, breadboard MB-102, and various jumper wires

Wiring Arduino to RFID Module RC522

The RC522 module communicates with Arduino over the SPI protocol. Here are the connections:

Arduino UNORFID Module RC522
3.3VVCC
GNDGND
D9RST
D10SDA
D11MOSI
D12MISO
D13SCK

The SPI pins (MISO, MOSI, SCK) follow the standard layout; only SDA and RST are reassignable if needed.

Breadboard wiring diagram showing Arduino UNO connected to RC522 via 3.3V, GND, MOSI/MISO/SCK, SDA on D10, and RST on D9

Installing the MFRC522 Library

In the Arduino IDE, go to Sketch > Include Library > Manage Libraries, search for MFRC522, and click Install.

Arduino IDE Library Manager window with MFRC522 library visible and ready to 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 UNOLED
D2Anode (via 220Ω)
GNDCathode

[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

จ้างทำโปรเจคเลย

ประเมินราคาอัตโนมัติ + Reference Code

ขอให้ AI ประเมินราคาโปรเจคนี้

กรอกข้อมูลให้ครบ ระบบจะสร้างรหัสอ้างอิงและประเมินราคา/ระยะเวลาคร่าว ๆ จากรายละเอียดงาน แล้วให้กด Add LINE พร้อมพิมพ์รหัสนี้เพื่อคุยต่อ

คำถามให้ AI ประเมินแม่นขึ้น

หลังส่งฟอร์ม ระบบจะโชว์ Reference Code ให้ copy แล้วกด Add LINE เพื่อคุยต่อ ข้อมูลส่วนตัวจะไม่ถูกส่งเข้า GA4

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...