A microcontroller pin can supply about 20 milliamps at 5 volts. A ceiling fan wants six amps at 230. The relay is the part that bridges those two worlds, and it turns up in more final-year projects than any component except the LCD — home automation, smart irrigation, security, industrial timers, every one of them ends in a relay.
It is also the only component on a student's bench that can kill somebody. This page covers both halves.
What is inside
An electromagnet and a switch, mechanically linked and electrically separate. Put current through the coil and it pulls a metal blade across; take the current away and a spring pulls it back. The blade is the switch, and it has no electrical connection to the coil at all — which is the entire point.
| Terminal | Means | At rest | Coil energised |
|---|---|---|---|
| COM | Common — the pole | — | — |
| NC | Normally closed | Connected to COM | Open |
| NO | Normally open | Open | Connected to COM |
Normally means with no power anywhere. For a lamp that should be off until the Arduino says otherwise, wire it to COM and NO.
Use a relay module, not a bare relay
A bare relay cannot be driven from a pin. Its coil needs 70 to 100 mA, which is five times what a pin can give, and switching it off produces a large reverse voltage spike from the collapsing magnetic field that destroys whatever was driving it.
A relay module — the small blue board with screw terminals — already has the three things you would otherwise have to add:
- A transistor to supply the coil current, so the pin only has to supply base current.
- A flyback diode across the coil, to absorb the spike. Look for the small glass part beside the relay.
- An opto-isolator on better boards — the black four-pin chip — so there is no electrical path at all between the Arduino and the switching side.
For a rupee or two more than a bare relay, this is not a decision worth thinking about. Buy the module.
Active LOW — the thing nobody warns you about
Most cheap relay modules switch on when the input pin goes LOW, not high. digitalWrite(relayPin, HIGH) turns the relay off. This is not a fault and it is rarely printed on the board.
It has a consequence that catches everybody. An Arduino pin starts as an input at reset, and an input floats or reads low — so on an active-low board every relay clicks on for the moment between power-up and your setup() running. On a project with four relays, all four appliances flick on at every reset.
The sketch
const int relayPin = 7;
// Most cheap relay boards are ACTIVE LOW: the coil energises when the pin
// goes LOW. Set this to false for a board that switches on a HIGH.
const bool ACTIVE_LOW = true;
void relay(bool on) {
digitalWrite(relayPin, on == ACTIVE_LOW ? LOW : HIGH);
}
void setup() {
// Set the pin to the OFF level BEFORE making it an output. An Arduino pin
// is an input reading LOW while it boots, which on an active-low board is
// "switch the load on" - so the lamp flashes on at every reset unless you
// do these two lines in this order.
relay(false);
pinMode(relayPin, OUTPUT);
relay(false);
Serial.begin(9600);
}
void loop() {
relay(true);
Serial.println("ON");
delay(3000);
relay(false);
Serial.println("OFF");
delay(3000);
}The relay(bool on) wrapper is worth keeping in every project. Once the polarity lives in exactly one place, the rest of the sketch says what it means — relay(true) — instead of digitalWrite(7, LOW), which reads as off to every person who ever opens your code, including you in three weeks.
The JD-VCC jumper
Multi-channel boards have three pins in a corner — VCC, JD-VCC and GND — with a small jumper across two of them. It decides whether the isolation the opto-coupler provides is real.
| Jumper | Coil powered from | Isolation |
|---|---|---|
| Fitted (as it ships) | The Arduino's 5 V | None in practice. The opto is bypassed, and coil noise goes straight into your supply. |
| Removed, JD-VCC fed from its own 5 V supply | A separate supply | Real. The two sides share no conductor. |
With the jumper removed, connect the separate supply's ground to JD-GND, not to the Arduino's ground.
What the contacts can really switch
The board is printed 10A 250VAC. Treat that as the number the relay survives once, not the one it works at every day.
- Derate to about a third for anything that will switch repeatedly. Call it 3 A on a 10 A relay.
- Motors, pumps and compressors draw several times their running current at start-up. A 1 A fridge compressor can pull 8 A for a moment, and it is that moment that welds the contacts.
- Fluorescent and LED lighting is worse than it looks. The electronic ballast or driver charges a capacitor at switch-on, which is nearly a short circuit for a millisecond.
- A relay switching an inductive load arcs. That arc is what eventually erodes the contacts, and it is why a relay that clicks perfectly may still not be passing current.
For anything above a couple of amps, or switched often, use a contactor driven by the relay, or a solid-state relay. Both are what the equivalent commercial product does.
Mains wiring — read this before you plug anything in
- 1
Switch the live wire, never the neutral
Break the live conductor with the relay. Switching the neutral leaves the appliance sitting at mains potential while apparently off, which is exactly how somebody gets hurt changing a bulb.
- 2
Mains never touches breadboard or jumper wires
Breadboard clips are rated for a few volts and 1 A, and the contacts are 2.54 mm apart. Mains goes into the screw terminals on the relay board and nowhere else, in properly rated cable.
- 3
Everything lives in a closed box
A plastic enclosure, with the mains side unreachable and cable glands where wires enter. A project carried to a viva with exposed 230 V terminals should not be plugged in, and a good panel will stop you.
- 4
Keep the two sides physically apart
The gap across the relay board between the low-voltage and mains sides is deliberate. Do not run a signal wire across it, and do not mount the Arduino directly under the terminals.
- 5
Have somebody competent check it, and test with a lamp
Prove the whole thing with a table lamp on an RCD-protected socket before it goes anywhere near the load you actually built it for.
When it does not work
| What you see | Cause | Fix |
|---|---|---|
| Relay is on when the sketch says off | Active-low board driven as active-high | Set ACTIVE_LOW = true, or wire the load to NO instead of NC. |
| Clicks at every reset | pinMode before the first digitalWrite | Set the OFF level first — see the sketch. |
| Clicks, but the load does not switch | Load wired to the wrong contact, or contacts welded | Check continuity COM to NO with the coil energised, power off. |
| Arduino resets when relays pull in | Coil current exceeding the regulator | Separate supply for the coils, JD-VCC jumper removed. |
| Relay chatters or buzzes | Coil voltage too low, or a floating input pin | Measure the coil supply under load. Set the pin before enabling it. |
| Works alone, misbehaves with a sensor attached | Switching noise coupling into the sensor | Separate supplies, short sensor leads, 100 nF across the sensor's power pins. |
Where to take it
- Add a soil moisture sensor and a pump, and it is automatic irrigation.
- Add an LDR and a real-time clock, and it is street lighting that ignores a cloudy afternoon.
- Add an ESP32 instead of the Arduino and it is app-controlled home automation — the relay side does not change at all.
- Add a current sensor on the load side and it can tell you whether the appliance actually came on, which is the difference between a controller and a monitored controller in a report.