This project builds an RFID access control display system using an ESP32 microcontroller. The MFRC522 RFID reader scans proximity cards, the ST7789 LCD displays the reader status and access results, and a relay module switches an external load when an authorized card is detected. The system stores up to 8 registered card UIDs with associated names in non-volatile memory.
The guide provides a complete wiring diagram connecting the RFID module, LCD display, and relay to the ESP32, a parts list with all required components, Arduino firmware with card registration and access control logic, and step-by-step assembly instructions including relay testing without load. Builders will have a working access display that authenticates RFID cards and provides visual feedback on a color LCD screen.
Wiring diagram

Gather all the parts
Assemble it in 3 steps
1. Sambungkan pembaca RC522
Cabut USB. Sambung VCC dan RST ke 3V3, GND ke GND, SCK ke GPIO18 (jam), MOSI ke GPIO23 (data masuk), MISO ke GPIO19 (data keluar), dan SDA ke GPIO4 (pilih pembaca). Biarkan IRQ kosong.
- Jangan sambung RC522 ke 5V; modul boleh rosak.
2. Sambungkan skrin ST7789
Sambung VCC dan BLK ke 3V3 (kuasa dan lampu latar), GND ke GND, SCK ke GPIO14 (jam skrin), SDA ke GPIO13 (data skrin), DC ke GPIO26 (data atau arahan), dan RES ke GPIO27 (set semula). Jam skrin dan jam RC522 mesti berasingan kerana skrin tiada pin CS.
- Ikut label pin, bukan warna wayar.
3. Uji relay tanpa beban
Cabut USB. Gunakan modul relay 5 V yang menerima isyarat 3.3 V dan aktif HIGH. Sambung VCC ke 5V, GND ke GND dan IN ke GPIO25. Biarkan COM, NO dan NC kosong. Cucuk semula USB. Buka Monitor Serial pada 115200, imbas kad dan hantar daftar Nama dengan pilihan baris baharu. Alihkan kad, kemudian imbas semula untuk hidupkan relay. Alih dan imbas lagi untuk matikan. Kad tidak berdaftar tidak mengubah relay.
- Jangan sambung solenoid atau bateri semasa ujian. Jangan gunakan relay gegelung kosong atau modul aktif LOW dengan pendawaian ini.
Review all connections
1. Connections between "rfid" and "ESP32"
2. Connections between "lcd" and "ESP32"
3. Connections between "relay" and "ESP32"
Deploy the firmware
#include <Arduino.h>
#include <SPI.h>
#include <Preferences.h>
#include <MFRC522.h>
#include <Adafruit_ST7789.h>
constexpr int RFID_CS = 4;
constexpr int RELAY = 25;
bool relayOn = false;
char ownerUid[21] = {};
Preferences cards;
bool storageReady = false;
char recentUid[21] = {};
char names[8][17] = {};
char uids[8][21] = {};
bool held = false;
uint32_t lastPresent = 0;
MFRC522 reader(RFID_CS, MFRC522::UNUSED_PIN);
SPIClass lcdSpi(HSPI);
Adafruit_ST7789 lcd(&lcdSpi, -1, 26, 27);
char shown[21] = {};
uint32_t lastScan = 0;
void draw(const char* message) {
lcd.fillScreen(ST77XX_BLACK);
lcd.setTextWrap(false);
lcd.setTextSize(2);
lcd.setTextColor(ST77XX_CYAN);
lcd.setCursor(12, 24);
lcd.print("Pembaca RFID");
lcd.setTextColor(ST77XX_WHITE);
lcd.setCursor(12, 88);
lcd.print(*message ? message : "Dekatkan kad");
lcd.setCursor(12, 146);
lcd.print(relayOn ? "Relay: ON" : "Relay: OFF");
}
void setup() {
digitalWrite(RELAY, LOW);
pinMode(RELAY, OUTPUT);
Serial.begin(115200);
storageReady = cards.begin("rfid", false);
if (storageReady) {
for (int i = 0; i < 8; ++i) {
char key[8];
snprintf(key, sizeof(key), "kad%d", i);
String entry = cards.getString(key, "");
int sep = entry.indexOf(':');
if (sep > 0 && sep <= 20) {
entry.substring(0, sep).toCharArray(uids[i], sizeof(uids[i]));
entry.substring(sep + 1).toCharArray(names[i], sizeof(names[i]));
}
}
}
Serial.println("Imbas kad, kemudian taip: daftar Nama");
pinMode(RFID_CS, OUTPUT);
digitalWrite(RFID_CS, HIGH);
SPI.begin(18, 19, 23, -1);
lcdSpi.begin(14, -1, 13, -1);
lcd.init(240, 240, SPI_MODE3);
lcd.setSPISpeed(4000000);
lcd.setRotation(1);
lcd.fillScreen(ST77XX_BLUE);
delay(700);
draw(shown);
reader.PCD_Init();
}
int findCard(const char* uid) {
for (int i = 0; i < 8; ++i)
if (*uids[i] && strcmp(uids[i], uid) == 0) return i;
return -1;
}
void enrollment() {
static char line[48];
static size_t len = 0;
static bool overflow = false;
while (Serial.available()) {
char c = Serial.read();
if (c != '\r' && c != '\n') {
if (len < sizeof(line) - 1) line[len++] = c;
else overflow = true;
continue;
}
if (!len) continue;
line[len] = 0;
len = 0;
bool valid = !overflow && strncmp(line, "daftar ", 7) == 0;
overflow = false;
if (!valid || !line[7] || strlen(line + 7) > 16 || !*recentUid) {
Serial.println("Imbas kad dahulu. Taip daftar Nama (1-16 aksara).");
continue;
}
int slot = findCard(recentUid);
if (slot < 0) {
for (int i = 0; i < 8; ++i) if (!*uids[i]) { slot = i; break; }
}
if (slot < 0) { Serial.println("Senarai penuh (8 kad)."); continue; }
char key[8], entry[38];
snprintf(key, sizeof(key), "kad%d", slot);
snprintf(entry, sizeof(entry), "%s:%s", recentUid, line + 7);
if (!storageReady || !cards.putString(key, entry)) {
Serial.println("Gagal menyimpan kad.");
continue;
}
strcpy(uids[slot], recentUid);
strcpy(names[slot], line + 7);
Serial.printf("Disimpan: %s (%s)\n", names[slot], recentUid);
if (strcmp(shown, names[slot])) { strcpy(shown, names[slot]); draw(shown); }
lastScan = millis();
}
}
void loop() {
enrollment();
if (held) {
byte atqa[2], size = sizeof(atqa);
auto status = reader.PICC_WakeupA(atqa, &size);
if (status == MFRC522::STATUS_OK || status == MFRC522::STATUS_COLLISION) {
lastPresent = millis();
if (reader.PICC_ReadCardSerial()) reader.PICC_HaltA();
} else if (millis() - lastPresent >= 250) {
held = false;
}
} else if (reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()) {
for (byte i = 0; i < reader.uid.size && i < 10; ++i)
snprintf(recentUid + 2 * i, sizeof(recentUid) - 2 * i, "%02X", unsigned(reader.uid.uidByte[i]));
reader.PICC_HaltA();
held = true;
lastPresent = lastScan = millis();
int slot = findCard(recentUid);
const char* message = slot < 0 ? "Tidak berdaftar" : names[slot];
bool changed = false;
if (slot >= 0) {
if (!relayOn) {
strcpy(ownerUid, recentUid);
relayOn = true;
changed = true;
} else if (strcmp(ownerUid, recentUid) == 0) {
ownerUid[0] = 0;
relayOn = false;
changed = true;
} else {
message = "Guna kad asal";
}
}
Serial.printf("UID %s: %s\n", recentUid, message);
if (changed) digitalWrite(RELAY, relayOn ? HIGH : LOW);
if (changed || strcmp(shown, message)) {
strcpy(shown, message);
draw(shown);
}
}
if (*shown && millis() - lastScan >= 4000) {
shown[0] = 0;
draw(shown);
}
delay(20);
}Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




