In what is a relay we explained the relay itself. Here we talk about the blue module that everyone buys: it has a relay, a transistor, a diode and an LED, and sometimes an optocoupler. It's ready to use, but there are details that confuse people.
What's inside?
- The relay itself (usually an SRD-05VDC-SL-C: a 5 V coil, and 10 A contacts).
- A transistor that drives the coil, because an Arduino pin can't supply 70 mA.
- A diode across the coil, absorbing the high voltage produced when the coil switches off. Without it, the transistor burns out.
- An LED showing the state.
- On some modules, an optocoupler before the transistor.
So the module is exactly the circuit you would have built yourself, ready-made.
The pins
| Control side | Load side |
|---|---|
| VCC: 5 V | COM: common |
| GND | NO: normally open (closes when the relay is on) |
| IN (or IN1, IN2…) | NC: normally closed (opens when the relay is on) |
NO or NC?
- NO for loads that should be off if something fails or the power goes: a motor, a heater and a pump. This is usually the safe choice.
- NC for loads that must stay on if the Arduino stops: for example a light or a cooling fan.
Always think: if the Arduino hangs or disconnects, what do you want the load to be?
Active low: why does it work backwards?
Many modules (especially those with an optocoupler) switch on when IN = LOW:
digitalWrite(RELAY, LOW); // ON
digitalWrite(RELAY, HIGH); // OFF
Test your module: if the relay switches on as soon as you connect IN to GND, it's active low.
Why did the designers do this? The optocoupler's LED is connected between VCC and the IN pin. So when IN goes LOW, current flows through the LED and the relay switches on. And some modules have an H/L jumper to choose.
The click when the board starts
As soon as the Arduino resets, the pins are INPUTs for a few moments, and the relay may click and switch the load on briefly. That's dangerous with a motor or a door.
The fix: write the safe state before pinMode:
digitalWrite(RELAY, HIGH); // OFF for an active-low module
pinMode(RELAY, OUTPUT);
Code with a button
const int RELAY = 7, BTN = 2;
bool on = false, lastBtn = HIGH;
void setup() {
digitalWrite(RELAY, HIGH); // OFF (active low)
pinMode(RELAY, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
}
void loop() {
bool b = digitalRead(BTN);
if (b == LOW && lastBtn == HIGH) { // a new press
on = !on;
digitalWrite(RELAY, on ? LOW : HIGH);
delay(50); // against bounce
}
lastBtn = b;
}
The details on bounce are in buttons and debouncing. And if you want to control it from a phone, see the ESP32 web server.
JD-VCC and isolation
On modules that have a JD-VCC jumper:
- Jumper fitted: the optocoupler and relay run on the Arduino's same 5 V. Easy, but there's no real isolation.
- Jumper removed: connect JD-VCC and its GND from a separate 5 V source, and VCC from the Arduino. That way the relay coil is isolated from the Arduino. See the optocoupler.
This is useful if there are many relays (each relay about 70 mA) or if noise is resetting the Arduino.
A 4- or 8-relay module
| Relays switched on | Approximate current |
|---|---|
| 1 | 70 mA |
| 4 | 280 mA |
| 8 | 560 mA |
8 relays from USB or from the Uno's onboard regulator is too much, and you'll find the board resetting. Power the relays from a separate 5 V source (at least 1 A) through JD-VCC. See powering the Arduino.
With an ESP32 (3.3 V)
A 5 V active-low module may not switch off properly, because the ESP32's HIGH (3.3) isn't enough to fully turn off the optocoupler LED running from 5 V. The fixes:
- A module marked as 3.3 V (low level trigger 3V).
- Or a transistor between them.
- Or an active-high module with a trigger that accepts 3.3.
Why? The difference between 5 and 3.3 = 1.7 V, which is close to the voltage the opto's LED needs to light. So the LED stays faintly lit, and the relay may stay on. Another simple fix: remove the JD-VCC jumper, and connect VCC to 3.3 from the ESP32, and JD-VCC to 5. That way the opto's LED runs on 3.3 and the coil on 5.
Load limits
What's marked on the relay (for example 10A 250VAC) is the limit of the relay, not the module:
- The board traces and terminals often can't carry 10 A safely. Treat the practical limit as much lower.
- Inductive loads (motors and compressors) and LED floodlights have a high inrush current that destroys the contacts. Choose a relay with a large margin.
- For frequent switching or silent operation, consider an SSR.
A relay or an SSR?
| Mechanical relay | SSR | |
|---|---|---|
| Sound | Clicks | Silent |
| Lifespan | Limited by the number of operations | Very long |
| With DC | Works | Needs a dedicated DC type |
| Leakage when off | Zero | A small leakage |
| Heat | Little | Heats up at high current |
An ordinary SSR is for AC only, and if you put it on a DC load it won't switch off. And a cheap SSR may be a fake that can't handle the current marked on it.
Well-known faults
| Symptom | Usual cause |
|---|---|
| The LED lights but the relay doesn't click | The relay supply is weak, or JD-VCC isn't connected |
| The relay works backwards | The module is active low |
| The Arduino resets when the relay closes | Noise or voltage drop; separate the supply with JD-VCC |
| It won't switch off with an ESP32 | 3.3 V isn't enough; see the fixes above |
| The contacts have welded and the load won't switch off | High inrush current or a load larger than the relay |
Warning
If you're connecting the relay to 220 V mains, read safety with household mains first. An exposed module must be enclosed in a box, and don't touch it while it's connected.
FAQ
Can I run a small DC motor with the relay? Yes, and put a diode across the motor itself too. And if you want to control the speed or switch on and off quickly, a MOSFET is better.
The relay is warm; is that normal? The coil takes about 0.35 W, so slight warmth is normal. But if the terminals themselves are hot, the current is too large for the module or the screws aren't tightened properly.