This project builds a WiFi-controlled four-channel relay module using an ESP32 microcontroller. The SunFounder optocoupled relay module provides electrical isolation for switching high-power loads, while tactile buttons offer local manual control. The system persists state across power cycles and serves a simple web interface for remote toggling.
The guide includes a complete wiring diagram showing how to connect the relay module's signal pins through 2N3904 transistor stages, a parts list with all resistor values, step-by-step assembly instructions, and Arduino firmware with WiFi connectivity and non-volatile storage. Assembly requires careful attention to the relay module's separate 5V supply and the transistor signal inversion logic.
Wiring diagram

Gather all the parts
Assemble it in 3 steps
1. Wire the buttons and transistor stages
With power disconnected, assemble the four identical stages according to the wiring view. Check the transistor lead order against its datasheet. Each button shorts its GPIO to ground when pressed.
2. Connect the separate relay supply
Remove the JD-VCC jumper. Feed JD-VCC and VCC from the external regulated 5 V adapter using a USB-C 5 V power breakout cable. Join adapter, relay and ESP32 grounds. Power the ESP32 separately over USB. Do not connect adapter positive to any ESP32 pin.
3. Test without a load
Leave all relay screw terminals empty. Power the relay board first and confirm all channels stay off until the ESP32 intentionally restores their saved state. Test reset, button presses, and a full power cycle.
- Keep mains off the prototype. Saved ON channels restart automatically.
Review all connections
1. Connections between "supply" and "ESP32"
2. Connections between "relay" and "ESP32"
3. Connections between "rb1" and "ESP32"
4. Connections between "rd1" and "ESP32"
5. Connections between "q1" and "ESP32"
6. Connections between "ru1" and "ESP32"
7. Connections between "b1" and "ESP32"
8. Connections between "rb2" and "ESP32"
9. Connections between "rd2" and "ESP32"
10. Connections between "q2" and "ESP32"
11. Connections between "ru2" and "ESP32"
12. Connections between "b2" and "ESP32"
13. Connections between "rb3" and "ESP32"
14. Connections between "rd3" and "ESP32"
15. Connections between "q3" and "ESP32"
16. Connections between "ru3" and "ESP32"
17. Connections between "b3" and "ESP32"
18. Connections between "rb4" and "ESP32"
19. Connections between "rd4" and "ESP32"
20. Connections between "q4" and "ESP32"
21. Connections between "ru4" and "ESP32"
22. Connections between "b4" and "ESP32"
Deploy the firmware
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <Preferences.h>
#include <driver/gpio.h>
constexpr char ssid[] = "YOUR_WIFI_SSID";
constexpr char password[] = "YOUR_WIFI_PASSWORD";
constexpr gpio_num_t outputs[] = {GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_21, GPIO_NUM_22};
constexpr uint8_t buttons[] = {25,26,27,32};
Preferences nvs;
WebServer server(80);
uint8_t mask = 0;
bool storage, online = false;
uint32_t lastRetry;
struct Button { bool raw, stable; uint32_t changed; } keys[4];
// The transistor inverts the signal: GPIO HIGH pulls relay IN low.
void apply() {
for (int i=0;i<4;++i) gpio_set_level(outputs[i], (mask >> i) & 1);
}
void toggle(int i) {
mask ^= 1u << i;
apply();
// One byte for all channels; write only when a state changes.
if (!storage || nvs.putUChar("states", mask) != 1)
Serial.println("State changed but could not be saved");
}
void page() {
String html = "<!doctype html><meta name='viewport' content='width=device-width'><title>Relay control</title><h1>Relay control</h1>";
for (int i=0;i<4;++i) {
html += "<form method='post' action='/toggle'><button name='ch' value='";
html += i+1;
html += "'>Channel "; html += i+1;
html += (mask & (1u<<i)) ? " ON" : " OFF";
html += "</button></form>";
}
server.sendHeader("Cache-Control", "no-store");
server.send(200,"text/html",html);
}
void setup() {
// Set the output latch before enabling each output driver.
for (auto pin : outputs) {
gpio_set_level(pin, 0);
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
}
Serial.begin(115200);
for (int i=0;i<4;++i) {
pinMode(buttons[i], INPUT_PULLUP);
bool level = digitalRead(buttons[i]);
keys[i] = {level, level, millis()};
}
storage = nvs.begin("relays", false);
if (storage) mask = nvs.getUChar("states",0) & 0x0F;
else Serial.println("NVS unavailable; starting OFF");
apply(); // Restore before starting Wi-Fi, even without an access point.
server.on("/",HTTP_GET,page);
server.on("/toggle",HTTP_POST,[] {
String ch = server.arg("ch");
if (ch.length()!=1 || ch[0]<'1' || ch[0]>'4') {
server.send(400,"text/plain","Invalid channel"); return;
}
toggle(ch[0]-'1');
server.sendHeader("Location","/");
server.send(303,"text/plain", "");
});
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(true);
WiFi.begin(ssid,password);
lastRetry = millis();
}
void loop() {
uint32_t now = millis();
for (int i=0;i<4;++i) {
Button &k = keys[i];
bool level = digitalRead(buttons[i]);
if (level != k.raw) { k.raw=level; k.changed=now; }
if (k.raw!=k.stable && now-k.changed>=30) {
k.stable=k.raw;
if (!k.stable) toggle(i); // One toggle per press, none on release.
}
}
if (WiFi.status()==WL_CONNECTED) {
if (!online) {
server.begin(); online=true;
Serial.print("Open http://"); Serial.println(WiFi.localIP());
}
server.handleClient();
} else {
if (online) { server.stop(); online=false; }
if (now-lastRetry>=15000) { lastRetry=now; WiFi.reconnect(); }
}
}Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




