A relay is an electrically controlled switch that allows a low-power microcontroller (like Arduino, ESP32, Raspberry Pi, or STM32) to control high-power devices (motors, lights, heaters, etc.). ๐
โก 1. Understanding Relay Modules
Types of Relays Used with Microcontrollers
โ
Electromechanical Relays (EMR) โ Clicky, with physical contacts, handles AC/DC loads.
โ
Solid-State Relays (SSR) โ No moving parts, faster switching, best for AC loads.
โ
Relay Modules (with built-in transistors & protection diodes) โ Plug-and-play!
๐ง 2. How to Connect a Relay to Microcontrollers
A microcontroller cannot drive a relay directly because it outputs low voltage (3.3V or 5V), while the relay coil may require higher current (70-150mA at 5V or 12V). Solution? Use a transistor!
๐น Method 1: Connecting a Relay Module (Easy Way)
๐ก If you have a ready-made relay module, it already has a transistor & diode built-in. Just connect it directly to your microcontroller.
๐ ๏ธ Wiring for a Relay Module
| Relay Pin | Connects To |
|---|---|
| VCC | 5V (or 3.3V for ESP32, if supported) |
| GND | GND of microcontroller |
| IN | Digital Pin (to control the relay) |
| COM (Common) | Connected to Load |
| NO (Normally Open) | Load’s live wire (default OFF) |
| NC (Normally Closed) | Load’s live wire (default ON) |
โ
When IN = HIGH, the relay turns ON (NO closes).
โ
When IN = LOW, the relay turns OFF (NO opens).
๐น Method 2: Using a Transistor (For a Raw Relay)
If you have a bare relay, you’ll need a transistor (e.g., NPN 2N2222, BC547, TIP120) to switch it ON/OFF.
๐ ๏ธ Circuit Diagram
- Microcontroller GPIO โ 1kฮฉ resistor โ Transistor Base
- Transistor Collector โ Relay Coil
- Transistor Emitter โ GND
- Diode (1N4007) across the relay coil (for protection)
- Relay Common & NO/NC connected to the load
๐ค 3. Connecting a Relay to Different Microcontrollers
๐น Arduino (5V Logic)
โ
Directly connect IN to an Arduino digital pin.
โ
Works with 5V relay modules.
int relayPin = 7;
void setup() {
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, HIGH); // Turns ON the relay
delay(1000);
digitalWrite(relayPin, LOW); // Turns OFF the relay
delay(1000);
}
๐น ESP32 (3.3V Logic)
โ Problem: Most relay modules require 5V signal, but ESP32 runs on 3.3V logic.
โ
Solution: Use a transistor circuit or a 3.3V relay module.
#define relayPin 2 // ESP32 GPIO2
void setup() {
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, HIGH); // Relay ON
delay(2000);
digitalWrite(relayPin, LOW); // Relay OFF
delay(2000);
}
๐น Raspberry Pi (3.3V Logic)
โ Use a 3.3V relay module or an NPN transistor (like ESP32 setup).
import RPi.GPIO as GPIO
import time
relayPin = 17 # GPIO17
GPIO.setmode(GPIO.BCM)
GPIO.setup(relayPin, GPIO.OUT)
while True:
GPIO.output(relayPin, GPIO.HIGH) # Relay ON
time.sleep(2)
GPIO.output(relayPin, GPIO.LOW) # Relay OFF
time.sleep(2)
๐น STM32 (3.3V Logic)
โ
Same as ESP32, use a transistor circuit if needed.
โ
Some STM32 boards have 5V pins, allowing direct connection.
โ ๏ธ Safety Tips When Using Relays
โก Relays control HIGH VOLTAGE โ Always be careful!
โ
Use optocouplers if controlling AC loads for isolation.
โ
Use a diode across the relay coil (if not using a module) to prevent voltage spikes.
โ
NEVER directly switch AC mains with a microcontroller โ use relays rated for it.
๐ฅ Conclusion: Which Method is Best?
๐น For Beginners โ Use a relay module (easiest, no extra components needed).
๐น For Low-Power Loads โ Use a transistor to drive a relay coil.
๐น For High-Voltage Loads โ Use optocouplers & high-quality relays.