← Beginner's guide to ESP32

Build an ESP32 Wi-Fi relay switch

Turn a lamp, fan or LED strip on and off from a web page on your phone. This is your first project that uses Wi-Fi, and the web server you build here is the foundation for dozens of other projects.

Safety: low voltage only. This guide switches low-voltage DC loads (12V or less, such as an LED strip, a small fan or a battery lamp). Do not connect mains electricity (wall power) to a relay unless you are qualified and the circuit is in a proper enclosure with a fuse. Mains voltage can kill.

What you will learn

Before you start: do steps 1 to 5 of the beginner's guide (install the Arduino IDE and run Blink). It helps to have built the motion alarm first, but it is not required.

Your phoneopens the web pagetaps "Turn ON" ESP32web server on Wi-Fisets GPIO 26 Relayclicks and closesthe circuit Loadlamp, fan,LED strip Wi-Fi
The data path. Your phone and the ESP32 must be on the same Wi-Fi network.

What is a relay?

A relay is an electrically operated switch. A small current from the ESP32 energises a coil inside, which clicks a metal contact closed. Because the switching contact is separate from the control side, a tiny 3.3V signal can control a device that uses far more power than the ESP32 could ever deliver. That is also why you can hear it click.

Parts you need

Some links are affiliate links, so I may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Details. Buy buttons open a store search; check the product matches before you order.

Wire it up

Part A: the control side. Board unplugged from USB while you wire.

Red = power, black = ground, yellow = control signal. The relay's coil is powered from 5V, but the signal wire carries only 3.3V.
Relay module pinConnects to (classic ESP32)
VCC5V (the pin labelled VIN or 5V)
GNDGND
INGPIO 26
Using an ESP32-S3 or ESP32-C3?
SignalClassic ESP32ESP32-S3ESP32-C3
Relay INGPIO 26GPIO 15GPIO 10

Change RELAY_PIN at the top of the sketch to match.

Part B: the load side. This is a separate circuit that does not touch the ESP32. The relay is simply a switch in the middle of one wire.

Power supply12V DC, matches load + (positive)- (negative) Relay terminals COMNONC (not used) LoadLED strip / fan + (positive)- (negative) When the relay clicks on, COM connects to NO and the load gets power.
Load circuit. Use the COM and NO terminals. NO means "normally open": the circuit is off until the relay is switched on.

Tip: test the control side first with nothing connected to the relay's screw terminals. If you hear a click when you press the button on the web page, the ESP32 side works. Then add the load.

The code

  1. Open a new sketch in the Arduino IDE and replace everything with the code below. Change WIFI_SSID and WIFI_PASS to your Wi-Fi name and password (both are case-sensitive).
  2. Upload it (hold BOOT if it stalls on "Connecting..."), then open Tools, Serial Monitor at 115200 baud. Press the EN (reset) button on the board.
  3. The monitor prints something like Open http://192.168.1.57. That is the ESP32's address on your network.

How the code works

Test it

  1. On a phone or computer connected to the same Wi-Fi, open the address from the Serial Monitor (for example http://192.168.1.57). You should see a big green Turn ON button.
  2. Tap it. The relay clicks, the page turns red and says Relay is ON. Tap again to switch off.
  3. With the load connected as in Part B, it should now switch on and off.
  4. Try http://relay.local. On most phones and recent Windows and Mac computers it works too, so you do not need to remember the number.

About security. Anyone on your home network who knows the address can press the button. That is fine for a lamp on your own Wi-Fi. Do not expose it to the internet, and do not use it for anything that could cause harm if switched unexpectedly.

Troubleshooting

ProblemWhat to try
Dots forever, then "Could not connect"Check the Wi-Fi name and password for typos. The ESP32 only supports 2.4 GHz, not 5 GHz-only networks. Some routers use one name for both bands; if so, make sure the 2.4 GHz band is on.
Page will not load on my phoneThe phone must be on the same network (not mobile data). Some "guest" networks block devices from talking to each other.
Relay clicks the wrong way roundChange RELAY_ON from LOW to HIGH.
Relay clicks briefly at power-upNormal for some modules. The sketch keeps it as quiet as it can.
Relay clicks but the load stays offCheck the load wiring: COM and NO (not NC), and that the load power supply is plugged in and matches the load.
Relay does not click at allCheck VCC is on 5V (not 3.3V), GND is connected, and IN goes to GPIO 26.
Board resets when the relay switchesSwitching can cause electrical noise. Use a short USB cable or a better power source, and keep the load wires away from the ESP32.

Make it better

Open this project in the Build Finder →

Where to next

Project 3: Wi-Fi weather station. Read a sensor and show it on a web page.  ·  ← Project 1: Motion alarm

This guide was written from standard Arduino and ESP32 practice (the WebServer and ESPmDNS libraries are part of the ESP32 Arduino core). It has not been built on every ESP32 variant. Not affiliated with the part makers.