Driving AC Loads Safely with a Transistor, a Relay Module, and Your ESP32
1. Introduction
A relay is an electromechanically or solid-state operated switch that allows a
low-power microcontroller signal — such as the 3.3 V logic output of an ESP32 — to safely
switch a completely separate, higher-power circuit, including mains AC appliances like bulbs,
fans, or pumps. Since the ESP32 GPIO pins cannot directly drive the current required by a
relay coil, an intermediate switching stage (commonly a transistor) is used, or a ready-made
relay module that already contains this driver circuitry is used instead.
This guide walks through both approaches: building a discrete transistor-relay driver from
scratch, and using an off-the-shelf single-channel relay module — the more common and
beginner-friendly method.
2. Why a Transistor Driver Is Needed
An ESP32 GPIO pin can typically supply only a few milliamps to a few tens of milliamps safely.
A small 5 V or 12 V relay coil, however, may require 50–150 mA to energize. Connecting the coil
directly to a GPIO pin would exceed the pin's current limits and could damage the microcontroller.
The solution is to use a transistor (typically an NPN type such as the 2N2222 or BC547) as an
electronic switch:
The ESP32 GPIO pin connects — through a current-limiting base resistor — to the base of the transistor.
The relay coil is connected between the external supply (5 V/12 V) and the transistor's collector.
The transistor's emitter connects to ground, which must be common with the ESP32's ground.
A flyback diode (e.g. 1N4007) is placed across the relay coil to protect the transistor from the voltage spike generated when the coil is de-energized.
When the GPIO pin outputs HIGH, a small base current flows, saturating the transistor and allowing
coil current to flow, energizing the relay and closing its contacts. When the GPIO goes LOW, the
transistor switches off and the relay releases.
3. Circuit Diagram
The diagram below shows the discrete transistor driver stage feeding a relay, which in turn switches an AC bulb.
Note that the ESP32 side (blue wires, low-voltage logic) and the AC side (orange wires, mains
voltage) are fully isolated by the relay contacts — only the grounds of the ESP32 and the
transistor/relay driver stage are shared, never the AC wiring.
4. Using a Ready-Made Relay Module
Instead of building the transistor driver circuit yourself, a pre-assembled relay
module is widely available and already contains the transistor (or optocoupler),
flyback diode, and status LED on a small PCB. These modules are sold as single-channel or
multi-channel (e.g., 2-channel, 4-channel, 8-channel) boards, but the underlying switching
logic is identical regardless of channel count.
Typical Single-Channel Relay Module Pinout
Pin
Label
Function
1
VCC
Module power input — commonly 5 V or 12 V depending on relay type
2
GND
Ground — must be common with the ESP32's ground
3
IN (Signal)
Control signal from an ESP32 GPIO pin (3.3 V logic)
4
COM
Common terminal of the relay's AC/DC switching side
5
NO
Normally Open contact — closes when relay is energized
6
NC
Normally Closed contact — opens when relay is energized
Two-channel and other multi-channel relay modules simply repeat this same VCC / GND / IN / COM / NO / NC
group once per channel, so a single-channel design scales directly.
5. Step-by-Step Wiring Instructions
Follow the exact order below when wiring an ESP32 to a single-channel relay module and an AC bulb.
Step 1: Connect relay module GND to ESP32 GND
Step 2: Connect relay module VCC to external 5V or 12V power supply
Step 3: Connect relay module IN pin to ESP32 GPIO (e.g., GPIO 2)
Step 4: Connect one AC mains wire to relay COM terminal
Step 5: Connect relay NO terminal to one terminal of the bulb holder
Step 6: Connect the other terminal of the bulb holder to the AC mains return wire
Step 7: Double-check that ESP32 GND and relay module GND share the same reference
Step 8: Power the ESP32 via USB or a separate 5V supply
Step 9: Power the relay module's VCC from the external supply only
Step 10: Plug the AC cable into the wall socket only after all connections are verified
Important: Never power the relay coil (VCC) directly from the ESP32's onboard
5V/3.3V pin if the relay draws more current than the regulator can supply. Use a dedicated
external supply and connect only the grounds together.
6. Sample Arduino Code — Blinking the Relay
The following simple sketch toggles the relay (and therefore the bulb) on and off every second,
equivalent to the basic blink program demonstrated on the ESP32 board.
// ESP32 Relay Blink Example
#define RELAY_PIN 2 // GPIO connected to relay IN pin
void setup() {
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
digitalWrite(RELAY_PIN, HIGH); // Energize relay -> bulb ON
delay(1000);
digitalWrite(RELAY_PIN, LOW); // De-energize relay -> bulb OFF
delay(1000);
}
Uploading the Code
Step 1: Open the Arduino IDE and select Tools > Board > ESP32 Dev Module
Step 2: Select the correct COM port under Tools > Port
Step 3: Click the Upload button (right-arrow icon)
Step 4: Wait for the message "Connecting..." to appear
Step 5: Press and hold the BOOT button if upload does not start automatically
Step 6: Wait for "Done uploading" to appear in the console
Step 7: Observe the relay module's onboard LED and audible click as it switches
7. Grounding and Safety Notes
A frequent point of confusion is why the ESP32's ground must be connected to the relay module's
ground even though the relay is switching a completely separate AC circuit. The reason is that
the relay's coil-driving side (VCC, GND, IN) is a low-voltage DC circuit that must share
a common reference with the ESP32 so that the GPIO's HIGH/LOW signal is correctly interpreted by
the module's onboard transistor or optocoupler.
Common Ground Rule: ESP32 GND ↔ Relay Module GND must always be tied together.
The relay's COM/NO/NC contact side, which carries the AC load, remains electrically isolated from
this logic-side ground.
Safety Warning: The relay's COM/NO/NC terminals carry mains voltage (110V/220V AC).
Always disconnect the AC supply before wiring or modifying these connections, use properly rated
wire and connectors, and never touch the relay's AC-side terminals while the circuit is powered.
8. Testing the Circuit
Step 1: Power the ESP32 and confirm the relay module's power LED turns on
Step 2: Listen for an audible click each time the relay switches state
Step 3: Verify the bulb turns on when the relay contact closes
Step 4: Verify the bulb turns off when the relay contact opens
Step 5: Adjust the delay() values in the sketch to change blink timing
Step 6: Re-upload the sketch after any code changes
If the relay clicks but the bulb does not light, recheck the AC-side wiring (COM/NO) and confirm
the bulb and socket are functional.
9. Future Enhancements
Once the basic ESP32-to-relay circuit is verified, the same hardware setup can be extended with
additional software features without changing any wiring, such as:
Controlling the relay over Wi-Fi using a simple web server hosted on the ESP32
Integrating with voice assistants such as Alexa or Google Home via IoT platforms
Scheduling automatic ON/OFF times using an NTP-synced clock
Expanding to multi-channel relay modules to control several appliances independently
The physical circuit — ESP32 GPIO → relay module IN, common ground, and isolated AC switching —
remains the same base setup for all of these future experiments.