Component Guide

Power and Switching Beginner

1-Channel Relay Module

Electrically isolated switch module that lets an ESP32 control higher-power DC loads and carefully supervised AC loads from a low-current GPIO signal.

BeginnerDifficulty 14 minReading time 20-35 minBench time ESP32Compatible
Share
1-Channel Relay Module

Plain-English Overview

A relay module is a remote-controlled switch. Your ESP32 sends a small signal to the relay input, and the relay clicks to open or close a separate circuit.

That separate circuit can control a larger load than an ESP32 pin can handle, such as a small pump, fan, lamp, or solenoid. The ESP32 does not power the load directly; it only tells the relay when to switch.

Start with low-voltage DC loads. Mains AC wiring can injure or kill people and should only be handled by qualified adults using proper enclosures, fuses, strain relief, and local electrical rules.

Where You Use It

  • Low-voltage pump control
  • Fan switching
  • Lamp automation demo
  • Irrigation valve control
  • Alarm siren control
  • Classroom switch experiment
  • Power strip prototype
  • Heater control with safety supervision

Quick Facts

  • Function Electrically controlled switch
  • Input behavior Often active LOW
  • Contacts COM, NO, NC
  • Isolation Often optocoupler/transistor driven
  • Best first load Low-voltage DC fan or LED strip
  • GPIO safety Use module input, not bare coil

How It Works

A relay contains an electromagnet and a mechanical switch. When the module energizes the coil, the switch moves with a click. COM connects either to NC or NO depending on whether the relay is off or on.

NO means normally open: the load is off until the relay activates. NC means normally closed: the load is on until the relay activates. COM is the moving common terminal.

The ESP32 GPIO does not power the relay coil directly. A relay module usually includes a transistor driver, diode, indicator LED, and sometimes optocoupler isolation. The GPIO only controls the input side.

Many modules are active LOW because of the input circuit design. That means digitalWrite(RELAY_PIN, LOW) turns the relay on and HIGH turns it off. Production code should define RELAY_ON and RELAY_OFF constants so the logic is obvious.

Technical Specifications

Arduino library: Built-in Arduino digitalWrite

SpecificationValueWhy it matters
Module supply Often 5 V Most relay coils need more current than the ESP32 3.3 V rail should provide.
Input signal 3.3 V may work on many modules; verify yours Some 5 V relay modules do not trigger reliably from 3.3 V logic.
Contact rating Often printed as 10 A 250 VAC or 10 A 30 VDC The printed maximum is not a beginner design target.
Isolation Module dependent Optocoupler labels do not guarantee safe mains layout.
Switch type SPDT: COM, NO, NC One moving contact can connect to either of two terminals.
Response Mechanical milliseconds Fine for pumps/lights, not for fast PWM.
Coil current Often 60-90 mA Too much for direct GPIO drive.
Load type Resistive loads easiest; inductive loads need protection Motors and solenoids create voltage spikes.
Default state Off when input inactive if wired to NO Important for fail-safe design.
Safety Use enclosure and fuse for hazardous voltage Relay contacts can expose dangerous voltage.

Pinout

  • VCC Module power 5 V supply Powers relay coil and input circuit on common modules.
  • GND Ground reference ESP32 GND Needed unless using true isolated wiring per module documentation.
  • IN Control input ESP32 GPIO26 Often active LOW; test before connecting real loads.
  • COM Common switch terminal Load power input Moving contact of the relay switch.
  • NO Normally open terminal Load switched output Use when load should be off by default.
  • NC Normally closed terminal Optional always-on path Use only when you understand fail-safe behavior.

Wiring Diagram

The control side connects to the ESP32. The contact side connects in series with the load power circuit. These two sides are different jobs.

For a beginner test, switch a low-voltage LED strip, buzzer, or small fan. Do not put mains voltage on a breadboard.

Wiring Diagram Relay module VCC to 5 V, GND to ESP32 GND, IN to GPIO26, load wired through COM and NO on low-voltage supply
  1. 1

    Unplug all power before wiring.

  2. 2

    Connect relay VCC to a suitable 5 V supply.

  3. 3

    Connect relay GND to ESP32 GND.

  4. 4

    Connect relay IN to ESP32 GPIO26.

  5. 5

    For a low-voltage load, connect supply positive to COM.

  6. 6

    Connect NO to the load positive wire.

  7. 7

    Connect the load negative wire back to the load supply negative.

  8. 8

    Upload test code before connecting valuable equipment.

Code Examples

Use the same wiring with Arduino IDE, PlatformIO, or ESP-IDF. Start with Arduino, then graduate when you need a larger project structure.

relay_esp32_safe_toggle.ino
#define RELAY_PIN 26

const int RELAY_ON = LOW;
const int RELAY_OFF = HIGH;

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, RELAY_OFF);
  Serial.println("Relay module test. Load should start OFF.");
}

void loop() {
  Serial.println("Relay ON");
  digitalWrite(RELAY_PIN, RELAY_ON);
  delay(2000);

  Serial.println("Relay OFF");
  digitalWrite(RELAY_PIN, RELAY_OFF);
  delay(2000);
}

The constants RELAY_ON and RELAY_OFF make active-LOW behavior explicit. setup() starts with the relay off before configuring normal operation.

loop() toggles the relay slowly so you can hear the click and measure the contact state. Do not use relay modules for rapid PWM; mechanical contacts wear out.

Expected Output

The relay clicks on for two seconds, then clicks off for two seconds. Serial Monitor prints Relay ON and Relay OFF. If the LED on the relay module changes but your load does not, the contact-side wiring is wrong or the load supply is missing.

Common Mistakes

  • Switching mains voltage on a breadboard.
  • Assuming HIGH always means relay on.
  • Connecting a motor directly to ESP32 instead of through contacts/driver.
  • Forgetting common ground on the control side.
  • Using the NC terminal when NO was intended.
  • Powering a 5 V relay coil from the ESP32 3V3 pin.
  • Rapidly toggling a mechanical relay.
  • Ignoring flyback protection for inductive DC loads.
  • Confusing module input pins with contact terminals.
  • Testing with dangerous loads before proving code on low voltage.

Troubleshooting

ProblemPossible causeSolution
Relay does not click No 5 V coil power, wrong GPIO, or input logic mismatch. Check VCC/GND, try active LOW, and measure IN voltage.
Relay clicks but load stays off COM/NO/NC wiring or load supply is wrong. Wire load supply through COM and NO for off-by-default behavior.
Relay is on at boot Active LOW input floats or boot state triggers module. Use pull-up, safe boot pin, or external driver logic.
ESP32 resets when relay switches Power dip or coil/load noise. Use separate relay supply, common ground, and proper suppression.
Module LED turns on but no click Insufficient coil voltage/current. Use rated 5 V supply for the relay module.
GPIO gets hot or damaged Bare relay coil connected directly. Use a relay module or transistor driver with flyback diode.
Load turns on opposite of code NC used instead of NO or active LOW logic. Move wire to NO or invert constants.
Motor causes false resets Inductive noise from load. Add flyback diode/snubber and separate power wiring.
Relay chatters Weak supply or noisy input. Use stable supply and avoid floating control pin.
No isolation despite optocoupler JD-VCC jumper/wiring not configured for isolation. Follow your module schematic; do not assume isolation.
Mains wiring unsafe Exposed contacts or no enclosure. Stop and use proper rated enclosure, fuse, and qualified supervision.
Relay too slow for dimming Mechanical contacts are not PWM devices. Use MOSFET or solid-state driver for fast switching.
Contacts wear out Switching high current or frequent cycles. Use rated relay and reduce switching frequency.
ESP32 cannot trigger 5 V module Input threshold too high. Use transistor driver or 3.3 V compatible relay module.
Load always on Wired through NC. Use NO for normally-off projects.

Related Guides

Related Projects

FAQ

Review, Testing, and References

Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-07-05. Reviewed: wiring, code, beginner safety, and ESP32 compatibility. Educational level: Beginner.

Use this component page as an educational starting point. Check official documentation before using the part in production, high-current, outdoor, battery, or safety-critical hardware.

Downloads

Relay modules vary by board. Check the exact module markings, schematic, coil voltage, input trigger type, and contact rating before connecting real loads.

No separate datasheet is needed for this beginner guide.