ESP32 with Relay Module: Master Complete Beginner-to-Expert Guide

0b63979cd9494aa401d1fce2d73bb002
On: December 5, 2025
ESP32 with Relay Module

Table of Contents

Beginner-friendly guide on using ESP32 with a relay module, covering wiring, safety, code examples, and real-world home automation applications.

If you’ve ever wanted to control a light, a fan, a pump, or any AC appliance using Wi-Fi or the internet, an ESP32 with relay module is the simplest and most powerful way to do it. Think of the ESP32 as the brain and the relay module as the muscle. When they work together, you can automate almost anything at home.

What Is an ESP32 with Relay Module?

If you’re new to automation, here’s the simplest explanation:

  • ESP32 → a Wi-Fi + Bluetooth microcontroller
  • Relay module → an electrical switch controlled by the ESP32
  • Combination → lets you turn appliances ON or OFF from anywhere

In homes, offices, farms, and DIY projects, this combo is basically the “Hello World” of smart automation.

Using an ESP32 relay board (1 channel, 2 channel, 4 channel, even 16 channel) makes it easy to switch heavy loads the ESP32 alone can’t handle. The ESP32 GPIOs provide only 3.3V logic and can’t drive large currents, so the relay becomes the safe isolation layer.

Types of Relay Modules for ESP32

You’ll find many variations online especially on marketplaces like AliExpress where “ESP32 relay board aliexpress” boards are very popular. Here’s what beginners usually use:

1 Channel Relay Module with ESP32

Good for controlling a single appliance, like a bulb.

2 Channel Relay Module with ESP32

Often searched as “esp32 2 relay module pinout” because beginners want wiring clarity. Useful for fan + light, or two devices.

4 Channel Relay Module with ESP32

The most common and versatile. You’ll see terms like 4 channel relay module with esp32, 4 relay module with esp32, or esp32 relay board 4 channel. Great for room automation.

8 Channel Relay Module with ESP32

Used in farms, water pumps, multi-light control.

16 Channel Relay Board (esp32 relay board x16)

Used in industrial-level automation or big home setups.

Low-Level & High-Level Trigger Boards

Important: Some boards turn ON when the signal is LOW, others ON when HIGH.
We’ll talk about this in the troubleshooting section.

Relay Ratings You Must Understand

Relay modules typically have:

  • 5V coil voltage → that’s why people search: 5v relay module with esp32
  • AC Load support → usually 250V/10A
  • DC Load support → varies, but 30V/10A is common

Always check the relay rating printed on the relay.

Power Requirements (VERY IMPORTANT)

Most relay modules require:

  • 5V for relay coil (ESP32 GPIO cannot provide this)
  • You must power the relay from 5V pin of ESP32 OR an external supply
  • For 8-channel or 16-channel boards, always use external 5V because ESP32 5V pin cannot supply enough current

A safe rule:

  • 1 relay → ESP32 5V pin is okay
  • 4 relays → borderline
  • 8/16 relays → must use external 5V

ESP32 Relay Module Schematic

A typical esp32 relay schematic or esp32 relay module schematic includes:

  1. ESP32 GPIO pin → Relay IN pin
  2. Optocoupler (optional) → isolation
  3. Transistor to drive current
  4. Flyback diode across the relay coil
  5. 5V power to relay VCC
  6. GND connected between ESP32 and relay module

Even if your module has an optocoupler, you still need a common ground.

How to Connect ESP32 with Relay Module

This is the most searched topic: how to connect esp32 with relay module.
Here is the simplest wiring guide that works for all relay modules:

Pin Connections

Relay Module PinConnect To (ESP32)
VCC (5V)5V of ESP32
GNDGND of ESP32
IN1GPIO 23 (example)
IN2GPIO 22 (for 2 or more relays)
IN3GPIO 21
IN4GPIO 19

You can choose any GPIO that supports OUTPUT mode.

ESP32 2 Relay Module Pinout

Most 2-relay modules have:

  • IN1
  • IN2
  • GND
  • VCC
  • JD-VCC (for some optocoupled boards)

If your board has JD-VCC, it’s for separate relay coil voltage.

How to Use Relay Module with ESP32

Here’s a beginner-friendly sketch:

int relay = 23;

void setup() {
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH); // turn OFF (depends on board)
}

void loop() {
  digitalWrite(relay, LOW);  // turn ON
  delay(2000);

  digitalWrite(relay, HIGH); // turn OFF
  delay(2000);
}

Most relay boards are LOW trigger, meaning they turn ON when you write LOW.

ESP32 Relay Module Control AC Appliances (Safety Note)

Many people want esp32 relay module control ac appliances or even an Internet-controlled esp32 relay module control ac appliances web server.
It’s completely possible, but NEVER touch AC wires when powered.

The relay gives you isolation but AC wiring still needs caution.
Use:

  • Proper screw terminals
  • Proper insulation
  • No loose wires
  • A fuse if possible

ESP32 Relay Board X1, X2, X4, X8, X16

These names simply represent the number of relays.
For example:

  • esp32 relay board x1 → 1 relay
  • esp32 relay board x2 → 2 relays
  • esp32 relay board x4 → 4 relays
  • esp32 relay board x8 → 8 relays
  • esp32 relay board x16 → 16 relays

They all work using the same principle. You just wire more GPIOs.

ESP32 Relay Board Firmware & Programming Basics

When engineers search esp32 relay board firmware or esp32 relay board programming, they usually want code that can:

  • Respond to GPIO
  • Handle Wi-Fi control
  • Use MQTT
  • Control via Web Server
  • Work with Alexa/Google

Controlling a Relay With ESP32 Web Server

One of the most popular automation projects is esp32 relay module control ac appliances web server.
This lets you turn appliances ON/OFF using your mobile, Wi-Fi, or even the internet.

Here’s the logic:

  • ESP32 creates a web server
  • You open its IP address in a browser
  • You tap a button
  • The ESP32 toggles a relay
  • Relay switches your appliance

This is the simplest way to start home automation without extra platforms.

Simple Web Server Code for ESP32 with 1 Relay

This code uses Wi-Fi + a relay connected to GPIO 23.

#include 
#include 

const char* ssid = "YourWiFi";
const char* password = "YourPassword";

WebServer server(80);
int relayPin = 23;
bool relayState = false;

void handleRoot() {
  String html = "

ESP32 Relay Control

"; html += "

Relay is " + String(relayState ? "ON" : "OFF") + "

"; html += ""; html += ""; server.send(200, "text/html", html); } void turnOn() { digitalWrite(relayPin, LOW); // For LOW-trigger relays relayState = true; handleRoot(); } void turnOff() { digitalWrite(relayPin, HIGH); relayState = false; handleRoot(); } void setup() { Serial.begin(115200); pinMode(relayPin, OUTPUT); digitalWrite(relayPin, HIGH); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) delay(500); server.on("/", handleRoot); server.on("/on", turnOn); server.on("/off", turnOff); server.begin(); } void loop() { server.handleClient(); }

Fully works with a 1 channel relay module with ESP32
You can extend it for 4 channel relay module with ESP32, 8 channel relay module with ESP32, or even esp32 relay board x16 by adding more pins.

Expanding to 2/4/8/16 Channel Relay Boards

People often search:

  • 4 channel relay module with esp32
  • esp32 relay board x4
  • 2 channel relay module with esp32
  • esp32 relay board x2
  • 8 channel relay module with esp32
  • esp32 relay board x8
  • esp32 relay board x16

Let’s make it super simple.

Wiring Logic for Multiple Relays

Assign each relay an individual ESP32 GPIO:

RelayESP32 GPIO
RELAY123
RELAY222
RELAY321
RELAY419
RELAY518
RELAY65
RELAY717
RELAY816

For a 4 relay module with ESP32, only the first four lines matter.

For a 16 channel relay board, continue mapping more GPIOs.

Code Logic for Multiple Relays

int relays[] = {23, 22, 21, 19}; // For 4 relays
int totalRelays = 4;

void setup() {
  for(int i = 0; i < totalRelays; i++) {
    pinMode(relays[i], OUTPUT);
    digitalWrite(relays[i], HIGH);
  }
}

When expanding to an esp32 relay board 4 channel, just add web routes:

server.on("/relay1/on", []() { digitalWrite(23, LOW); });
server.on("/relay1/off", []() { digitalWrite(23, HIGH); });

Repeat for relay2, relay3, relay4.

ESP32 Relay Board Firmware What Makes It Fast & Reliable?

When people talk about esp32 relay board firmware, they usually mean the software that:

  • Handles GPIO switching
  • Protects from false triggering
  • Manages Wi-Fi reconnection
  • Hosts a web server or MQTT
  • Uses NVS memory for saving relay states
  • Boots fast
  • Recovers from power failures

A good firmware includes:

Debouncing Logic

Relays shouldn’t toggle rapidly.

Boot State Control

Relays must stay OFF at startup (many boards mistakenly turn ON).

Non-Blocking Code

Use millis() instead of delay().

Watchdog Logic

Relay switching must not freeze the ESP32.

Control ESP32 Relay Module Through MQTT (Advanced But Useful)

If you want to connect your automation setup to:

  • Home Assistant
  • Node-RED
  • IoT Cloud
  • Alexa
  • Google Home

MQTT is your best friend.

MQTT lets your ESP32 relay board receive commands like:

home/livingroom/light1 ON
home/livingroom/fan OFF

It’s perfect for a 4 channel relay module with ESP32 or 8 channel relay module with ESP32.

MQTT also fixes the issue where the esp32 relay module not working when Wi-Fi drops. MQTT reconnect logic keeps the device alive.

Controlling AC Appliances Safely

Searches like esp32 relay module control ac appliances make sense because people want to automate:

  • Lights
  • Fans
  • Pumps
  • Geysers
  • Chargers
  • Motors

Safety Rules:

  • Always use screw terminals
  • Keep AC wiring isolated
  • Never touch wires while powered
  • Use a fuse
  • Keep the relay module in a plastic box
  • Keep ESP32 away from high-voltage lines

If you’re using a 5v relay module with esp32, make sure to power it properly.

If you’re building a home-automation setup with an ESP32 with Relay Module, it’s smart to think about adding secure access or authentication too. Many makers pair their relay modules with fingerprint-based control so only authorized users can turn appliances on or off. If you want a clean, step-by-step guide to integrating a biometric sensor, check out this detailed tutorial on ESP32 with fingerprint sensor it walks you through wiring, enrolling fingerprints, and writing the code in a beginner-friendly, practical way. You can read it here: ESP32 with fingerprint sensor

Why the ESP32 Relay Module Is Not Working (Troubleshooting Guide)

This is one of the most common problems: esp32 relay module not working.
Here are the usual reasons:

Problem 1: Wrong Trigger Type

Relays are either:

  • LOW-trigger
  • HIGH-trigger

Most cheap relays from AliExpress are LOW-trigger.

Fix:
Try switching HIGH/LOW in code.

Problem 2: Not Enough Power

If you’re using an 8 channel relay module with ESP32 or esp32 relay board x16, ESP32 cannot power them directly.

Fix:
Use an external 5V adapter.

Problem 3: Missing GND

Relay GND must connect to ESP32 GND.

Problem 4: Weak USB Cable

Cheap USB cables cannot supply enough current.

Problem 5: Boot Time GPIO Conflicts

GPIO 0, 2, 4, 12, 15, 34–39 are tricky.

Avoid them.

Problem 6: Flyback Noise

When relays switch, they create noise.

Fix:
Use:

  • Optocouplers
  • Snubber circuit
  • Decoupling capacitors

ESP32 Relay Board Programming Best Practices

To build stable projects, here’s what experts follow:

Use Arrays for Multiple Relays

Avoid writing duplicate code.

Store Relay State in NVS

So relays remember their ON/OFF state after power-off.

Use Async Web Server

Fast and responsive UI.

Separate Wi-Fi Code and GPIO Code

Clean structure = fewer bugs.

Use FreeRTOS Tasks

ESP32 supports multitasking.

Example:

  • Task1 → read sensors
  • Task2 → handle web server
  • Task3 → control relays

Understanding the ESP32 Relay Module Schematic in Real Projects

Understanding Relay Board Variants (x1, x4, x8, x16) With ESP32

When you work with an ESP32 with Relay Module, you’ll find different versions in the market: ESP32 relay board x1, x4, x8, or even x16.
The number simply tells you how many independent appliances you can control. But the core electronics behind every relay channel is almost identical, no matter how big the board is.

How the Internal Schematic Works (Easy Explanation)

Every relay channel on any board whether it’s 1-channel, 4-channel, 8-channel, or 16-channel follows the same pattern:

1) ESP32 GPIO → Transistor (or Driver Stage)

The ESP32 output pin cannot drive a relay coil directly.
So a transistor (like 2N2222, S8050, TIP122, or ULN2003/2803 driver IC) acts as a switch amplifier.

Why this matters:

  • ESP32 GPIO outputs only 3.3V
  • Relay coils need 5V
  • Relays draw 70–90mA, which ESP32 pins cannot supply

The transistor handles the heavy work while the ESP32 only sends tiny control signals.

2) Transistor → Relay Coil

Once activated, the transistor completes the circuit and energizes the relay coil.
This is what physically causes the relay “click” and switches your AC or DC load.

3) Relay Coil → 5V Power Supply

All 5V relay modules require a stable 5V supply, separate from the ESP32’s 3.3V line.

This ensures:

  • stable switching,
  • reduced noise,
  • no random resets of the ESP32.

4) Flyback Diode (Mandatory Protection)

When the relay turns OFF, the collapsing magnetic field generates a voltage spike.
A flyback diode (usually 1N4007 or SS14) absorbs this spike and protects:

  • ESP32 pins
  • transistor driver
  • overall board

If the diode is missing, your ESP32 may freeze or reboot.

5) Optocoupler (Optional but Great)

Some relay boards include an optocoupler like PC817.
This acts as a safety barrier between the ESP32 and the relay coil circuit.

Benefits:

  • better noise immunity
  • isolates AC side from ESP32
  • prevents damage from surges

Not all modules have it, but it’s a good feature.

6) Common Ground (Important!)

Even if the relay uses a 5V supply, the ESP32 must share a common GND with the relay module.
Otherwise:

  • relay may not trigger
  • logic levels become unstable
  • you may see “ESP32 relay module not working” issues

This is the #1 mistake beginners make.

What If Your Relay Board Doesn’t Have a Transistor?

Some cheap boards especially single-channel ones connect the relay coil directly to a pin header. This is not safe for the ESP32.

If your module does not include a transistor or driver stage, you must add one externally:

Simple Safe Driver Circuit

  • Use 2N2222 or S8050 transistor
  • Add 1K base resistor
  • Add 1N4007 flyback diode
  • Power relay with external 5V
  • Connect GND to ESP32 GND

This protects your ESP32 and ensures long-term stable operation.

If your relay board doesn’t have a transistor or diode, do NOT use it with ESP32.

ESP32 Relay Board From AliExpress Should You Buy It?

Searches like esp32 relay board aliexpress are common because AliExpress sells ESP32 boards with:

  • Built-in relays (x1, x2, x4)
  • Optocouplers
  • Power supply circuits
  • Screw terminals

Many are excellent for beginners.

Pros:

  • Cheap
  • Ready to use
  • No wiring headache

Cons:

  • Sometimes unreliable firmware
  • Not always well-documented

Real ESP32 + Relay Module Project Ideas

Here are some practical and fun project ideas that naturally use all versions of relay boards, including 1 channel relay module with ESP32, 2 channel relay module with ESP32, 4 channel relay module with ESP32, and even 8 channel relay module with ESP32.

1. Smart Home Switchboard (4-Channel Relay + ESP32)

A neat solution to control:

  • Tube light
  • Fan
  • Charging socket
  • Night lamp

Use a 4 relay module with ESP32 and a simple Wi-Fi web server.
This is the cleanest beginner automation project.

2. Garden Pump Automation (1-Channel Relay + ESP32)

Using a 1 channel relay module with ESP32, you can automate a water pump.

Features you can add:

  • Automatic start at a time
  • Control from smartphone
  • Soil moisture sensor reading
  • Water level alerts

3. AC Loads Dashboard (8-Channel Relay + Web App)

Using an 8 channel relay module with ESP32, you can build a full home dashboard:

  • Kitchen lights
  • Balcony lights
  • Bed lamp
  • Fan
  • TV
  • Motor
  • Router
  • Inverter charger

This setup is very popular on YouTube tutorials.

4. ESP32 Relay Board With Firmware + MQTT

If you want pro-level setup:

  • Use esp32 relay board firmware
  • Connect to MQTT server
  • Add password protection
  • Control via Alexa or Google Home

This works best with esp32 relay board x4 or esp32 relay board x8.

5. ESP32 + Relay Module Internet Control (IFTTT + Google Home)

Using:

  • IFTTT applets
  • Webhooks
  • ESP32 endpoints

You can control relays from anywhere in the world.

Great for:

  • Turning lights ON before reaching home
  • Turning geyser ON remotely
  • Switching OFF appliances left ON accidentally

6. ESP32 Relay Board With AC Appliance Monitoring

Use sensors + relays together:

  • ACS712 current sensor
  • DS18B20 temperature sensor

ESP32 measures values and switches relay automatically.

In-Depth Beginner Q&A on ESP32 With Relay Module

Q: How to connect ESP32 with relay module safely?

To connect ESP32 with a relay module:

  1. Connect relay IN1 → ESP32 GPIO pin (e.g., 23).
  2. Connect relay VCC → 5V power supply.
  3. Connect relay GND → ESP32 GND.
  4. Use LOW or HIGH trigger correctly.

This applies to 5v relay module with ESP32, 4 channel relay module with ESP32, 2 channel relay module with ESP32, and others.

Q: How to use relay module with ESP32 for AC appliances?

To use a relay with AC:

  • Keep AC wiring separate
  • Use screw terminals
  • Use a plastic enclosure
  • Add a fuse
  • Never touch AC directly

ESP32 only sends a low-voltage signal to the relay coil.

Q: My esp32 relay module not working what should I check?

Check these:

  • Ground is common
  • Trigger polarity is correct
  • Relay board has a transistor driver
  • USB cable is not weak
  • ESP32 GPIO pins are not in boot mode
  • Use external 5V for 8 channel relay module with ESP32

Q: Can ESP32 power a relay module directly?

No.
ESP32 cannot drive the relay coil directly.

Always use a transistor-based or optocoupler-based relay board.

Q: Which GPIO pins should I avoid for relays?

Avoid boot-mode pins:

  • GPIO 0
  • GPIO 2
  • GPIO 4
  • GPIO 12
  • GPIO 15

Also avoid 34–39 (input only).

Q: What is the esp32 2 relay module pinout?

For a typical 2 channel relay module with ESP32:

  • IN1 → GPIO23
  • IN2 → GPIO22
  • VCC → 5V
  • GND → GND

Some boards may use screw terminals labeled NO / COM / NC.

Q: What is the esp32 relay module schematic?

The schematic normally includes:

  • ESP32 GPIO → transistor
  • Flyback diode
  • Optocoupler (sometimes)
  • Relay coil → 5V
  • NO/NC/COM terminal
  • Power LED + status LED

Same structure applies to esp32 relay board x1, x2, x4, x8, x16.

Q: Can I buy an ESP32 relay board from AliExpress? Is it reliable?

Yes.
Search for esp32 relay board aliexpress and you’ll find cheap boards (IN1–IN4 built-in).

Great for beginners but always check firmware quality.

Q: Can ESP32 control AC appliances through a web server?

Yes.
People commonly search for:

esp32 relay module control ac appliances web server

And the answer is:
YES — you can create a web interface to control:

  • Light
  • Fan
  • TV
  • Router
  • Water motor

Everything works smoothly with:

  • 1 channel relay module with ESP32
  • 4 channel relay module with ESP32
  • 8 channel relay module with ESP32

Practical Tips for Stable Relay Control With ESP32

Use external 5V adapter

Don’t rely on ESP32 5V pin for high-current relays.

Add flyback diode (if missing)

Most branded modules include it.

Use shielded wires for AC

Avoid noise and interference.

Don’t mount ESP32 and relay in same box

Heat + voltage spikes may damage ESP32.

Use Async Webserver

More stable than synchronous server.

Save states in NVS

So relays remember their last state after reboot.

Common Mistakes Beginners Make (Avoid These)

Connecting relay VCC to 3.3V

Use 5V always.

Using GPIO 34–39

These pins cannot output signals.

Triggering relay without a transistor

Direct GPIO → relay coil will destroy your ESP32.

No common GND

Nothing works without a common ground.

Using weak USB cable

Causes random resets.

Not isolating AC & DC

Always keep AC wiring separate from ESP32 wiring.

Complete Relay Compatibility List for ESP32

Relay TypeCompatible with ESP32?Notes
1 channel relay module with ESP32Best for small projects
2 channel relay module with ESP32Great for fans + lights
4 channel relay module with ESP32Perfect for room automation
8 channel relay module with ESP32Needs external power supply
5v relay module with ESP32Most common type
esp32 relay board x1All-in-one compact
esp32 relay board x2Two circuits
esp32 relay board x4Four circuits
esp32 relay board x8Eight circuits
esp32 relay board x16Large automation panels

Leave a Comment