To control a 5V relay with an Arduino Uno R3, connect the module's VCC to the Arduino's 5V pin, GND to GND, and the IN pin to a digital GPIO (like D8). Use a flyback diode (usually built-in on standard modules) and never switch inductive loads without proper snubber circuits. The standard blue 5V relay module uses a Songle SRD-05VDC-SL-C relay, which draws about 71mA to energize the coil. Because the Arduino Uno's ATmega328P can source up to 40mA per pin (with a 200mA total package limit), you must use the module's onboard NPN transistor to drive the coil, rather than powering the coil directly from the GPIO pin.

Parts List and Module Specifications

Before wiring, verify your exact hardware variants. Using a 12V relay module on a 5V Arduino logic supply will result in a failure to latch, while using a raw relay without a driver transistor will brownout your microcontroller.

  • Microcontroller: Arduino Uno R3 (Rev3, ATmega328P DIP or SMD) - ~$25 (official) or ~$12 (clone).
  • Relay Module: 1-Channel 5V Relay Module with optocoupler (Songle SRD-05VDC-SL-C) - ~$3.
  • Wiring: Dupont Male-to-Female and Male-to-Male jumper wires (22 AWG stranded) - ~$5.
  • Power (Optional): External 5V 2A buck converter or USB power bank for isolated switching - ~$8.

Songle SRD-05VDC-SL-C Spec Sheet

Parameter Value Notes
Coil Voltage 5V DC Nominal; operates down to ~3.75V
Coil Resistance 70 Ω Draws ~71mA at 5V
Contact Rating (Resistive) 10A @ 120VAC / 10A @ 28VDC Derate for inductive/motor loads
Operate Time 10 ms Maximum pull-in time
Release Time 5 ms Maximum drop-out time
Electrical Life 100,000 operations At rated resistive load

Pin Mapping and Isolated Wiring Steps

Standard 5V relay modules feature a jumper labeled JD-VCC. This jumper determines whether the relay coil shares a ground with the Arduino (non-isolated) or uses a separate power supply (optically isolated). For maximum safety when switching mains AC, use the isolated configuration.

Pin Mapping Table

Arduino Uno R3 Pin Relay Module Pin Function
5V VCC Powers the optocoupler LED (Input side)
GND GND Reference ground for optocoupler LED
D8 (Digital Pin 8) IN Logic signal to trigger optocoupler
External 5V (+) JD-VCC Powers the relay coil (Output side)
External 5V (-) GND (Center pin) Ground for relay coil (Output side)

Wiring Steps (Optically Isolated)

  1. Remove the JD-VCC Jumper: Pull the yellow or black plastic jumper cap off the JD-VCC and VCC pins on the relay module. This breaks the physical connection between the input logic side and the output coil side.
  2. Wire the Input Side: Connect Arduino 5V to the module's VCC pin. Connect Arduino GND to the module's GND pin (the one on the edge, next to VCC and IN). Connect Arduino D8 to the IN pin.
  3. Wire the Output Side: Connect your external 5V power supply positive terminal to the JD-VCC pin. Connect the external 5V negative terminal to the module's center GND pin (next to JD-VCC).
  4. Wire the Load: Connect your AC or DC load to the COM (Common) and NO (Normally Open) terminals on the blue relay block. Never connect the load to the module's logic pins.
Callout Tip: The IN pin on most 5V relay modules is active LOW. This means writing LOW to the GPIO pin turns the relay ON, and writing HIGH turns it OFF. This is because the optocoupler LED cathode is tied to the IN pin, and current flows when the pin is pulled to ground.

Complete Arduino Code (Uno R3)

This code targets the Arduino Uno R3 (ATmega328P). It includes a board-check preprocessor directive to prevent compilation on incompatible 3.3V boards (which would damage the GPIO if wired directly without level shifting), non-blocking timing using millis(), and serial state verification.

#if !defined(__AVR_ATmega328P__)
#error "Board mismatch: This code targets the ATmega328P (Uno R3). Select the correct board in the IDE."
#endif

// Pin Definitions
#define RELAY_PIN 8
#define LED_BUILTIN 13

// Timing Variables (Non-blocking)
unsigned long previousMillis = 0;
const long interval = 5000; // 5 seconds
bool relayState = false;

void setup() {
  Serial.begin(9600);
  
  // Initialize pins
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  
  // Set initial state to OFF (Active LOW relay: HIGH = OFF)
  digitalWrite(RELAY_PIN, HIGH);
  digitalWrite(LED_BUILTIN, LOW);
  
  Serial.println("System Initialized. Relay is OFF.");
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    
    // Toggle state
    relayState = !relayState;
    
    // Apply to hardware (Active LOW)
    if (relayState) {
      digitalWrite(RELAY_PIN, LOW);  // Turn ON
      digitalWrite(LED_BUILTIN, HIGH);
      Serial.println("Command: Relay ON");
    } else {
      digitalWrite(RELAY_PIN, HIGH); // Turn OFF
      digitalWrite(LED_BUILTIN, LOW);
      Serial.println("Command: Relay OFF");
    }
    
    // Error Handling: Verify pin state matches expected state
    verifyRelayState();
  }
}

void verifyRelayState() {
  int actualPinState = digitalRead(RELAY_PIN);
  int expectedPinState = relayState ? LOW : HIGH;
  
  if (actualPinState != expectedPinState) {
    Serial.print("ERROR: Relay state mismatch! Expected ");
    Serial.print(expectedPinState);
    Serial.print(" but read ");
    Serial.println(actualPinState);
  } else {
    Serial.println("Verification: Pin state matches expected logic.");
  }
}

Debugging: When the Relay Fails to Click

When a 5V relay Arduino circuit fails, the issue is almost always power delivery or logic isolation. Before replacing the module, check these first three things:

  1. The JD-VCC Jumper Position: If you removed the jumper for isolation but forgot to wire an external 5V supply to the JD-VCC pin, the relay coil has no power. The optocoupler LED will light up, but the relay will not click.
  2. USB Port Current Limiting (Brownout): A standard PC USB port limits current to 500mA. The Arduino Uno's onboard regulator and the relay coil combined can draw 150mA+. If the voltage drops below 4.5V, the ATmega328P will brownout and reset. Check your serial monitor for continuous reboot loops.
  3. Logic Level Mismatch: If you are using an active-LOW module but your code initializes the pin as HIGH in setup without immediately pulling it high, the relay may trigger momentarily on boot. Always set digitalWrite(RELAY_PIN, HIGH) before calling pinMode(RELAY_PIN, OUTPUT) to prevent boot-spikes.

Compilation Error: #error "Board mismatch..."

If your IDE throws this exact error string: #error "Board mismatch: This code targets the ATmega328P (Uno R3). Select the correct board in the IDE."

Ranked Causes:

  1. Wrong Board Selected: You have an ESP32, Arduino Mega, or Nano Every selected in the IDE Tools menu. The preprocessor check caught it to prevent 3.3V GPIO damage.
  2. Missing Core Package: If using a clone Uno with an ATmega16U2/CH340, ensure you have the standard Arduino AVR Boards package installed via the Boards Manager.

Hardware Fault: Relay Chatters or Buzzes

If the relay makes a rapid buzzing sound instead of a solid click, the coil is receiving insufficient voltage or the flyback diode has failed. Measure the voltage across the JD-VCC and GND pins on the output side while the relay is engaged. If it reads below 4.2V, your power supply is sagging. Upgrade to a dedicated 5V 2A switching supply. For deeper insights into inductive kickback and diode selection, refer to the Texas Instruments Application Note on Relay Driving.

Extending and Simplifying the Build

Once you have a single 5V relay Arduino circuit working, you will likely want to scale it up or reduce its footprint.

How to Extend: Driving Multiple Relays

The ATmega328P has limited GPIO pins and a strict 200mA total package current limit. If you need to drive four or eight relays, do not wire them directly to the Arduino's 5V rail. Instead, use a ULN2803A Darlington Transistor Array. This single DIP-18 IC contains eight open-collector Darlington pairs with built-in flyback diodes. It costs about $1.50, handles up to 500mA per channel, and allows you to drive heavy relay coils safely while protecting your microcontroller. For AC inductive loads (like motors or transformers), add an RC snubber network (e.g., 100Ω resistor in series with a 0.1µF X2 capacitor) across the COM and NO terminals to suppress voltage spikes and protect the relay contacts from pitting.

How to Simplify: Solid State Relays (SSR)

If the mechanical clicking noise is undesirable, or you need to switch a load thousands of times per minute (PWM), replace the mechanical module with a 5V Solid State Relay (SSR) like the Omron G3MB-202P. SSRs use a TRIAC or MOSFET to switch the load optically. They have no moving parts, switch at the zero-crossing point (reducing EMI), and draw only ~10mA from the Arduino GPIO. Note that the G3MB-202P is limited to 2A at 120VAC and requires a heatsink for continuous loads above 1A. For more on digital pin current sourcing, consult the Arduino Digital Pins Documentation.

Frequently Asked Questions

Can I power a 5V relay module directly from the Arduino Uno 5V pin?

Yes, but only for a single 1-channel module. The relay coil draws ~71mA, and the optocoupler LED draws ~10mA, totaling ~81mA. The Arduino Uno's onboard 5V regulator (typically an NCP1117 or similar) can supply up to 800mA, but it must also power the ATmega328P and any attached sensors. If you use a 2-channel or 4-channel module, the combined current draw (160mA to 320mA) will cause the onboard regulator to overheat and shut down. For multi-channel modules, always use an external 5V power supply wired to the JD-VCC pin.

Why does my 5V relay Arduino circuit reset when the relay switches?

This is a classic brownout caused by inductive kickback or voltage sag. When the relay coil de-energizes, it generates a reverse voltage spike. If the module's flyback diode is missing, oriented backward, or too slow, this spike couples back into the Arduino's 5V rail, resetting the microcontroller. Additionally, if you are powering the relay from the Arduino's USB port, the sudden 71mA current draw when the coil engages can drop the USB voltage below the ATmega328P's brownout detection threshold (typically 2.7V to 4.0V depending on fuse settings). Add a 470µF electrolytic capacitor across the 5V and GND rails near the Arduino to absorb transient sags.

How do I use a 5V relay module with a 3.3V ESP32 or Raspberry Pi?

Standard 5V relay modules require a 5V logic HIGH to fully turn off the optocoupler, and a 3.3V GPIO often fails to reliably trigger the active-LOW input because the voltage difference isn't enough to forward-bias the internal LED. To fix this without a logic level shifter, power the module's VCC pin with 3.3V (from the ESP32/Pi) and power the JD-VCC pin with 5V (from an external supply). This allows the 3.3V GPIO to pull the input pin to GND (0V), creating a 3.3V differential across the optocoupler LED, which is sufficient to trigger it safely without overvolting the 3.3V microcontroller.