Connect RFID Module to NodeMCU ESP8266 to Read Key Cards and Control LED
Connect RFID Module to NodeMCU ESP8266 to Read Key Cards and Control LED
This tutorial covers using NodeMCU ESP8266 with the RFID RC522 module (13.56MHz) to read card UID and use it as a trigger to turn an LED on and off.
Required Components
- NodeMCU ESP8266 V2 (CP2102)
- RFID Reader RC522 with key card
- Micro USB cable for uploading code
- Breadboard 830 points
- Jumper wires (male-male, male-female)
- LED 5mm red with 220 Ohm resistor
Wiring NodeMCU ESP8266 to RFID RC522
Connect the wires between NodeMCU ESP8266 and RFID RC522 module as follows:
| NodeMCU ESP8266 | RFID RC522 |
|---|---|
| 3.3V | 3.3V |
| GND | GND |
| D7 (GPIO13) | MOSI |
| D6 (GPIO12) | MISO |
| D5 (GPIO14) | SCK |
| D2 (GPIO4) | SDA (SS) |
| D1 (GPIO5) | RST |
Note that we use 3.3V from the board to power the RFID module, which matches the RC522 requirement of 3.3V supply.
Wiring LED to NodeMCU ESP8266
For the LED, connect a 220 Ohm resistor in series with the LED and wire it to the board as follows:
| NodeMCU ESP8266 | LED Circuit |
|---|---|
| D0 (GPIO16) | LED anode (through R 220 Ohm) |
| GND | LED cathode |
The resistor limits current to protect the LED from damage.
Installing MFRC522 Library for Arduino IDE
- Open Arduino IDE
- Go to Sketch > Include Library > Manage Libraries
- Search for “MFRC522”
- Install the MFRC522 library that supports ESP8266
After installation, you can include the library in your code.
Code to Read RFID Card UID
Before writing the LED control code, we need to read the UID of the card you want to use.
#include <SPI.h>
#include <MFRC522.h>
// Define pins connected to RFID
#define SS_PIN 4 // D2
#define RST_PIN 5 // D1
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Serial.println("Ready to read RFID card...");
}
void loop() {
// Check if a card is present
if (!rfid.PICC_IsNewCardPresent()) {
return;
}
// Read card data
if (!rfid.PICC_ReadCardSerial()) {
return;
}
// Print UID to Serial Monitor
Serial.print("UID: ");
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i]);
Serial.print(" ");
}
Serial.println();
rfid.PICC_HaltA(); // Stop reading card temporarily
}
Upload this code to the board, then open Serial Monitor at 9600 baud rate. Tap the key card on the reader and you will see the UID displayed. Write down this UID for the next step.
Code to Control LED with RFID Card
Take the UID from the previous step and insert it into the code to check if the tapped card matches the registered one.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 4 // D2
#define RST_PIN 5 // D1
#define LED_PIN 16 // D0
MFRC522 rfid(SS_PIN, RST_PIN);
// Enter your card's UID here
// *** Replace these values with your card's UID ***
byte authorizedUID[] = {166, 233, 165, 29, 247};
byte uidSize = 5;
bool ledState = false;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(LED_PIN, OUTPUT);
Serial.println("RFID LED Control Ready");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent()) {
return;
}
if (!rfid.PICC_ReadCardSerial()) {
return;
}
// Check if UID matches the registered card
if (uidSize == rfid.uid.size) {
bool match = true;
for (byte i = 0; i < uidSize; i++) {
if (authorizedUID[i] != rfid.uid.uidByte[i]) {
match = false;
break;
}
}
if (match) {
// Valid card -> toggle LED state
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
Serial.println(ledState ? "LED ON" : "LED OFF");
} else {
// Invalid card
Serial.println("Unauthorized card");
}
}
rfid.PICC_HaltA();
delay(500); // Delay to prevent repeated reads
}
Upload this code and try tapping the card with the matching UID. The LED will turn on. Tapping another card will not trigger the LED.
Code Adjustments Required
- Line 12: Change
authorizedUIDvalues to match your card’s UID - Line 13: Update
uidSizeto match your card’s UID length - To support multiple cards, create a 2D array to store multiple UIDs
Summary
- Wire RFID RC522 to NodeMCU ESP8266 according to the connection table
- Install the MFRC522 library
- Upload the UID reading code to extract your card’s UID
- Insert the UID into the LED control code
- Test by tapping the card on the reader. If valid, the LED turns on; if not, nothing happens
Once you understand the principle, you can adapt this to control relays, motors, or other devices instead of an LED.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย