Overview & Types
Relays are electrically-operated switches that use a small control signal to switch much larger loads. They provide galvanic isolation between the control circuit (microcontroller) and the power circuit (motors, heaters, mains voltage), making them essential safety components in any embedded system that controls high-power devices.
Types
- Electromagnetic Relay (EMR): Traditional mechanical relay with a coil, armature, and metal contacts. Audible click, handles AC or DC, withstands surges. Typical: SRD-05VDC-SL-C (5 V coil, 10 A contacts).
- Solid State Relay (SSR): Uses semiconductor switching (TRIAC for AC, MOSFET for DC). No moving parts → silent, no contact bounce, millions of cycles. Typical: SSR-40DA (40 A, 3–32 V control, 24–380 V AC load).
- Reed Relay: Glass-enclosed reed contacts activated by an external coil or permanent magnet. Very small, fast switching, low contact current (0.5–1 A). Used in test equipment and signal routing.
- Contactor: Heavy-duty relay for high-power switching (10–1000+ A). Used in motor starters, EV battery disconnects, and industrial power distribution.
- Latching Relay: Maintains contact position after the control signal is removed. A pulse sets, a reverse pulse resets. Zero holding power — ideal for battery-powered applications.
Working Principle
Electromagnetic Relay
- Coil Energized: Current through the coil creates a magnetic field that attracts the armature (pivoting metal arm).
- Contact Closure: The armature moves, pressing the NO (Normally Open) contacts together and separating the NC (Normally Closed) contacts.
- Load Circuit Complete: Current flows through the closed contacts to the load.
- Coil De-energized: Spring returns the armature, NO contacts open, NC contacts close.
Solid State Relay
- Control Signal: A small DC voltage (3–32 V) activates an internal LED inside an optocoupler.
- Optical Isolation: The LED light triggers a photodetector, providing galvanic isolation (typically 4 kV).
- Load Switching: The photodetector drives a TRIAC (for AC) or MOSFET (for DC) that connects the load circuit.
- Zero-Cross Switching: AC SSRs typically switch at the zero-crossing point of the AC waveform, minimizing electromagnetic interference.
Contact Configurations
| Type | Abbreviation | Description | Use Case |
|---|---|---|---|
| SPST-NO | Form A | 1 pole, normally open | Simple on/off switching |
| SPST-NC | Form B | 1 pole, normally closed | Fail-safe circuits |
| SPDT | Form C | 1 pole, NO + NC (changeover) | Most common; select between 2 circuits |
| DPDT | 2 × Form C | 2 independent SPDT | Motor direction reversal, dual-circuit switching |
Electrical & Mechanical Specifications
| Parameter | EMR (5V module) | SSR (AC) | SSR (DC) | Contactor |
|---|---|---|---|---|
| Coil/Control Voltage | 5 V DC | 3–32 V DC | 3–32 V DC | 24–240 V AC/DC |
| Coil Current | 70–90 mA | 7–20 mA | 7–20 mA | 50–500 mA |
| Contact Rating | 10 A @ 250 VAC | 10–100 A @ 380 VAC | 10–60 A @ 60 VDC | 9–1000 A |
| Switching Speed | 5–20 ms | <1 ms (at zero-cross) | <1 ms | 30–100 ms |
| Isolation | 1.5–5 kV | 2.5–4 kV | 2.5–4 kV | 2–5 kV |
| Contact Bounce | 1–5 ms | None | None | 5–20 ms |
| Lifespan (cycles) | 100K mechanical / 10K electrical | Unlimited (no contacts) | Unlimited | 1M mechanical |
| Quiescent Power | 350–450 mW | ~50 mW | ~50 mW | 5–50 W |
Driver Circuits
Relay Module (Plug-and-Play)
Most hobby relay modules include the driver transistor, flyback diode, LED indicator, and screw terminals. Just connect VCC (5 V), GND, and signal pin (active LOW on most modules).
Discrete Driver (NPN + Flyback)
For bare relays: NPN transistor (2N2222 or BC547) or N-channel MOSFET, base/gate resistor (1k–10k), and flyback diode across the coil.
SSR Wiring
SSRs have 4 terminals: control + and − (DC input), load terminals (AC/DC output). Wire in series with the load. No flyback diode needed.
Heat Sinking for SSRs
SSRs have a voltage drop across the switching element (1–1.6 V for TRIAC, 0.1–0.5 V for MOSFET). Power dissipation = Vdrop × Iload. At high currents, this requires a heat sink. A 40 A SSR at 30 A load dissipates ~48 W!
Control Methods
Direct GPIO Drive (Relay Module)
Most relay modules are active LOW — connect control pin directly to GPIO. Pull-up during boot to prevent relay toggling during microcontroller reset.
PWM Dimming (SSR + AC)
Zero-cross SSRs can implement phase-angle control for AC dimming (heaters, incandescent lights). Cycle-skipping at low frequencies controls average power.
Multi-Relay Sequencing
When switching multiple relays, stagger activation to avoid simultaneous inrush current. Add 50–100 ms delays between relay activations.
Watchdog / Fail-Safe
For critical applications, use NC contacts so the load is energized by default and the relay de-energizes (opens) only when the controller is active. If the controller crashes, the relay returns to the safe (NC) state.
Code Example — Arduino & ESP32
Arduino: 4-Channel Relay Control
// 4-channel relay module control on Arduino
// Wiring: Relay IN1-IN4 → D4-D7 (active LOW), VCC→5V, GND→GND
const int RELAY_PINS[] = {4, 5, 6, 7};
const int NUM_RELAYS = 4;
const char* labels[] = {"Light", "Fan", "Heater", "Pump"};
void setup() {
Serial.begin(9600);
for (int i = 0; i < NUM_RELAYS; i++) {
pinMode(RELAY_PINS[i], OUTPUT);
digitalWrite(RELAY_PINS[i], HIGH); // HIGH = OFF (active LOW)
}
Serial.println("Relay Controller Ready");
Serial.println("Send: 1-4 to toggle relay");
}
void setRelay(int index, bool on) {
if (index < 0 || index >= NUM_RELAYS) return;
// Active LOW: LOW = ON, HIGH = OFF
digitalWrite(RELAY_PINS[index], on ? LOW : HIGH);
Serial.print(labels[index]);
Serial.println(on ? ": ON" : ": OFF");
}
void loop() {
if (Serial.available()) {
int cmd = Serial.parseInt();
if (cmd >= 1 && cmd <= 4) {
int idx = cmd - 1;
// Toggle current state
bool currentState = (digitalRead(RELAY_PINS[idx]) == LOW);
setRelay(idx, !currentState);
}
}
}
ESP32: SSR with Temperature-Based Control
// ESP32 controlling SSR for heater with temperature feedback
// Wiring: SSR control→GPIO25, Thermistor→GPIO34(ADC)
#include <Arduino.h>
#define SSR_PIN 25
#define THERM_PIN 34
#define TARGET_TEMP 60.0 // Target temperature in Celsius
#define HYSTERESIS 2.0 // ±2°C hysteresis band
// Thermistor parameters (NTC 10k, B=3950)
const float SERIES_R = 10000.0;
const float NOMINAL_R = 10000.0;
const float NOMINAL_T = 25.0;
const float B_COEFF = 3950.0;
void setup() {
Serial.begin(115200);
pinMode(SSR_PIN, OUTPUT);
digitalWrite(SSR_PIN, LOW); // Heater off
analogReadResolution(12);
Serial.println("SSR Heater Controller");
Serial.printf("Target: %.1f°C ± %.1f°C\n",
TARGET_TEMP, HYSTERESIS);
}
float readTemperature() {
int raw = analogRead(THERM_PIN);
float resistance = SERIES_R / ((4095.0 / raw) - 1.0);
float tempK = 1.0 / (
(1.0 / (NOMINAL_T + 273.15)) +
(1.0 / B_COEFF) * log(resistance / NOMINAL_R)
);
return tempK - 273.15;
}
void loop() {
float temp = readTemperature();
bool heaterOn = digitalRead(SSR_PIN);
// Hysteresis control
if (temp < TARGET_TEMP - HYSTERESIS && !heaterOn) {
digitalWrite(SSR_PIN, HIGH);
Serial.printf("HEATER ON | Temp: %.1f°C\n", temp);
} else if (temp > TARGET_TEMP + HYSTERESIS && heaterOn) {
digitalWrite(SSR_PIN, LOW);
Serial.printf("HEATER OFF | Temp: %.1f°C\n", temp);
}
Serial.printf("Temp: %.1f°C | Heater: %s\n",
temp, heaterOn ? "ON" : "OFF");
delay(1000);
}
Real-World Applications
Home Automation
- Smart light switches
- Appliance control (fans, heaters)
- Irrigation valve control
- Garage door openers
Industrial & Automotive
- Motor starters (contactors)
- PLC output stages
- Automotive starter motor relay
- Battery management disconnect
Advantages vs. Alternatives
| vs. Actuator | Relay Advantage | Relay Disadvantage |
|---|---|---|
| MOSFET (direct) | Galvanic isolation, handles AC, surge tolerant | Slower switching, contact wear (EMR), larger |
| Solenoid | Pure electrical switching, no mechanical output | Cannot perform physical actuation |
| SSR vs EMR | EMR: zero on-resistance, handles surges, cheaper; SSR: silent, no bounce, longer life | EMR: bounce, wear, noise; SSR: leakage current, heat, cost |
| Contactor vs Relay | Contactor handles 10–1000 A loads | Larger, more expensive, slower, louder |
Limitations & Considerations
- Contact Wear (EMR): Arcing at contact surfaces erodes material. Rated lifespan is typically 100,000 electrical operations. Exceeding rated load accelerates wear.
- Contact Bounce (EMR): Mechanical contacts bounce for 1–5 ms when closing, causing brief on/off pulses. Not an issue for motors/heaters but can cause problems for logic circuits.
- Coil Power (EMR): Standard 5V relay coils draw 70–90 mA — too much for direct GPIO drive on most 3.3V MCUs. Use driver transistor or relay module.
- Leakage Current (SSR): SSRs have small leakage current (1–10 mA) when “off.” This can keep LED lights dimly lit or prevent full shutdown of sensitive loads.
- Heat Dissipation (SSR): At high currents, SSR voltage drop creates significant heat. Derate current or add heat sink.
- Inductive Load Suppression: When switching inductive loads (motors, solenoids), add snubber circuits (RC network across contacts) to suppress arcing and extend contact life.