11:06:59 AM NPT Arduino, Robotics & IoT tutorials for Nepal
Beginner

Arduino Relay Module Guide: Switch Loads Safely

Learn to use a 5V relay module with Arduino to switch a 12V DC lamp or fan. Pinout, COM/NO/NC, active-LOW logic, code and safety rules.

On this page
Arduino Relay Module Guide: Switch Loads Safely

A relay is an electrically controlled switch. It lets a small Arduino signal turn a much bigger load on and off, such as a lamp, a fan or a pump. This guide explains the pins on a common 1-channel relay module, how to wire a 12 V DC lamp or fan through it, and why many modules work "backwards" (active LOW). We stay on low-voltage DC here, on purpose.

Safety note

Do not wire mains voltage yourself. Mains can kill. Switching a mains appliance should be done only by a qualified electrician, using a properly enclosed, rated relay and correct fuse and earthing. Everything in this tutorial uses a 12 V DC supply and a 12 V lamp or fan. Check that your supply and load match, and use a fuse or a current-limited adapter.

What you need

Scroll sideways to see all columns.

Item Qty Notes
Arduino Uno (or clone) + USB cable 1
1-channel 5 V relay module 1 Blue relay cube, 3 signal pins and a 3-screw terminal
12 V DC lamp (for example a 12 V LED bulb) or a small 12 V fan 1 Draws under 1 A
12 V DC adapter or battery 1 Rated higher than the load current
Push button 1 Optional, for toggling
1N4007 diode 1 Only for a motor load such as a fan
Jumper wires 8
Digital multimeter 1

The relay module pins

The module has two sides.

Signal side (to the Arduino): VCC, GND and IN. VCC is 5 V for the coil and the small driver circuit. IN is the control signal.

Load side (screw terminal): three terminals marked COM, NO and NC.

  • COM (common): the moving contact. Connect the supply to this one.
  • NO (normally open): not connected to COM when the relay is off, connected when it is on. Use this for "load off by default".
  • NC (normally closed): connected to COM when the relay is off, disconnected when it is on.

The relay is a switch only. It does not supply any power. Contact ratings are printed on the relay body (for example "10A 250VAC / 10A 30VDC"). Use the DC rating for DC loads and stay well below it.

Active LOW and the jumper

Most hobby modules have an optocoupler and a transistor. They are usually active LOW: the relay turns on when IN is pulled LOW, and off when IN is HIGH. Some modules have a jumper to choose HIGH or LOW trigger. A small LED on the board shows the state.

Some modules have a jumper between VCC and JD-VCC. With the jumper in place the coil takes power from the same 5 V as the logic. Removing it can let you power the coil from a separate 5 V supply. Full isolation depends on the module circuit and requires that the isolated sides share no ground or other connection. For this tutorial, leave the jumper in place.

Why a diode for a fan

A motor or coil stores energy and creates a voltage spike when switched off. That spike can create sparks across the relay contacts and cause noise. A 1N4007 diode across the motor, with the stripe (cathode) toward the positive terminal, gives the spike a safe path. A plain lamp does not need it.

Connection table

Scroll sideways to see all columns.

Component pin Connects to
Relay VCC Arduino 5V
Relay GND Arduino GND
Relay IN Arduino D7
12 V supply + Relay COM
Relay NO Lamp / fan +
Lamp / fan − 12 V supply −
Push button (optional) D2 and GND (uses the internal pull-up)

Signal path in one line:

D7 → relay IN → coil clicks → COM and NO close → 12 V supply → lamp

Step-by-step build

  1. With everything unplugged, wire the signal side: VCC to 5V, GND to GND, IN to D7.
  2. Wire the load side: 12 V + to COM, NO to the lamp, lamp back to 12 V −. Use properly tightened screw terminals with no loose strands.
  3. Upload the sketch with the 12 V supply still unplugged.
  4. Plug in the 12 V supply. The lamp should switch on and off every 3 seconds, and the module LED and click should match.
  5. Optional: press the button on D2 to toggle the lamp manually instead.

The code

sketch-1.inoDownload code
// ElectroDai - Relay module: switch a 12 V DC lamp
// Relay IN -> D7, button between D2 and GND

const int RELAY_PIN = 7;
const int BUTTON_PIN = 2;

// Most modules are active LOW. If yours works the opposite way, set false.
const bool RELAY_ACTIVE_LOW = true;

bool relayOn = false;
bool lastReading = HIGH;
bool stableButton = HIGH;
unsigned long lastChange = 0;

void setRelay(bool on) {
  relayOn = on;
  bool level = RELAY_ACTIVE_LOW ? !on : on;
  digitalWrite(RELAY_PIN, level ? HIGH : LOW);
  Serial.println(on ? "Relay ON" : "Relay OFF");
}

void setup() {
  digitalWrite(RELAY_PIN, RELAY_ACTIVE_LOW ? HIGH : LOW);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);   // button pressed = LOW
  Serial.begin(9600);
  setRelay(false);                     // start with the load off
}

void loop() {
  bool button = digitalRead(BUTTON_PIN);

  unsigned long now = millis();
  if (button != lastReading) {
    lastReading = button;
    lastChange = now;
  }
  if (button != stableButton && now - lastChange >= 50) {
    stableButton = button;
    if (stableButton == LOW) setRelay(!relayOn);
  }
}

If you do not have a button, replace the body of loop() with setRelay(true); delay(3000); setRelay(false); delay(3000); to run the demo.

Testing with a multimeter

  • Coil supply: 5 V between relay VCC and GND.
  • IN pin: about 5 V when idle (relay off) and about 0 V when active, on an active-LOW module.
  • Continuity on the load side: with the Arduino and the 12 V supply unplugged, set the meter to the continuity (beep) range. COM to NO should be open when the relay is off, and beep when you power the Arduino and trigger the relay (do this with the load supply still disconnected).
  • Load voltage: with the load connected and the relay on, measure across the lamp. It should be close to the 12 V supply.
  • Voltage drop: the voltage across the closed COM and NO contacts should be near 0 V. A noticeable voltage means a poor contact or too much load.

Troubleshooting

Scroll sideways to see all columns.

Symptom Likely cause Fix
Load is on when it should be off Active-LOW logic or NC used Set RELAY_ACTIVE_LOW; use NO
Relay clicks at power-up IN floats LOW during Arduino reset Disconnect the load and use a module with a defined inactive input at reset, or fit a suitable pull-up for an active-LOW input
Relay does not click No 5 V, or IN not connected Check VCC, GND, IN
Clicks but the lamp stays off Load wired on the wrong terminals Check COM, NO and the supply
Arduino resets when a fan starts Spikes and shared power Add a 1N4007 across the fan; power the fan separately
Contacts buzz or chatter Weak 5 V supply Use a better USB port or supply
Relay clicks but sparks or sticks Load above the contact rating Choose a lighter load or a higher-rated relay

FAQ

Can I switch a 220 V bulb?

That should be done only by a qualified electrician with proper enclosures and protection. Do not experiment with mains on a breadboard or an open module.

What is the difference between NO and NC?

NO is open when the relay is off. NC is closed when the relay is off. For safety most projects use NO, so the load stays off if the relay coil loses power.

What about a solid-state relay or a MOSFET?

For silent switching of DC loads such as LED strips, a logic-level MOSFET is often better. Relays are good for isolation and for switching either DC or AC contacts.

Can the Arduino pin drive the relay coil directly?

Not usually. The module has a driver transistor for that. A bare relay coil needs a transistor and a flyback diode.

Next step: control an LED over Wi-Fi in ESP32 Web Server: Control an LED from Your Phone over Wi-Fi.

Related tutorials

See all Arduino →