Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Controlled RFID Access with Relay Switching

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled RFID Access with Relay Switching

Circuit Documentation

Summary of the Circuit

This circuit integrates an ESP32 microcontroller with an RFID-RC522 reader and a 12V single-channel relay. The ESP32 is responsible for controlling the RFID reader to scan for RFID tags and, upon successful detection of a valid tag, activating the relay. The relay can be used to control a higher power device, such as turning on a light or triggering a motor. The circuit is powered by the ESP32, which also communicates with the RFID reader via SPI protocol.

Component List

ESP32 (30 pin)

RFID-RC522

12V SINGLE CHANNEL RELAY

Wiring Details

ESP32 (30 pin)

RFID-RC522

12V SINGLE CHANNEL RELAY

Documented Code

/*
 * ESP32 with Relay and RFID-RC522
 * This code initializes the RFID-RC522 module and reads RFID tags.
 * When a valid RFID tag is detected, it activates a relay connected to the ESP32.
 */

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 27 // ESP32 pin connected to RFID RST
#define SS_PIN 5   // ESP32 pin connected to RFID SDA
#define RELAY_PIN 26 // ESP32 pin connected to Relay IN

MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance

void setup() {
  Serial.begin(115200);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Ensure relay is off
  Serial.println("RFID Reader and Relay Initialized");
}

void loop() {
  // Look for new cards
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
    delay(50);
    return;
  }

  Serial.print("Card UID:");
  for (byte i = 0; i < rfid.uid.size; i++) {
    Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(rfid.uid.uidByte[i], HEX);
  }
  Serial.println();

  // Activate relay for 5 seconds
  digitalWrite(RELAY_PIN, HIGH);
  delay(5000);
  digitalWrite(RELAY_PIN, LOW);

  // Halt PICC
  rfid.PICC_HaltA();
  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

This code is designed to be uploaded to the ESP32 microcontroller. It initializes the RFID-RC522 module and sets up the relay pin as an output. In the main loop, it continuously checks for new RFID cards. When a card is detected, it prints the card's UID to the serial monitor and activates the relay for 5 seconds.