IOT

How To Connect Raspberry Pi 4 To Relay

Home automation is one of the most popular DIY projects among hobbyists and electronics enthusiasts. Controlling an electrical load, such as a light bulb or a ceiling fan, is a part of such home automation projects.

The fundamental idea behind Raspberry Pi Home Automation (or any other platform like Arduino) is to use the Raspberry Pi to control various electrical loads. To do so, you’ll need to know how to manage a relay using a Raspberry Pi, as the relay is the most important component in managing electrical loads. In this project, we will learn about Relay and Relay Module,how to interface a Relay with Raspberry Pi and see how to control a Relay using Raspberry Pi. This project could be the initial step toward establishing your own Raspberry Pi-based home automation project.

A relay designed as an electrically operated switch. Many relays operates with an electromagnet to mechanically operate  a switch. But other operating principles also used, like solid-state relays. Relays functions where it’s necessary to regulate a circuit by a low-power signal. This peformed with complete electrical isolation between control and controlled circuits, or several circuits that should controlled by one signal. Relays mainly used for  circuit components preferred in telecom applications, gas detector circuits, RF control applications, Infrared control applications, small volume circuits (gum relay) etc.

Hardware Required

  1. Four-channel 5V Relay Module
  2. Raspberry pi 4 
  3. A Few Jumper cables 
  4. Level converter 

Raspberry pi 4

Raspberry Pi 4 Model B designed as the latest product in the popular Raspberry Pi range of computers. This model features ground-breaking increases in processor speed, multimedia performance, memory. And offers connectivity compared to the predecessor Raspberry Pi 3 Model B+ while retaining backward compatibility and similar power consumption. 

Pi 4 B is an upgraded version with High-Performance Quad-Core 64-bit Broadcom 2711, Cortex A72  processor clocked at 1.5GHz speed. This model consumes 20% less power or offers 90% greater performance than its old version. This hardware developed for better performance not only the loading time with all-new 1GB/2GB and 4GB LPDDR4 SDRAM variants. But also in connectivity with Dual-band 2.4GHz and 5GHz, 802.11 b/g/n/ac wireless LAN and PoE capability via a separate PoE HAT.

Four-channel 5V Relay Module

The 4 channel 5v interface board is designed to control high voltage, high current load devices. In this relay interface board, each channel requires a 15-20mA driver current. This module is equipped with a high current relay of 10A @250 V AC/10A@ 30V DC. It consists of a standard interface that can be controlled by the microcontroller directly. And also led indication provided to indicate the status of the relay. The relay terminal Common(C), normally open (NO), normally closed NC) accessed through the screw terminals thus making wiring much easier. In this module, each relay can be ON/OFF by an optoisolated input that can be controlled by a microcontroller output pin.

This 5V 4-Channel Relay interface board is used to control various appliances and other equipment which requires a large current.. When the input digital signal switches to high, the relay switched ON (closed), else remains to switch  OFF (opened). This module is also used to control various applications and equipment with a large current compliant with international safety.

Switching Devices with Raspberry Pi

If you’ve ever worked with a Raspberry Pi, you’re familiar that it has a GPIO, or general purpose input/output port pin.

The 40-pin GPIO, as shown in the image above, can be used to connect a wide range of output devices.  Raspberry Pi being a digital computer, can generate logic outputs on the GPIO pins. These logic output is called as logic 0 and logic 1. In practise, when writing a software to write logic 0 on a Raspberry Pi pin, the pin displays a 0 volt or GND potential. Similarly, writing logic 1 on the pin results in a +3.3V on the Raspberry Pi pin.

We can create logic 0 (gnd) or logic 1 (+3.3v) on any output pin of the Raspberry Pi by writing a programme. However, this output voltage is insufficient to power any physical devices. In fact, the 3.3V generated on the output pin is current limited, thus it can only deliver 20 mA from that pin. It means, we can interface an LED directly to raspberry pi gpio pin, and thats about it. You can’t connect any other output device directly to the pin of raspberry pi, not even a DC motor. So connecting an AC Device needs something different.

Interfacing Diagram Of The Relay Module With The Raspberry Pi GPIO

Because the Raspberry Pi only generates 3.3 volts on its GPIO pins, you may not be able to power a 5 volt relay module with it. The adoption of level converters, which are nothing more than MOSFETs that convert low-level 3.3V to 5V signals, is the solution to this problem.

Raspberry Pi 4 Relay python

Python Code For Controlling The Relay Module Using Raspberry Pi 

The first thing in this code is to import the GPIO module and the time module, so let’s do it. Here I have used the time module to add a delay between scripts.

import RPi.GPIO as GPIO 

from time import sleep 

After that, you must inform the board about the numbering technique you want to utilise. Please see the coding section of this blog for further information on the numbering strategy. The BCM numbering scheme is used in this code.

GPIO.setmode(GPIO.BCM) 

The next step is pin initialization; you can initialise any GPIO pin based on your application’s needs, however I’ve utilised the following pins in this code.

Relay1_GPIO = 12

Relay2_GPIO = 7

Relay3_GPIO = 8

Relay4_GPIO = 25

The GPIO pin’s mode will be defined next. In this situation, we’re using the Raspberry Pi 4 GPIO to power an external device. This indicates that the value is being output, hence the GPIO pin will be designated as the OUTPUT pin in this case.

GPIO.output(Relay1_GPIO, GPIO.LOW)

sleep(1)

GPIO.output(Relay1_GPIO, GPIO.HIGH)

sleep(1)

GPIO.output(Relay1_GPIO, GPIO.LOW)

sleep(1)

GPIO.output(Relay1_GPIO, GPIO.HIGH)

sleep(1)

GPIO.output(Relay1_GPIO, GPIO.LOW)

sleep(1)

GPIO.output(Relay1_GPIO, GPIO.HIGH)

sleep(1)

GPIO.output(Relay1_GPIO, GPIO.LOW)

sleep(1)

GPIO.output(Relay1_GPIO, GPIO.HIGH)

sleep(1)

ConclusionIn this tutorial, we learnt how to interface relay modules with the Raspberry Pi 4 and how to communicate with relay modules using Python code.Hope you all have understand about the post.Will be back soon with more informative blogs only @ MATHA ELECTRONICS

Leave a Reply

Your email address will not be published.