Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

In this Raspberry Pi tutorial, you are going to learn about Raspberry Pi relay control. We will control the AC as well as DC device. Controlling the AC devices is dangerous because we will be using the high voltage so before controlling the AC device, we will control the DC device so that you have an idea of how the relay module is working.

If you are a beginner in electronics, then i will recommend you to not follow the AC control part.

Relay Module

A relay is basically a switch which is operated by an electromagnet. The electromagnet requires a small voltage to get activated which we will give from the Raspberry Pi and once it is activated, it will pull the contact to make the high voltage circuit.

The relay module we are going to use is the SRD-05VDC-SL-C and it runs on 5V. The Raspberry Pi can control the devices which runs on up to 3.3V so if we want to control the devices which runs on more than 3.3V or the A.C devices then we will have to use a relay module through which we can control A.C as well as DC devices.

Pin out of Relay Module

The relay module has total of six pins: three on one side and three on other side. On the bottom side, there are three pins which are signal or IN1, 5V and ground. We will connect these pins with the Raspberry Pi. While on the other side, there are NC (Normally close), C (Common) and the NO (normally open) which are the output pins of the 5V relay. There, we will connect the output device.

Normally open state (NO) VS Normally closed state (NC)

The relay module can be used in two states which are

  1. Normally open state (NO)
  2. Normally closed state (NC)

Normally open (NO)

In the normally open state, the initial output of the relay will be low when it will be powered. In this state, the common and the normally open pins are used.

Normally closed state (NC)

In the normally closed state, the initial output of the relay will be high when it will be powered. In this state, the common and the normally close pins are used.

Controlling DC devices using relay module

Firstly, we are going to control a led using the relay because controlling a DC device is easy as compared to the AC device and you will get an idea of how the relay module is working. For controlling the DC device, you do not require an external supply until you are controlling a small voltage device like LED which runs on up to 3.3V.

Circuit Diagram and Explanation

In this example, we will connect the relay module with Raspberry Pi in the normally open state. So, connect the 5V and the ground of the Raspberry Pi with the 5V and the ground of the relay module. Then connect the IN1, IN2, IN3 and IN4 to the GPIO 26, 19, 13, 6 of the Raspberry Pi respectively.

On the other side of the relay module, we will use the common pin and the normally open pin because we are going to connect the relay in the normally open state. So, connect the GPIO 21 of Raspberry Pi to the NO of relay module (K1 relay) and connect the common of the relay module (K1 relay) to the positive lead of the LED. Connect the other pin of LED to the ground through the 220 ohm resistor.

Then similarly connect the GPIO 20 of Raspberry Pi to the NO of relay module (K2 relay) and connect the common of the relay module (K2 relay) to the positive lead of the LED. Connect the other pin of LED to the ground through the 220 ohm resistor.

Then similarly connect the GPIO 16 of Raspberry Pi to the NO of relay module (K3 relay) and connect the common of the relay module (K3 relay) to the positive lead of the LED. Connect the other pin of LED to the ground through the 220 ohm resistor.

Then similarly connect the GPIO 12 of Raspberry Pi to the NO of relay module (K4 relay) and connect the common of the relay module (K4 relay) to the positive lead of the LED. Connect the other pin of LED to the ground through the 220 ohm resistor.

In the end, make sure you have grounded all the LED’s (black wire from the resistors to the ground pin of Raspberry Pi).

raspberry pi relay control

Python Code

import RPi.GPIO as GPIO
from time import sleep

relay_pins = [26, 19, 13, 6]
led_pins = [21, 20, 16, 12]

GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pins, GPIO.OUT)
GPIO.setup(led_pins, GPIO.OUT)
GPIO.output(relay_pins, 1)
GPIO.output(led_pins, 1)

try:
    while True:
        for pin in relay_pins:
            GPIO.output(pin, 0)
            sleep(1)
        for pin in relay_pins:
            GPIO.output(pin, 1)
            sleep(1)
except KeyboardInterrupt:
    pass
GPIO.cleanup()

Controlling AC Devices using Relay Module

Before going further, I must warn you that use of High voltage can be dangerous if used incorrectly and it can cause you serious injuries and i will not responsible for that.

For the control of AC device, we will require an external source which will power the AC source. So, before connecting anything, make sure your Raspberry Pi is turned off.

Now, connect the VCC, ground and signal or IN1 to the 5V, ground and GPIO26 of Raspberry Pi respectively. On the other end, connect one wire of the power plug to the one end of the bulb socket and the other wire of the power plug to the common (C) of the relay. Then connect the normally open (NO) of relay to the other end of the bulb socket.

 

 

 

 

 

 

raspberry pi relay control

Python code

import RPi.GPIO as GPIO
from time import sleep

relay_pin = 26

GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)
GPIO.output(relay_pin, 1)

try:
    while True:
        GPIO.output(relay_pin, 0)
        sleep(5)
        GPIO.output(relay_pin, 1)
        sleep(5)
except KeyboardInterrupt:
	pass
GPIO.cleanup()

If you have any questions, feel free to ask in the comment section.

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar