Raspberry Pi and a Relay Module | How to Control a Relay using Raspberry Pi!


We have seen few IoT projects in my previous tutorials like Lighting LED with Raspberry PiHome Security email alert using Raspberry PiSecurity System Using Raspberry Pi With Motion Detection Camera in which we have not yet used any AC devices. We have controlled DC devices in the home automation projects. I would like to share home automation's for AC devices in my coming tutorials, however before that I would like to introduce you to the device 5 Volt Relay Module which is an necessary part to control AC device as Raspberry Pi  cannot control high voltage . In this section we will understand how to control Relay switch via GPIO, which is the first step of interface a Relay with Raspberry Pi and see how to setup a 5V Relay with the Raspberry Pi.

What is a Relay?

A relay is an electrically operated switch.Relays control one electrical circuit by opening and closing contacts in another circuit.Many relays use an electromagnet to mechanically operate a switch, but other operating principles are also used, such as solid-state relays.Relays are used where it is necessary to control a circuit by a separate low-power signal where the output is a high voltage, or where several circuits must be controlled by one signal.In our IoT home automation projects using Raspberry Pi it can control ow voltage devices of 3.3 V, however when we speak about 220 V of AC Raspberry Pi cannot handle it. So we uses a Relay switch which have Normally Open (NO) and Normally Closed (NC) Pins which is controlled by low voltage. Refer the below diagram to understand Raspberry Pi Relay Control.

1) Normally Open (NO) : The circuit is disconnected when the relay is inactive.

2) Normally Closed (NC) : The circuit is connected when the relay is inactive.

Relay

The relay switch is utilized by means of a low-voltage to control the AC connected in NO or NC pins. Since the Pi only tolerates a maximum of 5V and the GPIOs only 3.3V without relays the Pi could burn out if you use direct connections to GPIO pins. We will not controlling ac with raspberry pi in this relay raspberry pi tutorial tutorial however we will be controlling led with relay and raspberry pi.

How to use a relay with a Raspberry Pi:

This will be the base for your home automation using relay and raspberry pi, as part of home automation's you may want to control an electrical load like a light bulb or a ceiling fan or a water pump etc. Once you know the control of Relay we will implement the same in the upcoming projects.

The components needed:

  1. Raspberry Pi
  2. 5V Relay Module
  3. LED
  4. Connecting wires
  5. Power supply for Pi
  6. Bread Board
  7. Resistor -220 ohm

Connection:

The connections is pretty simple refer the below simple Relay circuit. Connect the VCC of the Relay to GPIO Pin 2 and Relay GND to GPIO 39 which is GND in Pi. Connect the Raspberry Pi GPIO Pin 19 to Relay IN pin which will control the NO and NC connection of Relay .

Connect the 5 V from GPIO Pin 2 to the Normally Open connector in Relay and connect the Common port in Relay to the LED Anode. Connect the 220 Ohm resistor from cathode and other end of the resistor to the GND.



Lets write a simple script to bring the GPIO PIN 19 to high and low so that Relay will get activated when the voltage is high.

Raspberry pi relay python script:

import time

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(19, GPIO.OUT)
GPIO.output(19, GPIO.HIGH)

time.sleep(1)
GPIO.output(19, GPIO.LOW)

time.sleep(1)

GPIO.output(19, GPIO.HIGH)

time.sleep(1)
GPIO.cleanup()