Back to All Blog Posts
Tutorial EltroNerd Engineering

IoT smart lock with RFID

IoT Smart Lock using ESP32 with MFRC522 RFID and Wi-Fi for remote monitoring and control. OLED display to show status messages like “Authorized,” “Unauthorized,” or “Locked/Unlocked.”

IoT Smart Lock with OLED Display using ESP32 and MFRC522 RFID

A smart lock becomes more interactive when it can display status messages.
This project combines ESP32, MFRC522 RFID, servo lock, and an OLED display to provide real-time feedback along with Wi-Fi connectivity.


Hardware Requirements

  • ESP32 Development Board
  • MFRC522 RFID Reader Module
  • RFID Cards/Tags
  • Servo Motor or Electromagnetic Lock
  • 0.96" I2C OLED Display (SSD1306)
  • Jumper wires and breadboard
  • Power supply for ESP32, OLED, and servo

Working Principle

  1. RFID Authentication:

    • ESP32 reads RFID UID from MFRC522.
    • Checks UID against authorized list.
  2. Lock Control:

    • If authorized, unlocks via servo motor.
    • Otherwise, remains locked.
  3. OLED Feedback:

    • Displays “Authorized,” “Unauthorized,” “Locked,” or “Unlocked.”
    • Provides immediate visual feedback.
  4. Wi-Fi Monitoring (Optional):

    • Logs events to cloud or dashboard for remote monitoring.

Arduino Code Example

Install Adafruit SSD1306 and Adafruit GFX libraries via Library Manager.

c
#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SS_PIN 5
#define RST_PIN 4
#define SERVO_PIN 23
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

MFRC522 mfrc522(SS_PIN, RST_PIN);  
Servo lockServo;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";

String authorizedUID = "AB CD EF 12"; // Example UID

void setup() {
  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();

  lockServo.attach(SERVO_PIN);
  lockServo.write(0); // Lock closed

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 allocation failed");
    while(1);
  }
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Smart Lock");
  display.display();

  WiFi.begin(ssid, password);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWi-Fi connected!");
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()) return;
  if (!mfrc522.PICC_ReadCardSerial()) return;

  String readUID = "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    readUID += String(mfrc522.uid.uidByte[i], HEX);
    if (i < mfrc522.uid.size - 1) readUID += " ";
  }
  readUID.toUpperCase();
  Serial.println("Card UID: " + readUID);

  display.clearDisplay();
  display.setCursor(0, 0);

  if (readUID == authorizedUID) {
    Serial.println("Authorized! Unlocking...");
    lockServo.write(90); // Unlock
    display.println("Authorized");
    display.display();
    delay(5000);
    lockServo.write(0); // Lock
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("Locked");
    display.display();
  } else {
    Serial.println("Unauthorized Access!");
    display.println("Unauthorized");
    display.display();
    // Optional: send Wi-Fi alert
  }

  mfrc522.PICC_HaltA();
}

Try this project in your browser

Compile code and simulate hardware output with EltroNerd Cloud IDE.

Launch Cloud IDE