Back to Blog
Blogs 8 min read

How to Use Relay Module with ESP32: A Safe and Reliable Wiring Guide

Learn how to connect an ESP32 to 5V relay modules. This guide explains logic‑level mismatches, how optocoupler isolation works, how to choose safe GPIO, and code that avoids switching glitches when the system starts.

Himanshu Garg
Himanshu Garg November 21, 2026 1 views
how to use relay module with esp32 - How to Use Relay Module with ESP32: A Safe and Reliable Wiring Guide

Learn how to connect an ESP32 to 5V relay modules. This guide explains logic‑level mismatches, how optocoupler isolation works, how to choose safe GPIO, and code that avoids switching glitches when the system starts.

Quick answer

To work with how to use relay module with ESP32 boards safely, first fix the logic‑level mismatch. The ESP32 runs at 3.3V, but most standard electromagnetic relay modules need a 5V supply to pull in the coil. Hook the relay module's VCC to the ESP32's 5V (Vin) pin, connect GND to GND, and attach the input control pin (IN) to a clean ESP32 GPIO, like GPIO22. If you want full electrical isolation, take off the JD-VCC jumper and feed the relay coil from an external 5V source.

The 3.3V vs. 5V Logic Barrier in Relay Control

Connecting a standard 5V electromagnetic relay module to an ESP32 creates a basic electrical problem. The ESP32 works with 3.3V logic, but most relay modules expect 5V systems. A typical ESP32 General Purpose Input/Output (GPIO) pin can only output up to 3.3V when it is high. That voltage may not be enough to reliably turn on the optocoupler or transistor in a 5V relay module. Also, you cannot drive a relay coil directly from a microcontroller pin because a usual ESP32 GPIO can safely source or sink only 12mA to 20mA. In contrast, even a small 5V relay coil needs 70mA to 100mA to pull in.

Modern relay modules solve this by including a transistor or optocoupler that switches the coil current. Still, you must set up the connection correctly so you don’t damage the microcontroller.

For a closely related example, see How to Connect ESP32 to WiFi Using Arduino IDE.

Electromagnetic Relays (EMR) vs. Solid State Relays (SSR) for ESP32

Choosing between mechanical and solid-state switching depends on your load type, switching frequency, and safety requirements.

Factor Engineering view Why it matters
Switching Speed EMR: Slow (5ms to 15ms) | SSR: Fast (microseconds) SSRs are ideal for PWM or high-frequency switching.
Acoustic Noise EMR: Audible clicking sound | SSR: Silent operation Audible feedback is useful for diagnostics but poor for quiet environments.
Contact Wear EMR: Physical wear, arcing occurs | SSR: No moving parts, infinite cycles Inductive loads shorten EMR lifespan significantly without snubbers.
Electrical Isolation EMR: Excellent (physical air gap) | SSR: Optical isolation (built-in optocoupler) EMRs provide physical air-gap isolation when turned off.
Heat Dissipation EMR: Minimal heat generated | SSR: Generates heat under high loads SSRs require heatsinks when switching currents above 5A to 10A.

Understanding the JD-VCC Jumper and Optocoupler Isolation

Most multi‑channel relay modules have a physical jumper labeled JD-VCC. This jumper is what gives real galvanic isolation between the noisy high‑current relay coil circuit and the sensitive 3.3V ESP32 silicon. When the JD-VCC jumper is installed, the VCC pin powers both the optocoupler photodiode and the relay coil from the same supply. If you hook this directly to the ESP32's 3.3V rail, the relay coil will pull the rail down, causing the ESP32 to brown out or reset. To use these modules safely, take out the JD-VCC jumper. This separates the two circuits.

Connect the VCC pin to the ESP32's 3.3V pin to power the internal optocoupler LED. Then connect the JD-VCC pin to an external 5V power supply (or to the ESP32 Vin pin if you are using a clean 5V USB source) to drive the coil. Keep the ground lines separate to keep full optical isolation.

For a closely related example, see ESP32 MQTT Tutorial for Beginners: Build a Reliable IoT Node.

Selecting Safe ESP32 GPIO Pins

Not all ESP32 GPIO pins act the same during boot. Some pins are called strapping pins; they briefly send PWM signals, pull high, or pull low when the ESP32 starts up or resets. If you attach a relay to those pins, the relay will click or chatter quickly during boot, which can unintentionally switch connected AC appliances and be unsafe. The pins GPIO0, GPIO2, GPIO12, and GPIO15 are strapping pins, so don’t use them for relay control. Pins GPIO34 through GPIO39 are input‑only, so they cannot power any output device. The safest choices for relay control are GPIO4, GPIO16, GPIO17, GPIO18, GPIO19, GPIO21, GPIO22, and GPIO23.

These pins stay in a high‑impedance state while booting, so the relay stays in its default position until your code takes over.

For a closely related example, see How to Build a Reliable Smart Irrigation System Using ESP32.

Diagnosing ESP32 Relay Switching Issues

Follow this diagnostic path if your relay fails to switch or causes the ESP32 to behave erratically.

  1. 1ESP32 resets when relay switches
    Remove JD-VCC jumper and use an external 5V supply for the coil.

    Indicates the relay coil is pulling down the ESP32's 3.3V power rail.

  2. 2Relay LED lights up but contacts do not click
    Verify the JD-VCC pin is receiving stable 5V power.

    The optocoupler is working (LED is on), but the coil lacks sufficient voltage to close the contacts.

  3. 3Relay triggers randomly on controller boot
    Move the control wire to a safe GPIO like GPIO22 or GPIO23.

    You are likely using a strapping pin (e.g., GPIO0, GPIO2, GPIO15) that glitches during boot.

  4. 4Relay state is inverted from code logic
    Adjust code logic: write LOW for ON, and HIGH for OFF.

    Most hobbyist relay modules use active-low logic inputs.

Safe Wiring Schematic and Connections

To connect a 5V active‑low optoisolated relay module to an ESP32 while keeping full electrical isolation, follow these steps. First, take off the JD-VCC jumper on the relay module. Then, wire the ESP32 3.3V pin to the relay module’s VCC pin. This makes sure the optocoupler’s input logic matches the 3.3V level of the ESP32. Next, attach a safe GPIO, for example GPIO22, to the relay module’s IN1 pin. Connect the positive lead of an external, stable 5V power supply straight to the JD-VCC pin on the relay module.

Connect the negative lead of that external 5V supply to the GND pin that sits next to the JD-VCC pin. Finally, do not tie the external 5V ground to the ESP32 ground, or you will lose the complete optical isolation between the high‑current coil circuit and the microcontroller.

Writing Glitch-Free Control Firmware

Many common relay modules are active-low, meaning they turn on when the control pin is pulled to ground (LOW) and turn off when the pin is pulled to VCC (HIGH). If your code simply declares the pin as an output, the pin defaults to LOW initially, which can cause the relay to turn on briefly before your code sets it high. To prevent this initialization glitch, you must write the default idle state (HIGH) to the output latch before configuring the pin mode. The following C++ code demonstrates how to implement this sequence in the Arduino IDE framework:

#define RELAY_PIN 22
void setup() {
  // Set the pin state to HIGH (OFF for active-low relays) before enabling output mode
  digitalWrite(RELAY_PIN, HIGH); 
  pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
  // Turn the relay ON by pulling the pin LOW
  digitalWrite(RELAY_PIN, LOW);
  delay(5000);

  // Turn the relay OFF by pulling the pin HIGH
  digitalWrite(RELAY_PIN, HIGH);
  delay(5000);
}

This simple ordering in your setup function prevents the relay from clicking during the microsecond phase when the microcontroller boots up and configures its registers.

High-Voltage Safety and Snubber Circuits

When using a relay to switch high-voltage mains AC loads (110V/220V), physical safety is paramount. Ensure that the PCB traces on the high-voltage side of your relay module have adequate creepage and clearance distances, ideally at least 6mm of isolation gap from the low-voltage logic side. If you are switching inductive loads, such as electric motors, water pumps, or solenoids, the sudden collapse of the magnetic field when switching off creates a high-voltage back-EMF spike. This spike can cause arcing across the physical relay contacts, degrading them rapidly and emitting electromagnetic interference (EMI) that can freeze or reset your ESP32.

To mitigate this, connect a resistor-capacitor (RC) snubber network in parallel with the relay contacts to absorb the energy spike, or switch to a zero-crossing Solid State Relay (SSR) for high-frequency AC switching.

Key takeaways

  • The ESP32 operates at 3.3V, meaning GPIO pins cannot safely supply the current or voltage needed to power a 5V relay coil directly.
  • Removing the JD-VCC jumper enables true galvanic isolation, preventing relay coil power surges from resetting or damaging the ESP32.
  • Avoid using strapping pins like GPIO0, GPIO2, and GPIO15 to prevent dangerous relay chattering during the microcontroller boot phase.
  • Always declare the default idle state of your control pin before initializing it as an output to eliminate startup switching glitches.
  • Inductive loads such as motors and solenoids require RC snubber networks to protect mechanical relay contacts from high-voltage back-EMF arcing.

Questions engineers often ask

Can I power a 5V relay directly from the ESP32 3.3V pin?

No. A 5V electromagnetic relay coil requires 5V to reliably energise and pull in the contact arm. Attempting to run it on 3.3V will either cause the relay to fail to switch or pull excessive current from the ESP32's internal voltage regulator, leading to thermal shutdown, brownouts, or permanent damage to the board.

What is the purpose of the JD-VCC jumper on relay modules?

The JD-VCC jumper connects the relay coil power supply to the optocoupler signal power supply. Removing the jumper allows you to run the input logic side at 3.3V (matching the ESP32 logic level) while running the power-hungry relay coils at 5V from an external supply, ensuring complete electrical isolation.

Why does my relay click on and off rapidly when I power on the ESP32?

This occurs because the relay is connected to an ESP32 strapping pin (such as GPIO0, GPIO2, GPIO12, or GPIO15). These pins output state-change signals or PWM pulses during the boot sequence. Moving your control wire to a safe pin like GPIO22 resolves this behavior entirely.

Do I need a flyback diode when using an optoisolated relay module?

Most pre-assembled commercial relay modules already include an onboard flyback diode connected in parallel with the relay coil to suppress voltage spikes. However, if you are building a custom relay driver circuit from scratch using discrete components, you must include a diode (such as a 1N4007) to protect your switching transistor.

Continue learning: how to use relay module with esp32

Explore more embedded hardware integration guides on Fried Engineers.

Chat with Fried Engineers