Skip to content

Relay · ESP32 guide

Controlling 5V Relay Module (1-Channel, SRD-05VDC-SL-C) with ESP32

Let a 5 V or 3.3 V pin switch a lamp, pump, fan or solenoid on its own supply, with COM, NO and NC screw terminals.

Updated November 25, 2026 Arduino C++None neededAlso forArduino Uno

What is the 5V Relay Module?

A relay module is the simplest way to let a microcontroller turn something bigger on and off: a 12 V pump, an LED strip, a solenoid lock, a fan, or (carefully) a mains lamp. The Arduino never touches the load. It only energises a small coil, and the coil pulls a metal contact that closes a completely separate circuit.

The common blue 1-channel module carries a Songle SRD-05VDC-SL-C relay, a transistor to drive its coil, a flyback diode, a status LED and, on most versions, an optocoupler. That extra circuitry is why you can connect it straight to a GPIO pin instead of building the driver yourself. This page explains what each part does, how to wire the load side, why most modules switch ON when the input is LOW, and what changes on a 3.3 V ESP32.

Specifications

Relay
Songle SRD-05VDC-SL-C (or equivalent)
Coil voltage
5 V DC (operates from about 3.75 V)
Coil current
about 70–75 mA while ON (coil ≈ 70 Ω)
Contact rating (relay)
10 A at 250 V AC, 10 A at 30 V DC
Practical module limit
about 5 A continuous; PCB traces and screw terminals are the weak point
Contact form
SPDT: COM switches between NC and NO
Input (IN) current
about 1–5 mA, depending on optocoupler and LED
Trigger
usually active LOW; some boards have an H/L jumper
Switching time
about 10 ms to operate, 5 ms to release
Isolation
optocoupler versions, with the JD-VCC jumper removed

Relay figures from the Songle SRD datasheet. Module limits vary between makers, so treat 10 A as the relay's rating, not the board's.

Relay module pinout

PinSideWhat it does
VCCControl5 V supply for the coil driver and LED
GNDControlGround, shared with the microcontroller
INControlSignal from a GPIO pin. LOW turns the relay ON on active-LOW boards
COMLoadCommon contact: connect one wire of the load circuit here
NOLoadNormally open: connected to COM only while the relay is ON
NCLoadNormally closed: connected to COM while the relay is OFF
JD-VCCJumperSeparate coil supply on optocoupler boards (jumpered to VCC by default)

Wiring & code

Setting up 5V Relay Module with ESP32

Library

None needed by ESP32 Arduino core

No library required: the sketch uses digitalWrite() and the Serial Monitor from the ESP32 Arduino core.

A 5 V relay module and an ESP32 do work together, but not quite the way most tutorials wire them. The relay's coil needs 5 V, while the ESP32's pins only reach 3.3 V. On the usual active-LOW module, 3.3 V is often not high enough to switch the relay off: it clicks on and then stays on. This guide shows the JD-VCC wiring that fixes it, which GPIO pins to use, and a sketch you can control from the Serial Monitor.

Warning

Why 3.3 V fails on many modules. The optocoupler LED sits between the module's VCC (5 V) and IN. Pulling IN LOW turns it on. Setting IN to 3.3 V should turn it off, but that still leaves 1.7 V across an LED that conducts at about 1.2 V, so it stays faintly on and the relay may never release.

The fix: split the supplies with JD-VCC

Optocoupler modules have a second header with three pins: JD-VCC, VCC and GND, and a jumper linking JD-VCC to VCC. Remove that jumper and the board has two separate supplies:

  • VCC (on the input header) powers only the optocoupler LED. Give it 3.3 V from the ESP32, so HIGH at 3.3 V fully turns the LED off.
  • JD-VCC powers the transistor and coil. Give it 5 V from the ESP32's VIN/5V pin (USB powered) or a separate 5 V supply.
The optocoupler isolates the input: VCC and IN drive only its LED, while JD-VCC powers the coil

Wiring: relay module (JD-VCC jumper removed) to ESP32

Module pinESP32 pinNote
VCC (input header)3V3Optocoupler LED only, so 3.3 V HIGH switches it fully off
INGPIO26Safe output pin: not a boot strapping pin
GND (input header)GNDCommon ground
JD-VCCVIN / 5VCoil supply: 5 V, about 75 mA
GND (JD-VCC header)GNDCoil ground. Use a separate 5 V supply's ground for full isolation

Tip

No JD-VCC header on your board? It has no optocoupler. Either use a relay module sold as "3 V" or "high/low trigger" (it switches cleanly from 3.3 V), or drive IN through a small NPN transistor. Don't power the whole module from 3.3 V: the coil won't pull in reliably.

Which ESP32 pins to use for a relay

PinsUse for a relay?Why
GPIO 25, 26, 27, 32, 33BestPlain outputs, quiet during boot
GPIO 4, 13, 14, 16–19, 21–23FineNormal outputs
GPIO 0, 2, 5, 12, 15AvoidBoot strapping pins: they can toggle at reset and click the relay
GPIO 1, 3AvoidUSB serial TX/RX
GPIO 6–11NeverConnected to the flash chip
GPIO 34–39NeverInput only

The sketch: control the relay from the Serial Monitor

Upload this sketch, open the Serial Monitor at 115200 baud and type on, off or toggle, then press Enter. Type pulse to switch the relay on for three seconds, which is handy for a door strike or a pump dose. The relay state is also printed every time it changes.

sketch.ino
// 5V relay module on ESP32 (JD-VCC wiring), controlled from Serial
const int RELAY_PIN = 26;
const bool ACTIVE_LOW = true;        // most modules: LOW = ON

bool relayIsOn = false;
unsigned long pulseUntil = 0;

void setRelay(bool on) {
  relayIsOn = on;
  digitalWrite(RELAY_PIN, (on ^ ACTIVE_LOW) ? HIGH : LOW);
  Serial.printf("Relay %s\n", on ? "ON" : "OFF");
}

void setup() {
  digitalWrite(RELAY_PIN, ACTIVE_LOW ? HIGH : LOW);  // OFF before enabling the pin
  pinMode(RELAY_PIN, OUTPUT);
  Serial.begin(115200);
  Serial.println("Type: on, off, toggle or pulse");
}

void loop() {
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('\n');
    cmd.trim();
    cmd.toLowerCase();
    if (cmd == "on")          setRelay(true);
    else if (cmd == "off")    setRelay(false);
    else if (cmd == "toggle") setRelay(!relayIsOn);
    else if (cmd == "pulse")  { setRelay(true); pulseUntil = millis() + 3000; }
  }

  // end a pulse without blocking the loop
  if (pulseUntil && millis() > pulseUntil) {
    pulseUntil = 0;
    setRelay(false);
  }
}

(on ^ ACTIVE_LOW) is a compact way to flip the level for active-LOW boards: with ACTIVE_LOW = true, ON writes LOW and OFF writes HIGH. Set it to false for an active-HIGH or "3 V" module.

The pulse uses millis() instead of delay(), so the ESP32 keeps reading commands while the relay is on. The same pattern is what you want once the relay is driven by Wi-Fi, MQTT or a sensor.

Troubleshooting

  • Relay turns on and never off

    Likely cause
    VCC still on 5 V with 3.3 V logic
    Fix
    Remove the JD-VCC jumper and power VCC from 3V3
  • Clicks during boot or upload

    Likely cause
    Relay on a strapping pin (0, 2, 5, 12, 15)
    Fix
    Move IN to GPIO26, 27, 32 or 33
  • LED lights, no click

    Likely cause
    No 5 V on JD-VCC
    Fix
    Connect JD-VCC to VIN/5V (USB powered) or a 5 V supply
  • ESP32 resets when the load switches

    Likely cause
    Load noise on the shared supply
    Fix
    Separate coil supply, short load wires, diode across inductive loads
  • Works backwards

    Likely cause
    Active-HIGH module or load on NC
    Fix
    Set ACTIVE_LOW = false, or move the load to NO

Where to go next

Keep learning

Related guides & projects