Electronics for Building Things · Lesson 7

Relays & Switching Mains Safely

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 first Mains 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

For a lamp you want on-demand: wire it through COM → NO, so a dead or unprogrammed board leaves the lamp off.

Mains safety rules — non-negotiable

These come straight from experienced makers who teach this (DroneBotWorkshop: Control AC Devices Safely):

The safety checklist

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.

Check yourself

Recall first — the safety answers especially.

Read this next (primary sources) Random Nerd Tutorials — Relay Module Guide for the wiring, and DroneBotWorkshop — Control AC Devices Safely for the safety practices. Read both before going near mains.
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.

See also: Glossary · Rules of thumb