Search

Interfacing of Relay Module with Arduino & Raspberry Pi

What is a Relay?


In layman terms, a relay is a switch.


Technically, a relay is an electromagnetic switch where a small control signal (usually from a microcontroller) at the input of the Relay will control a high voltage supply (usually AC mains).


Commonly used Relay is Single Pole Double Throw (SPDT) Relay, it has five terminals as below:


When there is no voltage applied to the coil, COM (common) is connected to NC (normally closed contact). When there is some voltage applied to the coil, the electromagnetic field produced, which attracts the Armature (lever connected to spring), and COM and NO (normally open contact) get connected, which allows a larger current to flow. Relays are available in many ratings, here we used 6V operating voltage relay, which allows 7A-250VAC current to flow.

  • COM (Common connection): The COM is the center terminal of the relay and it is used in both (Normally open and normally closed) connections.

  • NO (Normally Open): This acts as a switch. In normally open connection, there will be no contact between COM and NO, since it is normally open. But when we will activate the relay module, then it will get connected to the COM and will supply power to the load, which will power up the light. Thus the circuit will initially be in open state until we trigger the state.

  • NC (Normally closed): its behavior is opposite to the normally open connection. It always remains in contact with COM, even when the relay module is not powered. When we will trigger the relay module then it will open the circuit, so the connection is lost.

  • We will use the normally open connection. When you will use the relays in NO (Normally Open) connection and you set the corresponding IN pin to LOW, then the power will flow from the COM connector and out of the normally open connector powering your device.


Interfacing of Arduino with Relay Module-


Components Required for relay module - • Arduino Uno (You can use any other type) • 4 Relay module • 3v Battery • LED’s • 220-ohm resistors • Breadboard

Code of relay module interfacing with micro-controller -


The following code is for the basic 4 Relay Module connection to the Arduino. Each relay will turn on for 5 seconds and then will turn off. You will hear the click sound as there state changes from OFF to ON or from ON to OFF. You will also see the Red LED on the 4 Relay board light up when the relay will on. Necessary comments are made in the code for better understanding. For the 4 input relay module, we will turn on each relay for 5 seconds and then turn it off.


 #define RELAY1 7 //Defining the pin 7 of the Arduino for the 4 relay module
 #define RELAY2 6 //Defining the pin 6 of the Arduino for the 4 relay module
 #define RELAY3 5 //Defining the pin 5 of the Arduino for the 4 relay module
 #define RELAY4 4 //Defining the pin 4 of the Arduino for the 4 relay module

void setup()
 {
 pinMode(RELAY1, OUTPUT); //Defining the pin 7 of the Arduino as output
 pinMode(RELAY2, OUTPUT); //Defining the pin 6 of the Arduino as output
 pinMode(RELAY3, OUTPUT); //Defining the pin 5 of the Arduino as output
 pinMode(RELAY4, OUTPUT); //Defining the pin 4 of the Arduino as output
 }

void loop()
 {
 digitalWrite(RELAY1,LOW); // This will Turn ON the relay 1
 delay(5000); // Wait for 5 seconds
 digitalWrite(RELAY1,HIGH); // This will Turn the Relay Off

digitalWrite(RELAY2,LOW); // This will Turn ON the relay
 delay(5000); // Wait for 5 seconds
 digitalWrite(RELAY2,HIGH); // This will Turn the Relay Off

digitalWrite(RELAY3,LOW); // This will Turn ON the relay
 delay(5000); // Wait for 5 seconds
 digitalWrite(RELAY3,HIGH); // This will Turn the Relay Off

digitalWrite(RELAY4,LOW); // This will Turn ON the relay
 delay(5000); // Wait for 5 seconds
 digitalWrite(RELAY4,HIGH); // This will Turn the Relay Off
 }


Interfacing of Raspberry Pi with Relay Module-


Components Required -

  • Raspberry Pi 3 Model B

  • 2-Channel Relay Module

  • Two Small Incandescent Bulbs (for demonstration in the output)

  • Connecting wires

  • Power Supply

  • Computer

Circuit Design

Connect the inputs to the two relay channels to GPIO16 and GPIO18 of the Raspberry Pi. Then connect the loads as shown in the circuit diagram.


Working


The main concept behind this project is to understand the working and use of a relay and also control a relay using Raspberry Pi.

There is nothing special going in the project. All you need to do is to control the GPIO pins connected to the Relay Module. If the GPIO Pin is made HIGH, the corresponding load will be switched ON.

To turn OFF the load, make the GPIO pin LOW.


Code :

import RPi.GPIO as GPIO
import time

in1 = 16
in2 = 18

GPIO.setmode(GPIO.BOARD)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)

GPIO.output(in1, False)
GPIO.output(in2, False)

try:
    while True:
      for x in range(5):
            GPIO.output(in1, True)
            time.sleep(0.1)
            GPIO.output(in1, False)
            GPIO.output(in2, True)
            time.sleep(0.1)
            GPIO.output(in2, False)
      
      GPIO.output(in1,True)
      GPIO.output(in2,True)

      for x in range(4):
            GPIO.output(in1, True)
            time.sleep(0.05)
            GPIO.output(in1, False)
            time.sleep(0.05)
      GPIO.output(in1,True)

      for x in range(4):
            GPIO.output(in2, True)
            time.sleep(0.05)
            GPIO.output(in2, False)
            time.sleep(0.05)
      GPIO.output(in2,True)

except KeyboardInterrupt:
    GPIO.cleanup()


Written by

Shubham Rattra

University Institute of Engineering and Technology, Panjab University



0 views

Technomekanics

Technomekanics was incorporated in 2019 by passion driven techies who are willing to contribute into the society.  We are specialist in the field of IoT & Electronics with an extensive team of competent research engineers and developers we are proficient in all domains related to the art of Robotics: AI & ML, IoT and software development. To do so we are providing a technical platform where one can get exposure about current industrial advancements, inbuilt their mindset, develop their ideas into reality and create a impact.

To get a call from our expert, fill the info below

Our Address

Duarachuk, Near Dibrugarh University

Dibrugarh- 786004

Assam

  • WhatsApp
  • LinkedIn Social Icon
  • Facebook
  • YouTube
  • Instagram

© 2020 Technomekanics, All Rights Reserved.

  Created with       from Assam