DEV Community

Cover image for Relay Module Interfacing with Arduino: Wiring, Code & Complete Beginner Guide
chip.pk
chip.pk

Posted on

Relay Module Interfacing with Arduino: Wiring, Code & Complete Beginner Guide

Relay Module Interfacing with Arduino: Wiring, Code & Complete Beginner Guide (2026)

A Relay Module is one of the most useful components for Arduino beginners. It allows a low-voltage microcontroller like the Arduino Uno to safely control high-voltage and high-current devices such as lights, fans, pumps, motors, and other electrical appliances.

Since Arduino digital pins can only provide a small amount of current, they cannot directly power large electrical loads. A relay module solves this problem by acting as an electrically isolated switch between the Arduino and the external device.

In this beginner-friendly guide, you'll learn:

  • What a Relay Module is
  • Relay Module pinout
  • Arduino wiring
  • Complete Arduino code
  • Code explanation
  • High-Level vs Low-Level Trigger
  • Safety tips
  • Practical applications

Whether you're building your first Arduino project or exploring home automation, this guide will help you understand relay modules from the ground up.

What is a Relay Module?

A Relay Module is an electrically operated switch that allows an Arduino to control another electrical circuit safely.

Instead of sending power directly from the Arduino, the Arduino only sends a small control signal to activate the relay. The relay then opens or closes a separate circuit capable of handling much higher voltages and currents.

Relay Modules are commonly used in:

  • Home Automation
  • Smart Lighting
  • Water Pump Controllers
  • Industrial Automation
  • Robotics
  • IoT Systems
  • Security Systems
  • Motor Control

Components Required

You'll need the following components:

Component Quantity
Arduino Uno R3 1
5V Relay Module 1
USB Cable 1
Jumper Wires 3
Breadboard (Optional) 1

Understanding the Relay Module

Before connecting the relay, it's important to understand what each terminal does.

Arduino Control Pins

VCC

Provides 5V power to the relay module.

GND

Common ground connection.

IN

Receives the control signal from the Arduino.

Relay Output Terminals

COM

Common terminal used to connect the electrical load.

NO (Normally Open)

COM connects to NO only when the relay is activated.

This is the most commonly used terminal for turning appliances ON only when required.

NC (Normally Closed)

COM remains connected to NC while the relay is OFF.

This terminal is used when a device should stay ON until the relay is activated.

Arduino to Relay Module Wiring

Connecting a relay module to an Arduino Uno only requires three wires.

Arduino Uno Relay Module
5V VCC
GND GND
Digital Pin 7 IN

For switching an external load:

Power Source
      │
     COM
      │
     Relay
      │
      NO
      │
   Electrical Load
Enter fullscreen mode Exit fullscreen mode

When the Arduino activates the relay, COM connects to NO, allowing current to flow through the connected device.

How the Relay Works

Inside every relay is an electromagnetic coil.

When Arduino outputs a HIGH signal:

  • The relay coil becomes energized.
  • An internal mechanical switch moves.
  • COM connects to NO.
  • Current flows through the external circuit.
  • The connected device turns ON.

When Arduino outputs LOW:

  • The coil is de-energized.
  • The internal switch returns to its default position.
  • COM disconnects from NO.
  • The connected device turns OFF.

You will usually hear a small clicking sound whenever the relay switches.

Arduino Relay Code

const int relayPin = 7;

void setup()
{
    pinMode(relayPin, OUTPUT);
}

void loop()
{
    digitalWrite(relayPin, HIGH);
    delay(1000);

    digitalWrite(relayPin, LOW);
    delay(1000);
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation

Let's understand how the Arduino program works.

Step 1

const int relayPin = 7;
Enter fullscreen mode Exit fullscreen mode

Stores the relay control pin number.

Step 2

pinMode(relayPin, OUTPUT);
Enter fullscreen mode Exit fullscreen mode

Configures Digital Pin 7 as an output.

Step 3

digitalWrite(relayPin, HIGH);
Enter fullscreen mode Exit fullscreen mode

Activates the relay.

Step 4

delay(1000);
Enter fullscreen mode Exit fullscreen mode

Keeps the relay ON for one second.

Step 5

digitalWrite(relayPin, LOW);
Enter fullscreen mode Exit fullscreen mode

Turns the relay OFF.

The loop() function repeats continuously, creating an ON/OFF switching cycle.

High-Level vs Low-Level Trigger

Not all relay modules behave the same way. Depending on the design of the module, the relay may activate when the Arduino outputs either a HIGH signal or a LOW signal.

Understanding the trigger type is important because it determines how your Arduino code controls the relay.

High-Level Trigger

A High-Level Trigger relay module activates when the Arduino sends a HIGH signal.

HIGH  → Relay ON
LOW   → Relay OFF
Enter fullscreen mode Exit fullscreen mode

This is the most common type found in beginner Arduino kits.

Low-Level Trigger

A Low-Level Trigger relay module activates when the Arduino sends a LOW signal.

LOW   → Relay ON
HIGH  → Relay OFF
Enter fullscreen mode Exit fullscreen mode

Some relay modules use this design because it offers better compatibility with industrial controllers and certain microcontroller platforms.

If your relay behaves opposite to the example code, simply reverse the HIGH and LOW values in your program.

Safety Tips

Although relay modules allow Arduino to switch high-voltage devices safely, incorrect wiring can still be dangerous.

Always follow these basic precautions:

  • Disconnect power before changing any wiring.
  • Never touch exposed AC wiring while power is connected.
  • Double-check all COM, NO, and NC connections.
  • Ensure the relay's voltage and current ratings match the connected load.
  • Use insulated wires and proper terminal connections.
  • Beginners should first test their project with an LED or low-voltage DC load.
  • Install relay modules inside a protective enclosure for permanent projects.

Warning: If you are not experienced with AC mains wiring, seek assistance from a qualified electrician before connecting household electrical appliances.

Common Troubleshooting

Relay Doesn't Click

Possible causes:

  • No 5V supply
  • Incorrect wiring
  • Wrong Arduino pin selected
  • Faulty jumper wire

Solution:

Verify all wiring and confirm that the relay receives a stable 5V supply.

Relay Clicks but Appliance Doesn't Turn ON

Possible causes:

  • Incorrect COM and NO wiring
  • Loose terminal connection
  • External power source disconnected

Solution:

Reconnect the appliance using the COM and NO terminals and verify the external power supply.

Relay Always Stays ON

Possible causes:

  • Low-Level Trigger relay module
  • Incorrect program logic

Solution:

Check the trigger type of your relay module and modify the code accordingly.

Relay Turns ON and OFF Randomly

Possible causes:

  • Unstable power supply
  • Loose jumper wires
  • Electrical noise

Solution:

Use a stable 5V supply and secure all wiring connections.

Practical Applications

Relay modules are widely used in embedded systems and automation projects.

Popular applications include:

  • Home Automation
  • Smart Lighting
  • Water Pump Controllers
  • Automatic Irrigation Systems
  • Smart Door Locks
  • Security Systems
  • Fan and Motor Control
  • Industrial Automation
  • Robotics
  • IoT Remote Switching

Once you understand relay operation, you can combine it with sensors, timers, Wi-Fi modules, and cloud services to build intelligent automation systems.

Frequently Asked Questions

Can Arduino directly control a 220V appliance?

No.

Arduino GPIO pins operate at only 5V and cannot safely switch AC mains voltage. A relay module provides electrical isolation between the Arduino and the high-voltage circuit.

What does COM mean?

COM stands for Common.

It connects to either NO or NC depending on the relay state.

What is Normally Open (NO)?

NO remains disconnected until the relay is activated.

What is Normally Closed (NC)?

NC remains connected to COM while the relay is inactive.

Can ESP32 control this relay module?

Yes.

Most 5V relay modules can be controlled by ESP32 development boards, provided the trigger logic and voltage compatibility are suitable.

Can Raspberry Pi control a relay module?

Yes.

Many relay modules work with Raspberry Pi GPIO pins. Always verify voltage compatibility before connecting.

Why does the relay click but nothing happens?

This usually indicates incorrect COM, NO, or NC wiring or an issue with the external power supply.

What is JD-VCC?

JD-VCC is a separate power input available on some relay modules. It allows the relay coil to use an independent power supply, improving electrical isolation and reducing electrical noise.

Final Thoughts

Learning to interface a relay module with Arduino is one of the most important milestones for anyone beginning embedded systems and electronics.

It introduces key concepts such as electrical isolation, digital switching, and hardware interfacing while providing a foundation for home automation, robotics, industrial control, and IoT projects.

After mastering this project, you can expand your skills by combining relay modules with sensors, Wi-Fi-enabled ESP32 boards, Raspberry Pi, timers, and cloud platforms to build intelligent automation systems.

Whether you're a student, hobbyist, or professional engineer, understanding relay modules will help you design safer and more capable electronics projects.

Continue Learning

If you enjoyed this tutorial, explore the complete resources below.

🌐 Original Article (Chip.pk)

📘 GitHub Repository

🛠 Hackster.io Project

✍️ Medium Article

⭐ If this guide helped you, consider following Chip.pk for more tutorials on Arduino, ESP32, Raspberry Pi, embedded systems, robotics, IoT, and electronics projects.

Chip.pk – Imagine • Build • Innovate

Top comments (0)