ESP32 Relay Module Control

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:

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.

ESP32 GPIO Pin R (1k) NPN Base Collector Emitter GND (Common) Relay Coil +12V Supply Flyback Diode 1N4007 Relay Contacts (COM / NO) AC Line Bulb AC Neutral / Return to Mains Cable

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

PinLabelFunction
1VCCModule power input — commonly 5 V or 12 V depending on relay type
2GNDGround — must be common with the ESP32's ground
3IN (Signal)Control signal from an ESP32 GPIO pin (3.3 V logic)
4COMCommon terminal of the relay's AC/DC switching side
5NONormally Open contact — closes when relay is energized
6NCNormally 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.

  1. Step 1: Connect relay module GND to ESP32 GND
  2. Step 2: Connect relay module VCC to external 5V or 12V power supply
  3. Step 3: Connect relay module IN pin to ESP32 GPIO (e.g., GPIO 2)
  4. Step 4: Connect one AC mains wire to relay COM terminal
  5. Step 5: Connect relay NO terminal to one terminal of the bulb holder
  6. Step 6: Connect the other terminal of the bulb holder to the AC mains return wire
  7. Step 7: Double-check that ESP32 GND and relay module GND share the same reference
  8. Step 8: Power the ESP32 via USB or a separate 5V supply
  9. Step 9: Power the relay module's VCC from the external supply only
  10. 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

  1. Step 1: Open the Arduino IDE and select Tools > Board > ESP32 Dev Module
  2. Step 2: Select the correct COM port under Tools > Port
  3. Step 3: Click the Upload button (right-arrow icon)
  4. Step 4: Wait for the message "Connecting..." to appear
  5. Step 5: Press and hold the BOOT button if upload does not start automatically
  6. Step 6: Wait for "Done uploading" to appear in the console
  7. 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

  1. Step 1: Power the ESP32 and confirm the relay module's power LED turns on
  2. Step 2: Listen for an audible click each time the relay switches state
  3. Step 3: Verify the bulb turns on when the relay contact closes
  4. Step 4: Verify the bulb turns off when the relay contact opens
  5. Step 5: Adjust the delay() values in the sketch to change blink timing
  6. 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:

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.