BysMax

How to Connect a Relay Module to Arduino — 5V and 12V Control

14 min

A relay module lets an Arduino control high-voltage devices: lamps, motors, fans, and appliances running on 110V or 220V AC. The relay is an electromagnetic switch — a low-current Arduino signal activates a coil that physically switches a high-current circuit.

Time to complete: 15 minutes
Skill level: Beginner to Intermediate
Safety: This project involves mains voltage (110V/220V). Work only with the AC side disconnected from power.

How Relay Modules Work

A relay module contains:

  • Relay coil: activated by 3.3V–5V Arduino signal
  • Optocoupler: electrically isolates the Arduino from the mains circuit
  • LED indicator: shows relay state (ON/OFF)
  • Flyback diode: protects against voltage spikes from the coil

The relay has three screw terminals for the AC side:

  • COM — common (always connected)
  • NO — Normally Open (circuit open when relay is off)
  • NC — Normally Closed (circuit closed when relay is off)

Most projects use COM + NO: the circuit is open (device off) until the relay activates.

Relay Module Specifications

ParameterValue
Coil voltage5V DC
Coil current70–90 mA per relay
Control signal3.3V–5V (active LOW on most modules)
AC rating10A at 250VAC / 10A at 125VAC
DC rating10A at 30VDC / 10A at 28VDC
Response time~10 ms

Most relay modules are active LOW — they trigger when the control pin goes LOW (0V), not HIGH. This is important for your code.

Relay Module Pinout

PinNameConnect to
VCCPower5V Arduino
GNDGroundGND Arduino
IN1 (IN2, IN3...)Signal inputDigital pin
COMAC commonLive wire
NONormally OpenLoad (device)
NCNormally Closed(usually unused)

Wiring — Single Relay Module to Arduino Uno

DC side (low voltage — safe):

Relay ModuleArduino
VCC5V
GNDGND
IN1Digital pin 7

AC side — WARNING: work with power DISCONNECTED:

Connect a standard lamp or appliance:

  1. Cut the live wire (usually black in US, brown in EU)
  2. Connect one cut end to COM
  3. Connect other cut end to NO
  4. Neutral and earth wires pass through uncut

When the relay triggers, it completes the live wire circuit and the device turns on.

Code — Basic Relay Control

const int RELAY_PIN = 7;

void setup() {
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  
  // Most relay modules are active LOW
  // HIGH = relay OFF (coil de-energized)
  // LOW  = relay ON  (coil energized)
  digitalWrite(RELAY_PIN, HIGH);  // Start with relay OFF
  
  Serial.println("Relay control ready");
}

void loop() {
  // Turn relay ON (device on)
  Serial.println("Relay ON");
  digitalWrite(RELAY_PIN, LOW);   // Active LOW module
  delay(3000);

  // Turn relay OFF (device off)
  Serial.println("Relay OFF");
  digitalWrite(RELAY_PIN, HIGH);
  delay(3000);
}

If your relay triggers immediately when you power on the Arduino (before your code runs), this confirms it's an active LOW module. HIGH = OFF, LOW = ON.

Code — Check If Your Module is Active HIGH or LOW

const int RELAY_PIN = 7;

void setup() {
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  
  // Test: set HIGH first, observe if relay clicks ON or stays OFF
  digitalWrite(RELAY_PIN, HIGH);
  Serial.println("Set HIGH — if relay is ON, it's active LOW");
  delay(2000);
  
  digitalWrite(RELAY_PIN, LOW);
  Serial.println("Set LOW — if relay is ON now, it's active HIGH");
  delay(2000);
}

void loop() {}

Listen for a click and watch the LED. This tells you the module logic level.

Code — Automatic Light with PIR Sensor

Turn a lamp on when motion is detected:

const int RELAY_PIN = 7;
const int PIR_PIN   = 8;

unsigned long lastMotion = 0;
const unsigned long LIGHT_ON_TIME = 30000;  // 30 seconds
bool lampOn = false;

void setup() {
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(PIR_PIN, INPUT);
  
  digitalWrite(RELAY_PIN, HIGH);  // Lamp off
  delay(30000);  // PIR calibration
  Serial.println("Motion-activated light READY");
}

void loop() {
  bool motion = digitalRead(PIR_PIN);
  unsigned long now = millis();

  if (motion == HIGH) {
    lastMotion = now;
    if (!lampOn) {
      digitalWrite(RELAY_PIN, LOW);  // Turn lamp ON
      lampOn = true;
      Serial.println("Lamp ON — motion detected");
    }
  }

  if (lampOn && (now - lastMotion >= LIGHT_ON_TIME)) {
    digitalWrite(RELAY_PIN, HIGH);  // Turn lamp OFF
    lampOn = false;
    Serial.println("Lamp OFF — timeout");
  }

  delay(100);
}

Code — Timer Relay (ON for X minutes)

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

unsigned long relayOnTime = 0;
const unsigned long RELAY_DURATION = 5 * 60 * 1000UL;  // 5 minutes in ms
bool relayActive = false;

void setup() {
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  digitalWrite(RELAY_PIN, HIGH);  // Start OFF
}

void loop() {
  // Button press (active LOW with INPUT_PULLUP)
  if (digitalRead(BUTTON_PIN) == LOW) {
    if (!relayActive) {
      relayActive = true;
      relayOnTime = millis();
      digitalWrite(RELAY_PIN, LOW);  // Turn ON
      Serial.println("Timer started: relay ON for 5 minutes");
      delay(200);  // Debounce
    }
  }

  // Auto-off after duration
  if (relayActive && (millis() - relayOnTime >= RELAY_DURATION)) {
    relayActive = false;
    digitalWrite(RELAY_PIN, HIGH);  // Turn OFF
    Serial.println("Timer done: relay OFF");
  }
}

Multi-Channel Relay Boards

2-channel relay (2 IN pins): control two independent devices
4-channel relay (4 IN pins): four independent circuits
8-channel relay (8 IN pins): eight independent circuits

All channels share VCC and GND. Each channel needs one Arduino digital pin.

// 4-channel relay example
const int RELAY1 = 4;
const int RELAY2 = 5;
const int RELAY3 = 6;
const int RELAY4 = 7;

void setup() {
  int relays[] = {RELAY1, RELAY2, RELAY3, RELAY4};
  
  for (int i = 0; i < 4; i++) {
    pinMode(relays[i], OUTPUT);
    digitalWrite(relays[i], HIGH);  // All OFF (active LOW)
  }
}

void loop() {
  // Sequence all 4 relays
  int relays[] = {RELAY1, RELAY2, RELAY3, RELAY4};
  
  for (int i = 0; i < 4; i++) {
    digitalWrite(relays[i], LOW);   // ON
    delay(1000);
    digitalWrite(relays[i], HIGH);  // OFF
    delay(500);
  }
}

Power Supply for Many Relays

Each relay coil draws ~70–90 mA. Arduino's 5V pin from USB supplies max 500 mA.

Relay countCurrent neededPower source
1–2 relays140–180 mAArduino 5V pin (OK)
4 relays280–360 mAArduino 5V pin (marginal)
8 relays560–720 mAExternal 5V supply

For 4+ relays, power VCC from an external 5V supply (not Arduino 5V pin). Connect the supply GND to Arduino GND.

Safety Checklist

Working with mains voltage (110V/220V) requires care:

  • Never work with the mains cable plugged in when wiring the AC side
  • Use an enclosure — exposed relay terminals are a shock hazard
  • Check relay rating — 10A/250VAC is sufficient for most home appliances under 2,400W
  • Don't exceed the relay's current rating — a 10A relay should not control a device drawing 15A
  • Use proper wire gauges — 18 AWG for up to 10A on AC side
  • Fuse the AC circuit — add a fuse rated slightly above your device's draw

For learning purposes, test with low-voltage DC devices (LED strips, DC motors, fans) before working with AC mains.

Troubleshooting

Relay clicks but device doesn't turn on:

  • Wrong terminal: check COM and NO (not NC)
  • Verify AC side is connected and powered
  • Test continuity of your wiring with a multimeter

Relay doesn't click:

  • Check VCC (5V) and GND connections
  • Verify the signal pin is set correctly (HIGH vs LOW, active logic)
  • LED should illuminate when relay is ON — if no LED, module may be defective

Arduino resets when relay activates:

  • Relay coil surge is drawing too much current from Arduino 5V
  • Power relay VCC from external supply; share GND with Arduino

Relay triggers randomly:

  • Long signal wire picking up noise — add 100nF capacitor on IN pin to GND
  • Floating input: set pin with pinMode(RELAY_PIN, OUTPUT) and initialize HIGH

Frequently Asked Questions (FAQ)

Can Arduino directly control 110V/220V without a relay? No. Arduino GPIO outputs 5V at max 40 mA per pin. Mains voltage is 110–220V AC at potentially hundreds of amps. A relay (or SSR) is required to bridge these two worlds safely.

What is the difference between a relay and a solid state relay (SSR)? A mechanical relay uses a physical electromagnetic switch — it clicks audibly and handles both AC and DC. An SSR (Solid State Relay) uses transistors or SCRs, switches silently, handles higher switching speeds, but costs more. For simple on/off control of AC devices, a relay module is cheaper and simpler.

Can I use a relay module with ESP32 or ESP8266? Yes. Connect VCC to 3.3V (or 5V if the module supports it), GND to GND, IN to any GPIO. Use the same code logic (HIGH/LOW). Note that some relay modules require 5V to trigger reliably — check the datasheet of the specific module.

What appliances can I control with a 10A relay? At 110V: up to 1,100W (10A × 110V). At 220V: up to 2,200W (10A × 220V). This covers most lamps, fans, small heaters, and motors. For high-power devices (electric stoves, air conditioners), use a 30A+ relay or a contactor.

Why does my relay module have an active LOW input? Manufacturers chose active LOW because it's safer at startup — most microcontrollers initialize digital pins as LOW or floating, so the relay stays OFF until explicitly commanded. It also reduces the chance of accidentally triggering the relay during a reboot.

Comentarios (0) /en/blog/arduino-relay-module