Relays and Solid-State Relays for Makers: Switching AC and DC Loads Safely
Sooner or later, most microcontroller projects need to switch something a GPIO pin can't drive directly — a mains-voltage appliance, a heater cartridge, a solenoid, or a motor that draws more current than an ESP32 or Arduino pin can safely source. That's the job of a relay: a low-current control signal (3.3V or 5V logic from the microcontroller) switches a much larger current or voltage on the load side, with electrical isolation between the two. Choosing the wrong relay type, or wiring one without the protection components it needs, is a common source of both flaky projects (a microcontroller that resets every time a relay clicks) and genuinely dangerous ones (mains wiring without proper isolation). This guide covers the two relay families that cover almost all maker use — electromechanical and solid-state — and how to drive and protect them correctly.
Electromechanical Relays
An electromechanical relay (EMR) uses a coil to physically pull a set of contacts together. They're cheap, handle a wide range of voltages and current on the same part, and give a visible/audible click that makes it obvious when they've switched — genuinely useful for debugging. Downsides: they're relatively slow to switch (milliseconds), the contacts wear out over repeated cycling (rated typically for tens to hundreds of thousands of cycles), and the mechanical click means they're not appropriate for anything switching frequently or requiring silent operation.
Relay typeTypical useNotes Signal relay (small PCB-mount)Low-power signal switching, small DC loadsCoil current a GPIO pin still usually can't drive directly — needs a transistor driver Power relay (5V/12V coil, 10-30A contacts)Household appliances, pumps, solenoids, small motorsThe common "relay module" board sold for Arduino/ESP32 projects Automotive relay (Bosch-style, 12V/24V coil)12V DC loads — lighting, fans, pumps in vehicle or solar/battery projectsCheap, widely available, plug-in socket standard ContactorHigh-current 3-phase or heavy single-phase loads (large motors, big heaters)Essentially a heavy-duty relay; usually has its own dedicated safety and interlock requirementsSolid-State Relays
A solid-state relay (SSR) uses a triac or a pair of MOSFETs behind an optocoupler for isolation instead of a mechanical contact. They switch fast, have no moving parts to wear out, switch silently, and (AC models especially) can be set to switch at the AC zero-crossing point, which reduces electrical noise and inrush stress on the load — a meaningful advantage for anything switched frequently, like a PID-controlled heater in a reflow oven or 3D printer heated bed. Downsides: they have voltage drop across the switched contacts that shows up as heat (meaning SSRs need a heatsink for anything beyond a few amps), they fail more often in the "always on" state than an EMR does when they fail, and they generally cost more per amp of switching capacity.
- AC SSRs (e.g. the common 25A/40A screw-terminal modules) — the standard choice for PID-controlled heaters: reflow ovens, resin curing chambers, kilns, and similar. Zero-crossing switching keeps the noise down and avoids the mechanical clicking an EMR would produce on a fast PID cycle.
- DC SSRs — less common in maker projects than AC SSRs, since a logic-level MOSFET module often does the same job more cheaply for pure DC loads, but useful where full input/output isolation matters.
- Heatsinking — check the datasheet's voltage drop and derate accordingly; a 25A-rated SSR switching 10A continuously without a heatsink can still overheat depending on the part's forward drop and the enclosure's airflow.
Driving a Relay from a Microcontroller
Most "relay modules" sold for Arduino/ESP32 already include the transistor driver and flyback diode on the board, which is why they can be driven directly from a GPIO pin through a single signal wire plus ground. If wiring a bare relay coil directly (no module board), the coil needs its own driver circuit:
- A GPIO pin cannot supply the coil current an EMR needs directly (usually tens of mA) without exceeding the pin's rated output — use an NPN transistor or logic-level MOSFET as a low-side switch, with a base/gate resistor sized for the GPIO's drive current.
- Always include a flyback (freewheeling) diode across the relay coil, oriented to block current in the coil's normal energized direction. Without it, the coil's collapsing magnetic field generates a voltage spike when the relay de-energizes that can easily exceed the transistor's or the microcontroller's breakdown voltage — this is one of the most common causes of a microcontroller randomly resetting or a driver transistor failing after a period of otherwise-normal operation.
- Many relay modules also include an opto-isolator between the logic input and the driver transistor, which is worth seeking out for anything switching mains voltage, since it keeps a fault on the relay side from having any direct electrical path back to the microcontroller and USB-connected computer.
Safety: Switching Mains Voltage
Mains-voltage relay wiring is the point where a maker project stops being purely a low-voltage electronics exercise and becomes something that can genuinely injure or kill if done carelessly. A few rules that aren't optional:
- Keep mains-side wiring physically separated from low-voltage logic wiring inside the enclosure — most relay modules have a silkscreen line or slot separating the two sides for exactly this reason. Don't route mains wire over or near the microcontroller board.
- Use an enclosure with proper strain relief on the mains cord, and make sure the enclosure itself is rated for the environment (see the site's enclosure design guide for IP ratings if this will see moisture or dust).
- Fuse the mains side appropriately for the load, upstream of the relay, so a relay or load fault trips a fuse instead of overheating wiring.
- Never work on a mains-connected circuit while it's plugged in — verify de-energized with a meter, not by assumption, before touching any conductor.
- If the project will run unattended (a common case for PID-controlled heaters), add a hardware-level safety cutoff — a thermal fuse or a separate over-temperature relay that isn't dependent on the microcontroller's firmware working correctly — so a firmware hang or crash can't leave a heater latched on indefinitely.
- If you're not confident about mains wiring practices, have the mains-side portion of the build checked by someone who is before it runs unattended.
Relays and SSRs are what turn a microcontroller from something that blinks LEDs into something that actually runs machines — heaters, pumps, motors, and mains-powered equipment. The core rules are simple and don't change much project to project: match the relay type to the switching frequency and load, always protect the coil driver with a flyback diode, and treat any mains-voltage wiring with the isolation and fusing it demands rather than the casual approach that's fine for 5V logic.
Related Guides
- Solenoids and Linear Actuators for Makers: Push, Pull, Latching, and Driving Them Safely
- How to Program Addressable LED Strips: WS2812B Patterns, Effects, and Power Design
- I2C vs SPI vs UART: How to Choose and Use Serial Communication Protocols
- How to Use Sensors with Arduino and ESP32: Temperature, Distance, Load, Current, and Hall Effect
- How to Control Motors with Arduino and ESP32: Stepper, DC, and Servo Drivers
- ESP32: Setting Up for Arduino IDE
- Arduino vs ESP32: Which Should You Use? A Practical Comparison
- Getting Started with ESP32: GPIO, WiFi, and Your First Project