The home-automation payoff — and the lesson where safety comes first.
This is what we've been building toward: making your ESP32 switch a real household device —
a lamp, a fan, a heater. The hero is the relay. But this lesson carries a
warning the others didn't, because for the first time we're near voltages that can injure or
kill. Read the safety section as carefully as the technical one.
Read this firstMains voltage (120V/230V AC) can kill you and can start fires. Do not wire mains
until you understand this lesson and are confident. If you're unsure at any point, stop and ask —
or use a commercial smart plug (which is a relay in a safe, certified box). There is no shame in
that; it's the same circuit, professionally enclosed.
What a relay is
A relay is an electrically-controlled switch with a wall between the two sides. A
small current energizes a coil, the coil's magnetism physically pulls a metal switch closed, and that
switch is a completely separate circuit — it can carry mains power your microcontroller never
touches
(Random Nerd Tutorials: Relay guide).
The key idea: isolation
The control side (your 3.3V/5V signal) and the switched side (mains) are isolated —
physically separate, no electrical connection. That barrier is what lets a tiny safe signal command a
dangerous load. Protecting that barrier is the whole safety game.
Use a relay module, not a bare relay
A bare relay coil is an inductive load — it needs the transistor and flyback diode from
Lesson 6 just to drive it. A relay
module (a little board) bundles all of that for you: the driver transistor, the flyback diode,
often an optocoupler for extra isolation, and screw terminals for the load.
You feed it power, ground, and one control pin from the ESP32.
Rule of thumb
As a beginner, always use a relay module, and check its contact rating
(printed on the relay, e.g. "10A 250VAC") comfortably exceeds your load. The module handles the
Lesson-6 details so you can focus on wiring safely.
The three contact terminals: COM, NO, NC
COM (common) — the pole the switch moves to and from.
NO (normally open) — disconnected until the relay activates. Use this so your
device is off by default (the safe choice).
NC (normally closed) — connected until the relay activates.
For a lamp you want on-demand: wire it through COM → NO, so a dead or unprogrammed
board leaves the lamp off.
Power off and unplugged before touching any mains wiring. Always.
Switch the hot/live wire only — never the neutral or ground.
Relay rating must exceed the load (voltage and current), with margin.
Everything in an enclosure. No exposed mains conductors — ever. Use proper
strain relief and the right wire gauge.
No breadboards for mains. Breadboards are for the low-voltage side only;
mains connections must be screw terminals or soldered and insulated.
When in doubt, stop. Use a certified smart plug, or consult a licensed
electrician. Mains is the one place "let's just try it" is the wrong instinct.
The whole picture
Putting the course together: a sensor or button (Lesson 5)
tells your ESP32 something; your code decides; a pin drives the relay module's control input; the module
safely switches the mains device — with isolation, a flyback diode, and a rated contact doing the
dangerous work behind a barrier. That's a home-automation gadget.
I'm your teacher — ask me anything, and please do before your first mains build.
Walk me through your plan and I'll help you check ratings, wiring, and whether a smart plug is the
smarter call. This is the lesson to over-ask on.