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.
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.
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 pin | ESP8266 pin | What it does |
|---|---|---|
| VCC | 5V (VU or VIN) | Powers the coil + onboard optocoupler. From the 5V rail, never from a GPIO |
| GND | GND | Common ground with the ESP8266 โ required for the input to trigger |
| IN | D1 (GPIO5) first | Signal input. Most modules are active-low โ LOW switches the relay ON |
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.
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 label | ESP8266 GPIO | Safe for relay? |
|---|---|---|
| D1 | GPIO5 | โ Safe โ first relay IN, my default choice |
| D2 | GPIO4 | โ Safe โ second relay IN |
| D5 | GPIO14 | โ Safe โ third relay IN |
| D6 | GPIO12 | โ Safe โ fourth relay IN |
| D7 | GPIO13 | โ Safe โ usable, but reserved for DMX data on event builds |
| D4 | GPIO2 | โ ๏ธ Boot-time quirk โ must be HIGH for a few ยตs at boot; avoid for relays unless you know the pull-up situation |
| D3 | GPIO0 | โ Flash mode pin โ avoid for general use |
| D8 | GPIO15 | โ 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.
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.
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:
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.
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.
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.
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)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.