573 Relays and Solenoids
Learning Objectives
After completing this chapter, you will be able to:
- Understand relay operation and specifications
- Interface relays safely with microcontrollers
- Control solenoids for linear actuation
- Implement flyback protection for inductive loads
- Design safe high-voltage switching circuits
- Use solid-state relays (SSR) for silent, high-speed switching
573.1 Relay Fundamentals
Relays are electrically-operated switches that allow low-power circuits (microcontrollers) to control high-power loads (motors, heaters, lights).
Key Benefits:
- Electrical isolation: Complete separation between control and load circuits
- High current switching: 10A, 20A, or more
- AC and DC loads: Can switch both types
- Low control current: Typically 20-100mA coil current
573.1.1 Relay Specifications
| Parameter | Typical Values | What It Means |
|---|---|---|
| Coil voltage | 3.3V, 5V, 12V, 24V | Voltage needed to activate relay |
| Coil current | 50-100mA | Current drawn by coil (needs driver!) |
| Contact rating | 10A @ 250VAC | Maximum load current and voltage |
| Contact type | SPST, SPDT, DPDT | Number of poles and throws |
573.1.2 Relay Control Circuit
Most relay coils draw 50-100mA, far exceeding the 20-40mA GPIO limit. Always use a transistor driver!
// Relay control with transistor driver
#define RELAY_PIN 25
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Relay OFF (normally open)
}
void loop() {
// Turn relay ON (closes normally-open contact)
digitalWrite(RELAY_PIN, HIGH);
Serial.println("Relay ON - Load powered");
delay(3000);
// Turn relay OFF
digitalWrite(RELAY_PIN, LOW);
Serial.println("Relay OFF - Load unpowered");
delay(3000);
}573.1.3 Relay Module Wiring
Most relay modules include the transistor driver and flyback diode:
ESP32 Relay Module High-Power Load
GPIO25 ------> IN (control)
GND ----------> GND
3.3V/5V -----> VCC
COM ----------------> Load common
NO (normally open) -> Load hot (when relay ON)
NC (normally closed)-> Load hot (when relay OFF)
573.2 Solenoid Control
Solenoids provide linear push/pull motion for locks, valves, and latches.
Characteristics:
- Fast response (5-50ms)
- Binary operation (on/off only)
- High inrush current
- Requires flyback protection
#define SOLENOID_PIN 26
void setup() {
pinMode(SOLENOID_PIN, OUTPUT);
}
void loop() {
// Activate solenoid (pull/push)
Serial.println("Activating solenoid lock...");
digitalWrite(SOLENOID_PIN, HIGH);
delay(1000);
// Release solenoid
Serial.println("Releasing solenoid lock...");
digitalWrite(SOLENOID_PIN, LOW);
delay(3000);
}
// Smart lock example
void unlockDoor() {
digitalWrite(SOLENOID_PIN, HIGH);
Serial.println("Door unlocked");
// Auto-lock after 5 seconds
delay(5000);
digitalWrite(SOLENOID_PIN, LOW);
Serial.println("Door locked");
}573.3 Flyback Protection
573.4 Critical: Inductive Kickback Protection
Relay coils and solenoids are inductors. When power is cut, the collapsing magnetic field generates a high voltage spike (potentially 100V+ from a 12V supply) that can destroy transistors and microcontrollers!
Required Protection Circuit:
GPIO --> [Transistor/MOSFET] --+-- Relay Coil --+
| |
+-- [Diode] <----+
|
GND
Diode specs:
- Voltage rating > supply voltage
- Current rating >= coil current
- 1N4007 for relays, 1N5819 Schottky for motors
573.5 Solid-State Relays (SSR)
For silent, high-speed, and maintenance-free switching, use solid-state relays.
Advantages over mechanical relays:
- No mechanical wear
- Silent operation
- Faster switching (microseconds vs milliseconds)
- No contact bounce
- Works with PWM for heater control
Disadvantages:
- Voltage drop across output (1-2V)
- Requires heatsink for high loads
- More expensive
- No electrical isolation (optocoupler-type has some isolation)
// SSR for heater PWM control
#define SSR_PIN 27
void setup() {
// SSR can handle PWM for proportional heating
ledcSetup(0, 1, 8); // 1 Hz, 8-bit (slow PWM for heating)
ledcAttachPin(SSR_PIN, 0);
}
void setHeaterPower(int percent) {
// 0-100% maps to 0-255
int duty = map(percent, 0, 100, 0, 255);
ledcWrite(0, duty);
}573.6 Valve Control
Solenoid valves control fluid flow in irrigation, HVAC, and industrial systems.
#define VALVE_PIN 25
#define FLOW_SENSOR_PIN 34
void setup() {
pinMode(VALVE_PIN, OUTPUT);
pinMode(FLOW_SENSOR_PIN, INPUT);
digitalWrite(VALVE_PIN, LOW); // Valve closed
}
// Water for specified duration
void water(int seconds) {
Serial.print("Watering for ");
Serial.print(seconds);
Serial.println(" seconds");
digitalWrite(VALVE_PIN, HIGH); // Open valve
delay(seconds * 1000);
digitalWrite(VALVE_PIN, LOW); // Close valve
Serial.println("Watering complete");
}
// Water until flow sensor detects specified volume
void waterVolume(float liters) {
float flowRate; // Liters per minute
float totalVolume = 0;
digitalWrite(VALVE_PIN, HIGH);
while (totalVolume < liters) {
// Read flow sensor (pulse counting)
// This is simplified - real implementation needs interrupt
flowRate = readFlowSensor();
totalVolume += flowRate / 60.0; // Convert to liters/second
delay(1000);
Serial.print("Volume: ");
Serial.print(totalVolume);
Serial.print(" / ");
Serial.print(liters);
Serial.println(" L");
}
digitalWrite(VALVE_PIN, LOW);
}573.7 Safety Considerations
573.8 High Voltage Safety
When working with mains voltage (120V/240V AC):
- Use properly rated relay contacts (check current AND voltage rating)
- Ensure electrical isolation between low-voltage and mains circuits
- Use enclosed, insulated enclosures for all high-voltage connections
- Install fuses or circuit breakers on the mains side
- Follow local electrical codes (NEC, IEC, etc.)
- Consider hiring a licensed electrician for permanent installations
573.9 Whatโs Next?
Now that you understand switching actuators, youโre ready to explore visual and audio output devices.