Relay Module Connection in Arduino: Code and Wiring Diagram

A relay module using Arduino is an electronically operated switches that can let electricity pass through or not. These modules can be operated by an Arduino to control other modules.

In this article, you will learn how to wire and code a relay module. To demonstrate the capability of the module, you will control a normal LED bulb.

Relay Module Connection in Arduino: Steps in Creating the Device

Here are the steps in creating Temperature Monitoring System using Arduino..

  1. Gathering the Components

    The first thing to do is to collect the hardware components for the Arduino device.
    Arduino Uno
    Single Channel Relay Module
    LED light bulb connected to a wall socket

  2. Connecting the Components

    Connect the components to the Arduino Uno. Please refer to the wiring diagram below.

  3. Coding the Arduino

    Third step is about coding the Arduino device to work with the components.

  4. Upload the Sketch

    Lastly, upload the sketch to the Arduino.

Relay Module Connection in Arduino: Detailed Explanation

To start this project, you need the following:

QtyComponent
1Arduino UNO
1Single Channel Relay Module
1LED bulb

Arduino UNO

For this project, we will be using an Arduino Uno microprocessor board. Arduino Uno is suitable for any projects and is the cheapest and widely used microprocessor board in the Arduino family. This is great for all kinds of IoT projects.

Arduino Uno
Arduino Uno

Single Channel Relay Module

This project uses a single channel relay module. There are other relay modules available. You can use 2-channel or 3-channel, it simply depends on the project requirements. The single relay module below is rated to switch a single high-powered device from the Arduino. It can switch up to 10 amperes with 250 volts of AC or 30 volts of DC.

This module has 3 pins – Signal, Power, and Ground. The output terminals are COM, NO, and NC.

The middle terminal is the COM. This is where you connect signal you want to switch (example: electricity).

The bottom terminal is the Normally Closed terminal. This terminal is used if you want to turn off your relay by default. On the other hand, the top terminal is the Normally Open terminal. This is the opposite of the other terminal. You can connect here if you want your relay to be open by default.

PLEASE BE CAREFUL IN WIRING. HIGH POWERED ELECTRICITY IS INVOLVED.

Schematic Diagram

The red wire of the lamp is connected to the COM terminal and to NO terminal of the relay. The relay itself is powered using the 5V power of the Arduino. Signal pin of the relay is connected to the digital pin 6 of the Arduino.

Relay Module Wiring Diagram
Relay Module Wiring Diagram

AGAIN, PLEASE BE CAREFUL IN WIRING. HIGH POWERED ELECTRICITY IS INVOLVED.

Code

For the code, first is to declare the data pin of the module. Then for the setup, simply set the pin mode of the declared variable to OUTPUT. In the loop function, digitalWrite function is used to turn on or off the relay module. with a 3-second delay.

int RelayPin = 6;

void setup() {
  // Set RelayPin as an output pin
 pinMode(RelayPin, OUTPUT);
}

void loop() {
  // Let's turn on the relay...
  digitalWrite(RelayPin, LOW);
  delay(3000);
  
  // Let's turn off the relay...
  digitalWrite(RelayPin, HIGH);
  delay(3000);
}

Connect your lamp to a wall socket, Double-check the wiring. PLEASE BE CAREFUL IN WIRING THE PROJECT. After that, upload the code to the Arduino.

Output

Successful upload will result to a the lamp turning on and off again for 3 seconds.

Relay module controlling a lamp.
Relay module controlling a lamp.

Summary

So there you have it – a Relay Module Connection in Arduino. This device is simple to create and can be used in other projects as well. You can always add more sensors to the device, upgrading it to a much-sophisticated device.

Download

Click the button below to download the source code.

Inquiries

Feel free to write your questions about the project at the comments below.

Leave a Comment