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
| Component | Qty | Notes |
|---|---|---|
| ESP32 NodeMCU ESP-WROOM-32 | 1 | Wi-Fi + Bluetooth, Dual Core |
| 4-Channel 3V Relay Module | 1 | Isolation High/Low Trigger 250V/10A |
| Breadboard 170 points | 1 | For prototyping |
| Micro USB Type B Cable 1m | 1 | |
| Micro USB Power Adapter 5V 2A | 1 | Power source for ESP32 |
| ESP32 Screw Shield (30 Pin) | 1 | Makes wiring easier |
| Jumper Wires M-M 20cm | 40 pcs | |
| Jumper Wires M-F 20cm | 40 pcs | |
| Jumper Wires F-F 20cm | 40 pcs | |
| E27 Lamp Socket + 7W LED Bulb | 1 set | Test load |
| Power Cable 2*0.75 with Plug | 2 pcs | 1.5m and 2m |
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.
Wiring ESP32 to the Relay Module
Control Side (5V DC)
| ESP32 | 4-Channel Relay Board |
|---|---|
| Vin (5V) | VCC |
| GND | GND |
| D23 | S1 |
| D22 | S2 |
| D21 | S3 |
| D19 | 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
- Open Arduino IDE → paste the code above
- Go to Tools → Port → select the port your ESP32 is connected to
- Go to Tools → Board → select your ESP32 board type
- Click the Upload button (right arrow icon)
- Wait for Done uploading message
Adjustment needed: If the relay does not activate, try swapping
digitalWrite(pin, LOW)withHIGHin 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:
- Relay channel 1 activates → audible click
- Relay channel 2 activates → audible click
- Relay channel 3 activates → audible click
- Relay channel 4 activates → light bulb turns ON (AC circuit closes)
- Relay channels 1–4 deactivate in sequence → light bulb turns OFF
- 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
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย