KACHANGยทSIA
SYSTEM OK
Home  /  Blog  /  ESP8266 Relay Module Guide

ESP8266 Relay Module โ€” Wiring & Programming Guide

A relay module is how an ESP8266 switches real lights โ€” 12V LED strips, uplights, even AC fixtures โ€” without ever touching the high-voltage side itself. This guide covers the four pins you need to wire, the active-low trap that confuses everyone, which GPIOs are actually safe, and the safety rules I follow on every build.

// HOW A RELAY MODULE WORKS

A tiny magnet switches a big load.

Inside a relay module there are two completely separate circuits. On one side, the coil โ€” a small electromagnet driven by your ESP8266's 3.3V logic. On the other side, the contacts โ€” COM (common), NO (normally open), and NC (normally closed) โ€” which sit in the path of your 12V or AC load. When the coil is energised, it pulls the contacts together and the load turns on. When it's not, the contacts spring back and the load turns off.

RELAY INTERNALS

Logic side and load side never touch

   Control side (3.3V logic)          Load side (12V / AC)
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚  ESP8266 GPIO โ”€โ”€โ–บ IN     โ”‚       โ”‚  12V+ โ”€โ”€โ–บ COM        โ”‚
   โ”‚                โ”Œโ”€โ”€โ”€โ”€โ”€โ”   โ”‚       โ”‚         โ”‚            โ”‚
   โ”‚  5V โ”€โ”€โ–บ VCC     โ”‚coilโ”‚   โ”‚       โ”‚   NO โ”€โ”€โ”€โ”ดโ”€โ”€โ–บ load +  โ”‚
   โ”‚  GND โ”€โ”€โ–บ GND    โ””โ”€โ”€โ”ฌโ”€โ”€โ”˜   โ”‚       โ”‚                    โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”˜       โ”‚   load โˆ’ โ”€โ”€โ–บ 12Vโˆ’   โ”‚
                          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ–บ contacts (electrically
                                 isolated from the coil)

That isolation is the whole point. The ESP8266 never carries load current โ€” it only drives a few milliamps of coil current, and the contacts do the heavy switching. This is what lets a $4 module safely switch lights that would destroy the ESP8266 if wired directly.

This matters for one practical reason: you can wire the control side without fear, because the dangerous voltage is physically separated. The fear should be reserved for the load side, which is where the fusing and isolation rules in the safety section come in.

// WIRING THE RELAY TO THE ESP8266

Four pins, two grounds.

Every single-channel relay module exposes the same four pins. Get these right and the rest of the build is just connecting the load.

VCC / GND / IN pin wiring

Relay pinESP8266 pinWhat it does
VCC5V (VU or VIN)Powers the coil + onboard optocoupler. From the 5V rail, never from a GPIO
GNDGNDCommon ground with the ESP8266 โ€” required for the input to trigger
IND1 (GPIO5) firstSignal input. Most modules are active-low โ€” LOW switches the relay ON
WIRING DIAGRAM

ESP8266 โ†’ 4-channel relay โ†’ 12V strip

   ESP8266 (NodeMCU)            4-channel relay module
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”             โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚ D1 (GPIO5)   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ IN1
   โ”‚ D2 (GPIO4)   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ IN2
   โ”‚ D5 (GPIO14)  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ IN3
   โ”‚ D6 (GPIO12)  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ IN4
   โ”‚ 5V (VU)      โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ VCC
   โ”‚ GND          โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ GND
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

   12V supply                 Each relay output:
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”               COM โ”€โ”€โ–บ 12V+
   โ”‚ +12V โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ–บ relay COM โ”‚
   โ”‚ GND โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ–บ strip โˆ’   NO โ”€โ”€โ–บ strip +
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜               (fuse the +12V line!)

On the load side: supply + โ†’ fuse โ†’ relay COM, relay NO โ†’ strip +, strip โˆ’ โ†’ supply โˆ’. Tie the supply GND back to the ESP8266 GND so both sides share a reference โ€” the input trigger won't work reliably without it.

Active-low vs active-high trigger (the trap)

Here is the #1 source of "my relay does nothing" confusion. Most cheap relay modules are active-low. That means the input is held HIGH internally (usually pulled up through an optocoupler LED), and the relay switches on when you pull the input to GND โ€” not when you send it HIGH.

Test it before you trust it: flash the blink sketch with your relay pin swapped in for the built-in LED. If the relay clicks when the GPIO goes LOW, your module is active-low (most are). If it clicks when the pin goes HIGH, it's active-high. Code the logic to match โ€” this one test saves an hour of "why isn't it working".

Some modules have a small jumper (often labelled JD-VCC or LOW / HIGH) that flips the trigger logic. If yours has one, decide the mode you want and set it before wiring. The silkscreen usually says which state the module is in.

// WHICH GPIO PINS TO USE

Four safe pins, two to avoid.

Not every GPIO on an ESP8266 is safe to drive a relay. Some pins are involved in booting the chip and will glitch โ€” or stop the board from booting at all โ€” if you hang a relay input on them. This is the table I keep on my bench:

NodeMCU labelESP8266 GPIOSafe for relay?
D1GPIO5โœ… Safe โ€” first relay IN, my default choice
D2GPIO4โœ… Safe โ€” second relay IN
D5GPIO14โœ… Safe โ€” third relay IN
D6GPIO12โœ… Safe โ€” fourth relay IN
D7GPIO13โœ… Safe โ€” usable, but reserved for DMX data on event builds
D4GPIO2โš ๏ธ Boot-time quirk โ€” must be HIGH for a few ยตs at boot; avoid for relays unless you know the pull-up situation
D3GPIO0โŒ Flash mode pin โ€” avoid for general use
D8GPIO15โŒ Must be LOW at boot โ€” conflicts with relay modules that pull inputs HIGH

For a 4-zone build, D1, D2, D5 and D6 give you four clean relay channels with zero boot weirdness. That's the exact pinout I use on event controllers.

// POWERING THE RELAY COIL

5V rail, not GPIO โ€” and a 100ยตF cap if it flickers.

Here's the current math, honestly: a single relay coil draws roughly 70 mA when energised, and a 4-channel module can pull 250โ€“300 mA with all channels on. An ESP8266 GPIO can safely source about 12 mA. Powering the coil from a GPIO isn't just marginal โ€” it's not going to work, and it can damage the pin over time.

  • Power the module from the 5V rail โ€” the VU pin (USB 5V) or VIN pin (5V regulated input) on a NodeMCU. Both are rated for far more current than the coil needs.
  • Add a 100 ยตF electrolytic capacitor across the relay module's VCC and GND terminals if the relay chatters or flickers when switching. The coil's inrush current makes the rail sag for a few milliseconds; the cap smooths that out. This is a real fix, not a myth โ€” I've used it on builds that flickered on cheap USB supplies.
  • If your USB supply is weak (< 500 mA), use a separate 5V supply for the relay module instead of the ESP8266's USB line โ€” common ground still ties them together.

In my experience, the capacitor fix is needed more often on NodeMCU boards powered from laptop USB ports than on wall chargers. If the relay clicks once and then does nothing, measure the rail โ€” a sagging 5V line is usually the culprit, and the cap fixes it.

// SAFETY RULES

Fusing, isolation, and no AC on the breadboard.

I'm an electronics builder, not a licensed electrician โ€” and this section is where that boundary lives. These rules aren't optional for me, and they shouldn't be for you:

1. No AC on the breadboard

Mains voltage never goes anywhere near a breadboard, jumper wires, or the ESP8266. If you're switching AC, put the relay module in an insulated enclosure with screw terminals for the mains side.

2. Fuse the load line

An inline fuse rated just above your load current goes on the supply + line before the relay. A shorted strip otherwise becomes a fire risk before the relay or supply can protect itself.

3. Keep load wiring separate

12V or AC wiring should not run alongside or twisted with 3.3V logic wires. Separate them in the enclosure so a damaged load wire can't couple into the logic.

4. No touch when powered

Never touch the relay's switched terminals while the load supply is on โ€” even 12V can bite in the wrong conditions, and AC absolutely will. Disconnect power before re-wiring anything.

For permanent house wiring โ€” anything inside walls, or a fixed mains circuit โ€” hire a licensed electrician. A relay module is a project component, not a certified safety device.

This is exactly the wiring the ESP8266 Event Lighting Control pack walks you through, with diagrams for 4 zones โ€” built from real event builds, not a textbook.

๐Ÿ‘‰ See the ESP8266 Event Lighting Pack ($19)
// FAQ

Quick answers before you wire.

Why does my relay module do nothing when the GPIO goes HIGH?

Because most cheap relay modules are active-low: the input is internally pulled HIGH and the relay switches on when you pull it to GND. Sending HIGH actually switches it off (or does nothing visible). Write the GPIO LOW to trigger, or flip the module's logic jumper if it has one.

Can the ESP8266 power the relay coil directly from a GPIO?

No. A relay coil draws roughly 70 mA per channel, and an ESP8266 GPIO can only source about 12 mA. Power the module from the 5V rail (VU or VIN) and drive only the IN signal from the GPIO. On a 4-channel module, add a 100 ยตF capacitor across VCC/GND if it flickers.

Can I switch 240V AC lights with an ESP8266 relay module?

Yes, the contacts are rated for mains โ€” but treat it as a project, not a certified appliance. Keep mains on the switched side only, use an inline fuse, never bring mains near a breadboard or 3.3V logic, and leave permanent house wiring to a licensed electrician.

Should I buy a 1-channel or 4-channel relay module?

A 4-channel module costs only a few dollars more, uses the same VCC/GND wiring, and gives you four independently switchable zones. For event lighting you'll almost always want more than one zone โ€” start with the 4-channel version and you won't have to re-buy later.

// ABOUT THE AUTHOR

Who wrote this guide.

๐Ÿ‘ท

About the author: Jack Loh is an electronics & coding builder based in Singapore. He builds ESP8266 controllers for real event jobs โ€” weddings, parties, exhibitions โ€” and sells the firmware and wiring guides from his own builds.

The wiring above is the same relay setup he runs at events โ€” the active-low trap and the capacitor fix are both things he's hit on real jobs, not theory.