X

How to Connect a Relay Board to a Raspberry Pi

by | May 31, 2022

In this tutorial I will show you how to connect a relay board to a Raspberry Pi, relays are an electromagnetic switch that allows you to control a high voltage electrical circuit by opening and closing contacts in another low voltage circuit. On the Raspberry Pi the control circuit will be operated by our GPIO pins. A relay generally has 3 connection points on the controlled circuit side, Normally Open (NO), Normally

Closed (NC) and a Common, an important convention to note is that when a relay contact is normally open (NO) the relay is not energized.

To configure the relay board with a Raspberry Pi I am assuming that you are running the latest version of Raspbian and have the ability to connect to your Pi through SSH with Putty and FTP with Filezilla, or directly with a keyboard and monitor, if you haven’t set-up your Pi yet then check out my getting started section.

I will not be connecting AC powered equipment to the output of the relay yet but we will be able to see that we have control by the led indicator lights on the circuit board itself. You could also use a buzzer or multi-meter to prove that the relays are switching.

 

Materials

 

In this tutorial I will be using the following materials:

 

When describing the physical pin connections I will be following the GPIO pin numbering convention shown below.

 

 

Warning: Working with AC current is Dangerous, Please exercise extreme caution and preferably consult a professional to make any AC Power connections. Always disconnect the circuit from the mains when not in use and mount in a secure, sealed enclosure to prevent accidental contact.

 

Connecting the Relay

 

The relay board that I am using is “ACTIVE LOW” which means that the relays are switched on when the inputs have a Ground (0 volts) connection applied to them while there are 5 volts connected to the Vcc pin.

Since the GPIO pins on the Pi output 3.3V when active we need a way to effectively short the input pins on the relay board to Ground when we activate the GPIO pin, the Transistor (2N3904)/Resistor (2.2kΩ, 10kΩ) circuit shown below will achieve this. It also ensures that when the Pi is shutdown that the relay does not stay energized when connected to and external 5V power source.

In this case, the transistor can be used as the equivalent of a switch which is controlled by the GPIO pin. When the GPIO pin is activated a voltage is applied to the Base (B) pin of the transistor, this effectively “closes the switch” and allows current to flow between the Collector (C) pin, to the Emitter (E) pin, completing the circuit from the 5 volt Vcc connection on the relay board to Ground.  The opposite is also true when the GPIO pin is deactivated there is no voltage applied to the Base (B) pin and this effectively “opens the switch” and breaks the connection to Ground from the relay board.

One of these circuits will be required for each GPIO pin to relay input that you are connecting.

 

To connect the relay module to your Pi you will need to make the following connections, I’ll be using GPIO pins 22-25 to cover 4 relays but you can use any spare pins.

  • Connect the 5V Pi pin to the Vcc pin of the relay board
  • Connect the Ground Pin of the Pi to the Ground pin of the relay board
  • Connect GPIO pin 22 to the input of the transistor circuit above and the output to IN1 pin of the relay board
  • Repeat the process above for any remaining relays (each relay requires a separate transistor circuit)
  • If not connected already then place the jumper between the JD-Vcc and the Vcc pin. (remove if you are using an external 5V power supply for the relay board)

It should be noted that each relay draws approx 72mA from the Pi when activated, given that the Pi itself required 500mA it is important to make sure that your power supply for the Pi can provide this current, a minimum of a 1.5 amp supply should be used to allow for a margin of safety. If you are connecting more than 4 relays then it is probably necessary to power your relay board with a separate 5V supply.

 

Note: Connecting the jumper between the JD-Vcc and Vcc pins removes the optical isolation provided by the circuit board but allows you to power the relay coils from the Pi. If you want full isolation then remove the jumper from JD-Vcc to Vcc and the Ground pin connection to the Pi. Connect a separate 5V power supply to the JD-Vcc pin and the Ground pin of the relay board.

 

The Relay Control Code

 

Now that we have a circuit connected we can go ahead and add some code for our Raspberry Pi relay control and test the connections.

 

#!/usr/bin/env python
# Import GPIO Module
import RPi.GPIO as GPIO
# Import sleep Module for timing
from time import sleep
# Configures how we are describing our pin numbering
GPIO.setmode(GPIO.BCM)
# Disable Warnings
GPIO.setwarnings(False)
# Set the GPIO pins that are required
OutputPins = [22, 23, 24, 25]
#Set the GPIO pins to outputs and set them to off
for i in OutputPins:
    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, False)
try:
    while (True):
# Step through each GPIO pin and set On
        for i in OutputPins:
            GPIO.output(i, True)
# Sleep for 5 seconds
            sleep(5)
            GPIO.output(i, False)
except KeyboardInterrupt:
# catches the ctrl-c command, breaks the loop above 
# and turns the relays off
    For i in OutputPins:
        GPIO.output(i, False)

 

Now transfer the code to your chosen folder on the Pi using an FTP client and then run the program using SSH or from the desktop using a Terminal.

 

$ cd your/code/directory
$ python3 your_code.py

 

While there is no visible output from the program the led’s and relays should cycle on and off every 5 seconds until ctrl-c is entered to break the loop.

To ensure that it is working correctly when one of the relays is activated so that the led is on, use a multimeter to check that the output of the relay is active, shut down your Pi and the circuit should open. This will ensure that the transistor circuit is working correctly and in the event that your Pi loses power, whatever you are powering from the relay will also turn off.

All the python code is available on my Hydropi GitHub Repository.

All going well you have now configured your Pi to interface with a 4-Channel Relay Board.

If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.

 

 

View Comments (10)

  • At the end of last year I had become interested in the cool things being down with the Raspberry Pi, handheld video games systems, robots, light shows, servers, etc. and I am interested in learning more.

    As I read your post I found myself thinking what can you use the relay board for, what projects can you incorporate a relay board in and what can a relay board make a Raspberry Pi do?

    • Hi Salvatore, I’m using the relay board to switch pool pumps, waterfall pumps and lights at the moment but pretty much any AC powered device that you want to turn on/off remotely you can control.

  • This is super helpful. Thanks a ton for this. I actually have a relay board and was told that I could actually configure and connect a Raspberry Pi to it. I really didn’t know if that was true or how to do it. But this really did help me with it. Thanks a lot of the helpful information.

  • Hi Dominic,
    Can you elaborate a little more on how that transistor and resisters changes a 3.3v output from the gpo pin to a zero single to the relay?
    Loving with website by the way!
    Rob

    • Hi Rob

      Loosely you can think of the transistor as a switch, when the GPIO pin on the Pi goes HIGH (3.3V) on the Base(B) of the transistor this closes the switch and allows current to flow between the Collector(C) and the Emitter(E) and effectively connects the relay pin to ground(0V), activating the relay. The resistors are there to control the source/sink current so as not to damage the GPIO port. You can see a similar example using a FET rather than an NPN transistor here under Controlling the LED. When it comes to electronic circuits I am still learning myself so I would encourage you to search for a more detailed explanation.

      Dom

  • Thanks for the great explanation. I didn’t realize I should be keeping a max amperage of 3mA through these pins.
    Rob

    • Hey Rob, you can actually draw more than 3mA but it’s really hard to find a definitive answer. From my reasearch you shouldn’t draw more than 16 mA from an individual GPIO or more than 50 mA from the GPIO as a whole, but this dates back to the original Pi, for more info have a look at this.

  • Thank you for the instructions. I am coming up with my own pool control system. I have the software more or less working and I am now working on the hardware part. Would a PN2222 transistor work? I used that transistor but it seems not to work. As I understand, the base (pin2) of the transistor should connect to a 2k resistor (I do not have a 2.2k on hand), the other side of that resistor should connect to both 10k resistor and to the gpio pin. The 10k resistor should connect to the ground. Is that correct?

    • Hi Chris
      The PN2222 should be fine as it is a similar transistor but is designed to handle much higher currents. Your description of the resistor configuration seems correct as well.

Related Post