Introduction
The primary goal of this project is to develop a secure system that controls the activation of a relay module based on RFID card authentication. When an authorized RFID card is scanned using an NFC reader, the relay is switched on or off, offering a practical solution for applications such as access control, device activation, and automation systems. The project employs components such as an NFC card reader, I2C OLED display, 5V relay module, and ESP32-C3 microcontroller for seamless operation.
System Overview
The system is designed to authenticate RFID cards using an NFC reader and then activate a relay based on the card's authorization status. The OLED display provides real-time feedback to the user, such as card status (authorized or unauthorized). The ESP32-C3 serves as the central processing unit, managing communication between the NFC reader, display, and relay module.
Hand Sketchs of the proposed outcome:



Hardware Components
- ESP32-C3 Microcontroller: Acts as the system's brain, handling data processing and device communication.
- NFC Card Reader (MFRC522): Reads RFID tags to retrieve unique identification data.
- I2C OLED Display: Displays real-time system messages and statuses.
- 5V Relay Module: Switches electrical appliances on or off based on input signals.
- Authorized RFID Cards: Pre-configured cards used to control access.
Hardware Setup
Below are the wiring and setup details for connecting the components:
Connections
- ESP32-C3 to NFC Reader:
- SDA -> GPIO21
- SCK -> GPIO22
- MOSI -> GPIO23
- MISO -> GPIO19
- RST -> GPIO18
- ESP32-C3 to OLED Display:
- SDA -> GPIO4
- SCL -> GPIO5
- Relay Module:
- IN -> GPIO16
- VCC -> 5V
- GND -> GND
Software Design
Programming the ESP32-C3
The ESP32-C3 is programmed using the Arduino IDE. Required libraries include:
- MFRC522 for NFC reader communication
- Adafruit SSD1306 for the OLED display
- ESP32 core libraries
Key steps include:
1. Installing Required Libraries
Install the MFRC522 and Adafruit SSD1306 libraries from the Arduino Library Manager. To do this, navigate to Sketch > Include Library > Manage Libraries and search for the respective libraries.
2. Writing the Code
The code is divided into sections:
- Initialization: Set up the NFC reader, OLED display, and relay module.
- Reading RFID Tags: Retrieve unique ID from the scanned RFID card.
- Authorization Check: Compare the scanned card ID with pre-stored authorized IDs.
- Relay Control: Activate or deactivate the relay based on authorization.
- Display Feedback: Show status messages on the OLED display.
Sample Code
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <MFRC522.h>
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
#define RST_PIN 18
#define SS_PIN 21
MFRC522 rfid(SS_PIN, RST_PIN);
#define RELAY_PIN 16
String authorizedUID = "12345ABC"; // Example UID
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
display.begin(SSD1306_I2C_ADDRESS, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Initializing...");
display.display();
SPI.begin();
rfid.PCD_Init();
delay(1000);
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
return;
}
String cardUID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
cardUID += String(rfid.uid.uidByte[i], HEX);
}
display.clearDisplay();
if (cardUID.equalsIgnoreCase(authorizedUID)) {
digitalWrite(RELAY_PIN, HIGH);
display.println("Access Granted");
display.println("Relay ON");
} else {
digitalWrite(RELAY_PIN, LOW);
display.println("Access Denied");
}
display.display();
delay(2000);
}
Testing and Debugging
Test the system thoroughly:
- Ensure the relay switches on/off correctly based on RFID card authentication.
- Test multiple RFID cards to verify proper authorization logic.
- Monitor OLED display outputs for clear feedback.
Challenges and Solutions
Challenge 1: Inconsistent RFID reads
Solution: Adjust antenna position and ensure proper wiring.
Challenge 2: Relay module not switching
Solution: Verified power supply and GPIO pin assignments.
Potential Enhancements
- Adding Wi-Fi functionality to log card usage online.
- Integrating a keypad for multi-factor authentication.
Conclusion
This project demonstrates the effective use of RFID technology for secure relay control. The combination of NFC authentication, real-time feedback via an OLED display, and ESP32-C3 microcontroller makes the system versatile for various applications. With further enhancements, this system can serve as a robust foundation for advanced IoT-enabled access control solutions.