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.
- 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.
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.
How the Contacts Move
| Terminal | Meaning | Behaviour |
|---|---|---|
| COM | Common | The moving armature terminal — always connects to either NC or NO |
| NC | Normally Closed | Connected to COM when coil is not energized |
| NO | Normally Open | Connected to COM only when coil is energized |
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.
Circuit Connections
| Component | Connection |
|---|---|
| Relay coil terminal 1 | Connected to +5V / +12V DC supply |
| Relay coil terminal 2 | Connected to Transistor Collector |
| Transistor Emitter | Connected to GND |
| Transistor Base | Connected through a ~1kΩ resistor to an ESP32 GPIO pin |
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.
How it Works
- The diode is connected in reverse bias across the coil (cathode to the positive supply side, anode to the transistor-collector side).
- During normal operation (coil energized), the diode is reverse-biased and has no effect on the circuit.
- When the coil is switched off, the induced reverse voltage forward-biases the diode, giving the stored current a safe path to dissipate quickly (typically within 100–200 milliseconds) instead of spiking across the transistor.
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.
| Feature | Details |
|---|---|
| Coil voltage | Usually 5V (some 12V modules exist) |
| Control input | Digital signal pin (IN), plus separate VCC and GND |
| Isolation | Some relay boards include an optocoupler like PC817, which acts as a safety barrier between the ESP32 and the relay coil circuit |
| Output terminals | Screw terminals labeled NO / COM / NC for the switched AC or DC load |
| Channel count | Available as 1, 2, 4, 8, or even 16-channel boards |
| Power note | All 5V relay modules require a stable 5V supply, separate from the ESP32's 3.3V line |
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
| Command | Purpose |
|---|---|
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) |
8. Summary
- ESP32 GPIO pins output only 3.3V DC and cannot switch AC mains loads directly.
- A bare transistor cannot switch AC and provides no isolation — a relay is required.
- A relay is an electromechanical switch: energizing its coil moves an armature from the NC contact to the NO contact.
- The relay coil is driven through an NPN transistor (or a MOSFET), controlled by a GPIO pin through a base resistor.
- A flyback diode across the coil protects the transistor and microcontroller from voltage spikes when the coil is switched off.
- Ready-made 5V relay modules bundle the transistor, diode, and often an optocoupler for isolation — always share a common ground with the ESP32.
- In software, a single
digitalWrite()command on the control GPIO turns the connected AC or DC load on or off.