Building a reliable relevador arduino (Arduino relay) circuit is the standard bridge between low-voltage microcontroller logic and high-voltage AC loads. A relay is an electrically operated switch that uses a low-power electromagnet to mechanically close or open a high-power circuit. When you need an Arduino to turn on a 120V desk lamp, a 240V water heater, or a 12V DC solenoid valve, you cannot wire the load directly to the microcontroller's GPIO pins. Doing so will instantly destroy the ATmega328P chip and potentially start a fire.
This guide provides the exact hardware specifications, isolation wiring techniques, and compilable C++ code to safely switch mains voltage using a standard 5V relay module. Note: All mains voltage wiring assumes NEC-style guidance; your local AHJ (Authority Having Jurisdiction) has final authority on code compliance.
Project Overview & Difficulty Rating
Target Board Variant: Arduino Uno R3 (ATmega328P) or Arduino Nano v3
Time to Complete: 45 minutes
Core Concept: Galvanic isolation via optocouplers and electromechanical switching
The direct answer for most hobbyist AC switching needs is a 5V relay module featuring a Songle SRD-05VDC-SL-C relay and a PC817 optocoupler. This module requires only 70mA of coil current to switch up to 10A at 250VAC, making it perfectly matched to the Arduino's 5V output capabilities, provided you manage the power budget correctly.
Hardware BOM & Pin Mapping
Before stripping any wires, verify your components against this spec sheet. Cheap, unbranded relay modules often omit the flyback diode, which will destroy your Arduino's voltage regulator over time due to back-EMF spikes.
| Component | Exact Variant / Model | Key Specification |
|---|---|---|
| Microcontroller | Arduino Uno R3 (or Nano v3) | ATmega328P, 5V Logic, 20mA max per GPIO |
| Relay Module | Songle SRD-05VDC-SL-C (Blue) | 5V Coil, 10A/250VAC Contact, ~70mA trigger current |
| Optocoupler | PC817 (Built into module) | Provides galvanic isolation between logic and coil |
| Flyback Diode | 1N4148 or 1N4007 (Built-in) | Dissipates inductive kickback from the relay coil |
| Load | 120V AC Desk Lamp (Resistive) | Max 60W incandescent or 10W LED for this test |
Pin Mapping Table
| Arduino Uno R3 Pin | Relay Module Pin | Wire Color (Recommended) | Function |
|---|---|---|---|
| 5V | VCC | Red | Powers the optocoupler LED and logic side |
| GND | GND | Black | Common ground reference for logic |
| Digital Pin 8 | IN1 | Yellow | Trigger signal (Active LOW) |
| External 5V Source | JD-VCC | Orange | Powers the relay coil (See isolation tip below) |
Step-by-Step Wiring Procedure
- Prepare the Low-Voltage Logic: Connect the Arduino 5V pin to the module's VCC pin. Connect Arduino GND to the module's GND pin. Connect Digital Pin 8 to IN1.
- Configure True Isolation (The JD-VCC Jumper): Locate the small plastic jumper cap on the module bridging
VCCandJD-VCC. Remove it. Connect an external 5V power supply (like a spare USB buck converter) toJD-VCCand the module'sGND. This ensures the high-current relay coil is powered independently of the Arduino's fragile onboard 5V regulator. - Wire the Mains Load (Normally Open): Strip the hot (black/brown) wire of your AC lamp cord. Cut it in half. Connect one end to the relay's COM (Common) terminal and the other end to the NO (Normally Open) terminal. The neutral (white/blue) wire bypasses the relay and connects directly to the lamp.
- Secure Terminals: Ensure no stray copper strands are poking out of the blue screw terminals. Tug-test the wires. A loose mains connection causes arcing and fire.
setup() function to prevent the load from turning on momentarily while the Arduino boots.
Complete Arduino IDE Code
This code targets the Arduino Uno R3 and Nano v3. It includes pin definitions, safe state initialization, and serial error handling to verify GPIO assignment. It uses the Arduino digitalWrite reference standards for active-LOW relay modules.
// Relevador Arduino Control Code
// Target: Arduino Uno R3 / Nano v3
// Module: Active LOW 5V Relay with PC817 Optocoupler
#define RELAY_PIN 8
#define RELAY_ON LOW // Active LOW module
#define RELAY_OFF HIGH // Active LOW module
// Timing variables
const unsigned long ON_DURATION = 5000; // 5 seconds ON
const unsigned long OFF_DURATION = 5000; // 5 seconds OFF
unsigned long previousMillis = 0;
bool relayState = false;
void setup() {
Serial.begin(9600);
// Error handling: Validate pin assignment
if (RELAY_PIN < 0 || RELAY_PIN > 13) {
Serial.println("FATAL ERROR: Invalid RELAY_PIN defined. Halting.");
while(1); // Infinite loop to prevent unsafe toggling
}
// CRITICAL: Initialize pin state BEFORE setting pinMode to prevent boot glitch
digitalWrite(RELAY_PIN, RELAY_OFF);
pinMode(RELAY_PIN, OUTPUT);
Serial.println("Relevador Arduino System Initialized. Relay is SAFE (OFF).");
previousMillis = millis();
}
void loop() {
unsigned long currentMillis = millis();
if (relayState == false) {
if (currentMillis - previousMillis >= OFF_DURATION) {
digitalWrite(RELAY_PIN, RELAY_ON);
relayState = true;
previousMillis = currentMillis;
Serial.println("STATUS: Relay ENGAGED (Load ON)");
}
} else {
if (currentMillis - previousMillis >= ON_DURATION) {
digitalWrite(RELAY_PIN, RELAY_OFF);
relayState = false;
previousMillis = currentMillis;
Serial.println("STATUS: Relay DISENGAGED (Load OFF)");
}
}
}
Debugging: Compilation & Hardware Failures
When your relevador arduino build fails, use this decision tree. Here are the first three things to check: 1) Is the module Active HIGH or Active LOW? 2) Is the Arduino browning out due to USB current limits? 3) Are you wired to NO or NC?
1. Compilation Error: Scope Declaration
Exact Error String: error: 'RELAY_PIN' was not declared in this scope
- Cause: You missed the
#define RELAY_PIN 8at the top of the sketch, or you placed it inside thesetup()function instead of the global scope. - Fix: Move the
#definemacro to the very top of your code, beforevoid setup().
2. Hardware Failure: LED is ON, but No "Click"
Symptom: The red LED on the relay module turns on, but you don't hear the mechanical click, and the load stays off.
- Cause 1 (Most Likely): Insufficient current. If you are powering the Arduino via a low-power USB hub, the 5V rail may sag below 4.5V when the optocoupler and coil try to draw ~90mA combined. The LED lights up (requires only 2mA), but the coil doesn't generate enough magnetic field to pull the armature.
- Cause 2: The JD-VCC jumper is removed, but you forgot to connect the external 5V supply to JD-VCC.
- Fix: Power the Arduino via the barrel jack with a 7-9V wall adapter, or use the external 5V supply trick detailed in the wiring steps.
3. Hardware Failure: Arduino Resets on Click
Symptom: The relay clicks, but the Arduino immediately reboots, and the serial monitor disconnects.
- Cause: Back-EMF spike or USB brownout. When the relay coil de-energizes, it generates a massive reverse voltage spike. If the module lacks a flyback diode, this spike travels back into the Arduino's 5V rail, triggering the ATmega328P's brownout detection (BOD) and resetting the chip.
- Fix: Inspect the module. If there is no diode across the relay coil pins, solder a 1N4007 diode across the coil pins (stripe facing the positive side). Alternatively, implement the JD-VCC isolation jumper to physically separate the coil ground from the Arduino ground.
Extending and Simplifying the Build
How to Simplify (Solid State Relays): If you are switching a resistive load like a heater or an LED array and want to eliminate the mechanical "click" and contact arcing, swap the electromechanical module for a Solid State Relay (SSR) like the Omron G3MB-202P or Fotek SSR-25DA. SSRs use a TRIAC to switch AC loads silently and can be driven directly by Arduino PWM for proportional heating control. Note: SSRs generate heat and require a heatsink for loads >2A. For more on solid-state switching, refer to the SparkFun Relay Tutorial.
How to Extend (Multi-Channel & IoT): To control multiple appliances, upgrade to a 4-channel or 8-channel relay module. However, do not power an 8-channel module directly from the Arduino Uno's 5V pin; the combined coil current (8 x 70mA = 560mA) exceeds the onboard NCP1117 voltage regulator's safe thermal limits. Instead, migrate the logic to an ESP32 DevKit v1 and use the MQTT protocol to trigger the relays remotely via Home Assistant.
Frequently Asked Questions (FAQ)
¿Cómo conectar un relevador arduino a 12V? (How to wire a 12V relay to Arduino?)
You cannot power a 12V relay coil directly from the Arduino's 5V pin; it will not generate enough magnetic force to pull the contact. You must use a 12V relay module that features an optocoupler and a separate coil power input. Wire the Arduino 5V to the module's VCC (logic side), Arduino GND to module GND, and a Digital Pin to IN1. Then, connect an external 12V power supply to the module's JD-VCC (or coil VCC) and the module's ground. The optocoupler will safely bridge the 5V logic and 12V coil domains.
Can a relevador arduino module switch a 1HP water pump?
Technically yes, but practically no. While a 1HP (746W) pump at 120V draws about 6.2A running current (well within the 10A rating of the Songle relay), AC motors have an inrush current (Locked Rotor Amperage) that is 6 to 8 times higher than the running current. This means a 1HP pump can pull 40A+ for a fraction of a second when starting. This will instantly pit and weld the contacts of a standard hobbyist relay shut. For motors >1/4 HP, use the Arduino to trigger a heavy-duty industrial contactor (like an Eaton or Schneider Electric 30A contactor) which is designed to extinguish the massive inductive arc.
Why does my relay module get hot after 10 minutes?
The relay coil is an electromagnet that requires continuous current to hold the mechanical armature closed. A standard 5V Songle relay coil has a resistance of about 70 ohms, meaning it constantly dissipates roughly 0.35 Watts of heat (P = V²/R). In a poorly ventilated 3D-printed enclosure, this heat builds up. If the module is too hot to touch, ensure your enclosure has ventilation slots, or switch to a latching relay or SSR, which do not require continuous coil current to maintain state.
What is the difference between active HIGH and active LOW relevador modules?
This refers to the logic level required to energize the coil. On an Active LOW module (the most common blue boards), the optocoupler LED is wired between VCC and the IN pin. Pulling the IN pin to GND (LOW) completes the circuit and turns on the relay. On an Active HIGH module, the LED is wired between the IN pin and GND; sending 5V (HIGH) turns it on. Always check the silkscreen on your specific module or test it with a multimeter before wiring up your mains load.






