Community project

ESP32 Four-Channel Home Automation

Photo of ESP32 Four-Channel Home Automation
Generated with AI

Agniname 2

Published December 3, 2026

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

Wiring diagram for ESP32 Four-Channel Home Automation
Loading diagram…

Gather all the parts

QtyComponent
1

SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module

Active-low optocoupled relay board with separate coil supply and onboard flyback diodes.

1

USB-C 5V Adapter

5 V, 2 A

USB-C wall adapter delivering regulated 5 V to the board's USB or VBUS rail. Default wired power source for desktop / stationary projects.

1

2N3904 NPN Transistor

General-purpose NPN bipolar junction transistor (BJT) in a TO-92 package. Rated for 60V collector-emitter voltage and 200mA continuous collector current with an hFE (gain) of 100–300. Commonly used as a low-side switch or signal amplifier driven by a microcontroller digital output pin via a current-limiting base resistor.

1

12 mm Tactile Push Button (Round Cap)

Momentary 12 x 12 mm four-leg through-hole tactile push button with a round cap. Closes a normally open contact while pressed; wire diagonally opposite legs between a digital input configured with INPUT_PULLUP and GND.

1

Resistor

820 ohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

2N3904 NPN Transistor

General-purpose NPN bipolar junction transistor (BJT) in a TO-92 package. Rated for 60V collector-emitter voltage and 200mA continuous collector current with an hFE (gain) of 100–300. Commonly used as a low-side switch or signal amplifier driven by a microcontroller digital output pin via a current-limiting base resistor.

1

12 mm Tactile Push Button (Round Cap)

Momentary 12 x 12 mm four-leg through-hole tactile push button with a round cap. Closes a normally open contact while pressed; wire diagonally opposite legs between a digital input configured with INPUT_PULLUP and GND.

1

Resistor

820 ohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

2N3904 NPN Transistor

General-purpose NPN bipolar junction transistor (BJT) in a TO-92 package. Rated for 60V collector-emitter voltage and 200mA continuous collector current with an hFE (gain) of 100–300. Commonly used as a low-side switch or signal amplifier driven by a microcontroller digital output pin via a current-limiting base resistor.

1

12 mm Tactile Push Button (Round Cap)

Momentary 12 x 12 mm four-leg through-hole tactile push button with a round cap. Closes a normally open contact while pressed; wire diagonally opposite legs between a digital input configured with INPUT_PULLUP and GND.

1

Resistor

820 ohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

2N3904 NPN Transistor

General-purpose NPN bipolar junction transistor (BJT) in a TO-92 package. Rated for 60V collector-emitter voltage and 200mA continuous collector current with an hFE (gain) of 100–300. Commonly used as a low-side switch or signal amplifier driven by a microcontroller digital output pin via a current-limiting base resistor.

1

12 mm Tactile Push Button (Round Cap)

Momentary 12 x 12 mm four-leg through-hole tactile push button with a round cap. Closes a normally open contact while pressed; wire diagonally opposite legs between a digital input configured with INPUT_PULLUP and GND.

1

Resistor

820 ohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

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"

FunctionsupplyESP32
power+5V → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module JD-VCCEXT
groundGNDGND

2. Connections between "relay" and "ESP32"

FunctionrelayESP32
powerVCC → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module JD-VCCEXT
groundGNDGND

3. Connections between "rb1" and "ESP32"

Functionrb1ESP32
digitalP1GPIO 18
digitalP2 → 2N3904 NPN Transistor Base (B)EXT

4. Connections between "rd1" and "ESP32"

Functionrd1ESP32
digitalP1 → 2N3904 NPN Transistor Base (B)EXT
groundP2GND

5. Connections between "q1" and "ESP32"

Functionq1ESP32
groundEmitter (E)GND
dataCollector (C) → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module IN1EXT

6. Connections between "ru1" and "ESP32"

Functionru1ESP32
digitalP1 → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module VCCEXT
digitalP2 → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module IN1EXT

7. Connections between "b1" and "ESP32"

Functionb1ESP32
digitalSIGNALGPIO 25
groundGNDGND

8. Connections between "rb2" and "ESP32"

Functionrb2ESP32
digitalP1GPIO 19
digitalP2 → 2N3904 NPN Transistor Base (B)EXT

9. Connections between "rd2" and "ESP32"

Functionrd2ESP32
digitalP1 → 2N3904 NPN Transistor Base (B)EXT
groundP2GND

10. Connections between "q2" and "ESP32"

Functionq2ESP32
groundEmitter (E)GND
dataCollector (C) → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module IN2EXT

11. Connections between "ru2" and "ESP32"

Functionru2ESP32
digitalP1 → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module VCCEXT
digitalP2 → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module IN2EXT

12. Connections between "b2" and "ESP32"

Functionb2ESP32
digitalSIGNALGPIO 26
groundGNDGND

13. Connections between "rb3" and "ESP32"

Functionrb3ESP32
digitalP1GPIO 21
digitalP2 → 2N3904 NPN Transistor Base (B)EXT

14. Connections between "rd3" and "ESP32"

Functionrd3ESP32
digitalP1 → 2N3904 NPN Transistor Base (B)EXT
groundP2GND

15. Connections between "q3" and "ESP32"

Functionq3ESP32
groundEmitter (E)GND
dataCollector (C) → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module IN3EXT

16. Connections between "ru3" and "ESP32"

Functionru3ESP32
digitalP1 → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module VCCEXT
digitalP2 → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module IN3EXT

17. Connections between "b3" and "ESP32"

Functionb3ESP32
digitalSIGNALGPIO 27
groundGNDGND

18. Connections between "rb4" and "ESP32"

Functionrb4ESP32
digitalP1GPIO 22
digitalP2 → 2N3904 NPN Transistor Base (B)EXT

19. Connections between "rd4" and "ESP32"

Functionrd4ESP32
digitalP1 → 2N3904 NPN Transistor Base (B)EXT
groundP2GND

20. Connections between "q4" and "ESP32"

Functionq4ESP32
groundEmitter (E)GND
dataCollector (C) → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module IN4EXT

21. Connections between "ru4" and "ESP32"

Functionru4ESP32
digitalP1 → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module VCCEXT
digitalP2 → SunFounder 4-channel 5V optocoupled low-trigger JD-VCC relay module IN4EXT

22. Connections between "b4" and "ESP32"

Functionb4ESP32
digitalSIGNALGPIO 32
groundGNDGND

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.

Open in Schematik