A relay module lets the ESP32 switch a circuit that is electrically separate from it — a lamp, a heater element, or the coil of a contactor. The DevKit V1 can’t power the coil from its 3.3 V pin, so the coil runs from VIN (5 V) and only the control line comes from a GPIO.
Two ESP32 details matter: pick a pin that stays quiet during boot (GPIO26 here, not a strapping pin), and set the OFF level before the pin becomes an output so the relay never clicks on at reset. Type on or off in the Serial Monitor to switch it.
The low-voltage side is safe to wire on the bench. Anything on the screw terminals at 230 V is electrician-only work — and motors or pumps always go through a contactor, never straight through this relay.
Wiring
| ESP32 DevKit V1 | 1-channel relay module (5 V coil) | Wire | Notes |
|---|---|---|---|
| — | COM | — | To Lamp circuit — live in from the MCB (230 V, electrician only) (not drawn) |
| — | NO | — | To Lamp circuit — live to a small lamp within the relay rating (230 V, electrician only) (not drawn) |
| VIN | DC+ | red | 5 V for the coil (about 70 mA) — only while the board runs from USB or a 5 V supply. 3V3 is too low for a 5 V coil. |
| GND | DC− | black | |
| D26 | IN | green | Active-LOW on most modules. GPIO26 is not a strapping pin and stays quiet during boot. |
5 V coil part, 3.3 V board. The coil runs on 5 V, but the IN pin only drives an optocoupler LED. Pick a module rated for a 3.3 V trigger when using ESP32 or Pico — some 5 V-only modules never fully release from a 3.3 V HIGH.
Board quirks
- The sketch writes the OFF level before
pinMode(OUTPUT). Otherwise an active-LOW relay can click on for a moment at every reset. - Avoid D2, D5, D12, D14 and D15 for relays: they are strapping pins or output PWM during boot, so the relay chatters when the ESP32 starts.
- A 3.3 V HIGH may not fully switch off a module built for a 5 V trigger (the opto LED still glows faintly). Buy a module sold as “3.3 V trigger” / “high-low level trigger”, or drive IN through an NPN transistor.
- 230 V is lethal. Only the DC+, DC− and IN side is low voltage. The COM/NO/NC terminals carry mains: have an electrician wire them behind an MCB and RCCB, switch the live wire only, earth any metal enclosure, and keep the module in a closed plastic box.
- The “10 A 250 VAC” rating is for resistive loads. A pump, motor or compressor draws several times its running current at start — let the relay switch a contactor coil, and the contactor the motor.
Code
// 1-channel relay module (active-LOW) on an ESP32 DevKit V1.
// DC+ → VIN (5 V), DC− → GND, IN → D26 (GPIO26). Type "on" or "off" in the Serial Monitor.
// Low-voltage side only. The screw terminals (COM/NO) carry 230 V — electrician only.
constexpr uint8_t RELAY_PIN = 26;
constexpr bool ACTIVE_LOW = true; // most blue relay modules: LOW = relay on
void setRelay(bool on) {
digitalWrite(RELAY_PIN, (on ^ ACTIVE_LOW) ? HIGH : LOW);
Serial.printf("Relay %s\n", on ? "ON (COM–NO closed)" : "OFF (COM–NC closed)");
}
void setup() {
// Drive the pin to the OFF level before it becomes an output, so the relay never
// clicks on during boot.
digitalWrite(RELAY_PIN, ACTIVE_LOW ? HIGH : LOW);
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Relay on GPIO26 — type on / off");
setRelay(false);
}
void loop() {
if (!Serial.available()) return;
String cmd = Serial.readStringUntil('\n');
cmd.trim();
cmd.toLowerCase();
if (cmd == "on") setRelay(true);
else if (cmd == "off") setRelay(false);
else if (cmd.length()) Serial.println("Type on or off");
}// 1-channel relay module (active-LOW) on an ESP32 DevKit V1.
// DC+ → VIN (5 V), DC− → GND, IN → D26 (GPIO26). Type "on" or "off" in the Serial Monitor.
// Low-voltage side only. The screw terminals (COM/NO) carry 230 V — electrician only.
constexpr uint8_t RELAY_PIN = 26;
constexpr bool ACTIVE_LOW = true; // most blue relay modules: LOW = relay on
void setRelay(bool on) {
digitalWrite(RELAY_PIN, (on ^ ACTIVE_LOW) ? HIGH : LOW);
Serial.printf("Relay %s\n", on ? "ON (COM–NO closed)" : "OFF (COM–NC closed)");
}
void setup() {
// Drive the pin to the OFF level before it becomes an output, so the relay never
// clicks on during boot.
digitalWrite(RELAY_PIN, ACTIVE_LOW ? HIGH : LOW);
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Relay on GPIO26 — type on / off");
setRelay(false);
}
void loop() {
if (!Serial.available()) return;
String cmd = Serial.readStringUntil('\n');
cmd.trim();
cmd.toLowerCase();
if (cmd == "on") setRelay(true);
else if (cmd == "off") setRelay(false);
else if (cmd.length()) Serial.println("Type on or off");
}Expected serial output
Relay on GPIO26 — type on / off
Relay OFF (COM–NC closed)
Relay ON (COM–NO closed)
Relay OFF (COM–NC closed)Relay on GPIO26 — type on / off Relay OFF (COM–NC closed) Relay ON (COM–NO closed) Relay OFF (COM–NC closed)
Common errors
Relay clicks on when the ESP32 resets or uploads
The pin floats until setup() runs. The sketch drives it HIGH first; for a hard guarantee add a 10 kΩ pull-up from IN to 3V3. Don’t use D2, D12 or D15 for the relay.
Relay turns on but never turns off
A 5 V-trigger module seeing 3.3 V as “not quite HIGH”. Use a 3.3 V-trigger module or an NPN transistor driver. Check also that ACTIVE_LOW matches your module (some are active-HIGH).
ESP32 resets or Wi-Fi drops when the load switches
Coil and load switching noise on the shared 5 V. Power the relay from its own 5 V supply with a common GND, and keep mains wiring away from the ESP32 and its USB cable.