กลับหน้าหลัก
views
Wiring ESP32 to Control a 4-Channel 3V Relay Module for Home Appliances
Last updated on

Wiring ESP32 to Control a 4-Channel 3V Relay Module for Home Appliances


Wiring ESP32 to Control a 4-Channel 3V Relay Module for Home Appliances

This guide walks through connecting an ESP32 NodeMCU ESP-WROOM-32 to a 4-Channel 3V Relay Module with isolation (High and Low Trigger support). You will see the pin mapping, wiring steps, and Arduino code that activates each relay in sequence.

Warning: The 220V AC section carries lethal voltage. If you are unsure, consult a licensed electrician.

Required Components

ComponentQtyNotes
ESP32 NodeMCU ESP-WROOM-321Wi-Fi + Bluetooth, Dual Core
4-Channel 3V Relay Module1Isolation High/Low Trigger 250V/10A
Breadboard 170 points1For prototyping
Micro USB Type B Cable 1m1
Micro USB Power Adapter 5V 2A1Power source for ESP32
ESP32 Screw Shield (30 Pin)1Makes wiring easier
Jumper Wires M-M 20cm40 pcs
Jumper Wires M-F 20cm40 pcs
Jumper Wires F-F 20cm40 pcs
E27 Lamp Socket + 7W LED Bulb1 setTest load
Power Cable 2*0.75 with Plug2 pcs1.5m and 2m
Top-down layout diagram showing ESP32 on Screw Shield positioned next to a breadboard and the 4-Channel Relay Module, with colored jumper wires connecting them

How a 4-Channel Relay Module Works

Each relay channel on the module has:

  • VCC — Powers the internal control circuit (5V)
  • GND — Common ground
  • Signal (S1-S4) — Receives digital signal from ESP32 to trigger the relay
  • COM — Common terminal of the contacts
  • NO — Normal Open contact — closes when relay activates
  • NC — Normal Closed contact — opens when relay activates

When the signal from ESP32 drives the relay, NO connects to COM, allowing AC current to flow through the load.

Internal relay circuit diagram for one channel showing VCC, GND, Signal input on the left connected through an optocoupler to the relay coil, with COM, NO, NC output terminals on the right

Wiring ESP32 to the Relay Module

Control Side (5V DC)

ESP324-Channel Relay Board
Vin (5V)VCC
GNDGND
D23S1
D22S2
D21S3
D19S4
Pin mapping table between ESP32 and Relay Module with recommended wire colors — Vin (red) → VCC, GND (black) → GND, D23 (orange) → S1, D22 (yellow) → S2, D21 (green) → S3, D19 (blue) → S4

Load Side (220V AC)

To control one light bulb (using relay channel 4):

  • Live wire (L, brown/red) from wall outlet → NO terminal of channel 4
  • COM terminal of channel 4 → Live wire going to the lamp
  • Neutral wire (N, blue) from wall outlet → Neutral wire of lamp (direct connection)

Note: To control multiple lights, wire each additional bulb using the same pattern on channels 1, 2, or 3.

[image: AC 220V wiring diagram — wall outlet L wire enters relay channel 4 at NO terminal, exits at COM terminal and goes to E27 lamp socket, neutral N wire connects directly to lamp]

Arduino Code — Sequential Relay Control

// Define signal pins connected to Relay 4 Channel
const int RELAY_S1 = 23;  // Relay channel 1
const int RELAY_S2 = 22;  // Relay channel 2
const int RELAY_S3 = 21;  // Relay channel 3
const int RELAY_S4 = 19;  // Relay channel 4

void setup() {
  // Set all 4 signal pins as OUTPUT
  pinMode(RELAY_S1, OUTPUT);
  pinMode(RELAY_S2, OUTPUT);
  pinMode(RELAY_S3, OUTPUT);
  pinMode(RELAY_S4, OUTPUT);

  // Start with all relays OFF
  // Adjust to HIGH or LOW depending on your board type
  digitalWrite(RELAY_S1, LOW);
  digitalWrite(RELAY_S2, LOW);
  digitalWrite(RELAY_S3, LOW);
  digitalWrite(RELAY_S4, LOW);
}

void loop() {
  // Turn on relays one by one, 1 second apart
  digitalWrite(RELAY_S1, HIGH);
  delay(1000);

  digitalWrite(RELAY_S2, HIGH);
  delay(1000);

  digitalWrite(RELAY_S3, HIGH);
  delay(1000);

  digitalWrite(RELAY_S4, HIGH);  // Light bulb turns on here
  delay(1000);

  // Turn off relays one by one, 1 second apart
  digitalWrite(RELAY_S1, LOW);
  delay(1000);

  digitalWrite(RELAY_S2, LOW);
  delay(1000);

  digitalWrite(RELAY_S3, LOW);
  delay(1000);

  digitalWrite(RELAY_S4, LOW);
  delay(1000);
}

Upload Steps

  1. Open Arduino IDE → paste the code above
  2. Go to Tools → Port → select the port your ESP32 is connected to
  3. Go to Tools → Board → select your ESP32 board type
  4. Click the Upload button (right arrow icon)
  5. Wait for Done uploading message

Adjustment needed: If the relay does not activate, try swapping digitalWrite(pin, LOW) with HIGH in setup(). Some relay boards are Active LOW, meaning the coil energizes when the signal pin is pulled LOW.

Testing the Setup

After uploading, observe this behavior:

  1. Relay channel 1 activates → audible click
  2. Relay channel 2 activates → audible click
  3. Relay channel 3 activates → audible click
  4. Relay channel 4 activates → light bulb turns ON (AC circuit closes)
  5. Relay channels 1–4 deactivate in sequence → light bulb turns OFF
  6. Cycle repeats every 8 seconds

[image: Relay timeline diagram showing 4 rows (one per relay channel) with HIGH periods marked in dark color and LOW periods in light color, annotated with when the light bulb is ON]

Safety Warnings

  • Separate DC and AC wiring — Route 220V wires cleanly away from signal wires
  • Use properly rated lamp sockets — E27 sockets must have protective covers to prevent electric shock
  • Never touch AC wiring while powered — Disconnect the plug before wiring
  • Check relay specifications — This board supports 250V/10A. Do not exceed this rating
  • Optocoupler isolation protects ESP32 — The board’s optocoupler isolates the control circuit from the load, preventing voltage spikes from damaging your ESP32

Expanding the Project

Active LOW vs Active HIGH

If your relay behaves opposite to the code (always on or always off), your board uses Active LOW triggering. Swap the logic in setup() and loop() accordingly:

void setup() {
  // For Active LOW relay boards
  digitalWrite(RELAY_S1, HIGH);  // Keep relay OFF
  digitalWrite(RELAY_S2, HIGH);
  digitalWrite(RELAY_S3, HIGH);
  digitalWrite(RELAY_S4, HIGH);
}

void loop() {
  digitalWrite(RELAY_S1, LOW);   // Turn relay ON
  // ... rest of the code
}

Control Multiple Lights Together

To turn all lights on/off simultaneously:

void loop() {
  // All relays ON
  digitalWrite(RELAY_S1, HIGH);
  digitalWrite(RELAY_S2, HIGH);
  digitalWrite(RELAY_S3, HIGH);
  digitalWrite(RELAY_S4, HIGH);
  delay(3000);

  // All relays OFF
  digitalWrite(RELAY_S1, LOW);
  digitalWrite(RELAY_S2, LOW);
  digitalWrite(RELAY_S3, LOW);
  digitalWrite(RELAY_S4, LOW);
  delay(3000);
}

Add Wi-Fi Remote Control

Once ESP32 connects to Wi-Fi, you can add a Web Server or use Blynk to control the relays from your phone. This is the foundation for building a Smart Home system.

Summary

Wiring ESP32 to a 4-Channel 3V Relay Module is straightforward: power the relay board with 5V, share ground, and drive the signal pins (D23, D22, D21, D19) using digitalWrite(). For AC control, wire live current through COM and NO terminals properly, and always prioritize safety when working with household voltage.

Reference Video

https://www.youtube.com/embed/V0laB4hj1W8

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

Project estimate

Want something like this? Open the estimate page.

The long form is now on a separate estimate page, so this article stays clean.

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...