⚡ ESP32 & Relays

Switching AC-powered household appliances safely using a low-voltage microcontroller like the ESP32 — construction, driver circuits, protection, and ready-made modules explained.

1. The Problem: Switching AC Appliances with a Microcontroller

Most household appliances run on AC mains supply — typically 110V/60Hz or 220V/50Hz depending on your region. An ESP32 GPIO pin, however, only outputs 3.3V DC and can safely source only a few milliamps of current.

Why you can't just connect a transistor directly to an AC load:
  • A transistor is an active semiconductor device — it is not designed to switch bidirectional AC current directly.
  • There is no electrical isolation between the microcontroller, the transistor, and the load — a fault on the load side can feed mains voltage straight back into the microcontroller.
  • If the AC load side ever short-circuits or back-feeds voltage, the microcontroller circuit will be instantly damaged.

So we need an intermediate device that can be triggered by a low-voltage DC signal (from the ESP32) while safely switching a completely separate, isolated high-voltage AC circuit. That device is the relay.

2. What is a Relay?

A relay is an electromechanical switch. It has been used for decades because it behaves exactly like a mechanical switch — it simply doesn't care whether the load is AC or DC, making it perfectly suited for controlling AC appliances from DC logic circuits.

Key idea A relay lets a tiny DC control signal (from the ESP32) magnetically operate a completely separate, mechanically isolated switch contact that can carry high-voltage AC current.

3. Relay Construction & Working Principle

A relay consists of an electromagnetic coil (two terminals, often labeled L1 & L2, or A & B) and a movable metal armature held by a spring against a fixed contact.

Coil (Electromagnet) L1 L2 Pivot COM NC (Normally Closed) NO (Normally Open) Spring holds armature against NC contact until coil is energized Apply DC voltage here
Fig 1 — Simplified relay construction showing coil, armature, and COM / NC / NO terminals

How the Contacts Move

TerminalMeaningBehaviour
COMCommonThe moving armature terminal — always connects to either NC or NO
NCNormally ClosedConnected to COM when coil is not energized
NONormally OpenConnected to COM only when coil is energized
Working principle: When you apply a rated DC voltage (e.g., 5V or 12V — polarity does not matter) across the coil terminals, the coil becomes an electromagnet. This magnetic pull overcomes the spring tension, pulling the armature away from NC and snapping it onto NO. As soon as the coil voltage is removed, the magnetism collapses and the spring pulls the armature back to NC. As long as the coil stays energized, COM stays connected to NO.

4. Driving a Relay from the ESP32 (Transistor Driver Circuit)

A relay coil typically needs 40–100mA at 5V or 12V — far more current than an ESP32 GPIO pin (max ~12mA per pin) can safely supply. So the GPIO cannot drive the coil directly; instead it drives an NPN transistor, which does the actual switching of the coil current.

+5V / +12V Relay Coil Flyback Diode NPN Collector Base Emitter GND 1kΩ ESP32 GPIO LED (optional indicator + resistor)
Fig 2 — ESP32 GPIO drives an NPN transistor through a base resistor, which switches the relay coil; flyback diode and optional LED indicator are shown across the coil

Circuit Connections

ComponentConnection
Relay coil terminal 1Connected to +5V / +12V DC supply
Relay coil terminal 2Connected to Transistor Collector
Transistor EmitterConnected to GND
Transistor BaseConnected through a ~1kΩ resistor to an ESP32 GPIO pin
Never connect a relay coil directly to a GPIO pin. Always drive it through a transistor (or a ready-made relay driver module). As one embedded guide notes, direct GPIO connection to a relay coil will destroy the ESP32.

5. Why You Need a Flyback (Snubber) Diode

A relay coil is an inductor. When the transistor switches the coil OFF (GPIO goes LOW), the coil's stored magnetic energy tries to keep the current flowing, generating a sudden high-voltage reverse spike. This spike can destroy the driving transistor and even feed back into the microcontroller.

When the relay turns off, the collapsing magnetic field generates a voltage spike, and a flyback diode absorbs this spike and protects the circuit. If the diode is missing, the ESP32 may freeze or reboot.

How it Works

A common component used for this purpose is a flyback diode, usually 1N4007 or SS14.

6. Ready-Made Relay Modules for ESP32

Instead of wiring the transistor, flyback diode, and LED indicator yourself, you can buy ready-made relay modules that already integrate all of this circuitry, often with added optical isolation for extra safety.

FeatureDetails
Coil voltageUsually 5V (some 12V modules exist)
Control inputDigital signal pin (IN), plus separate VCC and GND
IsolationSome relay boards include an optocoupler like PC817, which acts as a safety barrier between the ESP32 and the relay coil circuit
Output terminalsScrew terminals labeled NO / COM / NC for the switched AC or DC load
Channel countAvailable as 1, 2, 4, 8, or even 16-channel boards
Power noteAll 5V relay modules require a stable 5V supply, separate from the ESP32's 3.3V line
Common Mistake: Even if the relay uses a 5V supply, the ESP32 must share a common GND with the relay module — this is the #1 mistake beginners make.
For safety and reliability, experts recommend: using a relay with a proper transistor driver on the board, adding a flyback diode, and adding a snubber/MOV where needed for AC inductive loads.

7. Example: Controlling a Relay from ESP32 (Arduino IDE)

Below is a simple sketch that turns a relay ON and OFF, simulating a lamp switching on for 3 seconds and off for 3 seconds. Each instruction is shown on its own line for clarity.

// Relay control example - ESP32
// Relay module IN pin connected to GPIO 26

#define RELAY_PIN 26

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
}

void loop() {
  digitalWrite(RELAY_PIN, HIGH);
  delay(3000);
  digitalWrite(RELAY_PIN, LOW);
  delay(3000);
}

Command Reference

CommandPurpose
pinMode(RELAY_PIN, OUTPUT);Configures the GPIO pin as a digital output
digitalWrite(RELAY_PIN, HIGH);Sets GPIO HIGH — turns transistor ON, energizes relay coil, closes NO contact
digitalWrite(RELAY_PIN, LOW);Sets GPIO LOW — turns transistor OFF, de-energizes coil, contact returns to NC
delay(3000);Pauses execution for 3000 milliseconds (3 seconds)
Many pre-built relay modules use active-LOW logic (IN pin LOW = relay ON). Always check your specific module's datasheet before wiring your load.

8. Summary