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
- What a relay is, and why the ESP32 cannot power a lamp directly.
- How to connect the ESP32 to your Wi-Fi network.
- How to run a tiny web server on the ESP32 and serve a page that works on any phone.
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.
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.
| Relay module pin | Connects to (classic ESP32) |
|---|---|
| VCC | 5V (the pin labelled VIN or 5V) |
| GND | GND |
| IN | GPIO 26 |
Using an ESP32-S3 or ESP32-C3?
| Signal | Classic ESP32 | ESP32-S3 | ESP32-C3 |
|---|---|---|---|
| Relay IN | GPIO 26 | GPIO 15 | GPIO 10 |
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.
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
- Open a new sketch in the Arduino IDE and replace everything with the code below. Change
WIFI_SSIDandWIFI_PASSto your Wi-Fi name and password (both are case-sensitive). - 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.
- The monitor prints something like
Open http://192.168.1.57. That is the ESP32's address on your network.
How the code works
WiFi.begin()joins your network. The loop waits up to 20 seconds and prints a dot every half second so you can see progress.WebServer server(80)listens on port 80, the normal web port.server.on("/toggle", ...)says "when a browser asks for/toggle, run this function".- The page is just a text string of HTML.
+=adds more text to the end. The ternaryrelayState ? "ON" : "OFF"means "if the relay is on, use ON, otherwise OFF". loop()callsserver.handleClient()over and over. That is all it takes to keep the server responsive.
Test it
- 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. - Tap it. The relay clicks, the page turns red and says Relay is ON. Tap again to switch off.
- With the load connected as in Part B, it should now switch on and off.
- 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
| Problem | What 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 phone | The 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 round | Change RELAY_ON from LOW to HIGH. |
| Relay clicks briefly at power-up | Normal for some modules. The sketch keeps it as quiet as it can. |
| Relay clicks but the load stays off | Check 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 all | Check VCC is on 5V (not 3.3V), GND is connected, and IN goes to GPIO 26. |
| Board resets when the relay switches | Switching 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
- Add a second relay (a 2-channel module) and a second button.
- Add a physical push button on a spare GPIO to toggle the relay by hand.
- Show the state on a tiny OLED display (project 4).
- Add a password with
server.authenticate(), or join the idea to Home Assistant (see the touch-panel guide).
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