HomeTutorials › Getting Started With The Raspberry Pi Relay HAT

Getting Started With The Raspberry Pi Relay HAT

Our Pi Relay HATs are designed to allow your Pi to switch higher voltages and higher currents from one self contained board. In this tutorial we are going to go over soldering the header to the Relay HAT, use Python with the included Pi.GPIO library to write code that triggers each relay, and go over the external relay connections and configuration options on the board. This tutorial can be used for both the 4 Channel Standard Relay HAT and the 2 Channel Pi Zero Relay HAT.

About The Boards:
We make two versions of this relay board, one for the standard Raspberry Pi with 4 relays, and one for the Raspberry Pi Zero with 2 relays. On the board, each relay’s Common, Normally Open, and Normally Closed pins are brought out to screw terminals. These are not the most sophisticated circuits, but they do provide a compact, permanent solution for attaching a number of relays to the Pi.

Safety Note: The boards are designed to handle higher voltages, but we do not recommend doing so for anyone unfamiliar with the dangers of high voltage. If you are inexperienced or unsure about how to use this product safely we recommend looking at the IoT Power Relay, which has all of its high voltage circuitry fully enclosed.

The Parts Needed:

parts

This tutorial requires a few parts depending on which version of the Pi and Pi Relay HAT you are looking to use:

parts

Step 1 – A Quick Overview

Step1

There isn’t much in the way of assembly required with these boards as they ship with everything but a header installed. We do not solder a header to the board as different heights or types may be required depending on your application (and removing them can be quite a pain!).

For all standard Raspberry Pi we recommend using a Tall Header, as this will allow inputs on the “USB / Ethernet” side of the Pi to clear. For the Pi Zero, a shorter header can be used so we recommend using the standard GPIO header, but feel free to go a different way as needed. Once we have the headers soldered in, we will use Python and the GPIO library to write some code to trigger each relay, and finally we will look at the different connections on the board.

Step 2 – Soldering the header

Step2

We are going to recycle a couple old photos in the next two steps for soldering the header. The boards are different, but the overall process is the same.

First we need to solder the header that allows this board to plug into the Raspberry Pi. If you haven’t soldered before, or want a quick refresher course, have a look at this awesome comic: Soldering Is Easy! https://mightyohm.com/…/FullSolderComic_EN.pdf

Start by Tacking two opposite corners of the connector in place and checking the connector alignment. We do this to ensure the connector is sitting correctly before soldering all 40 pins; once these have all been soldered, it is very difficult to adjust the alignment.

Step 3 – Soldering the header (continued)

Step3

Once the connector is aligned to the board, and you are happy with the alignment, solder the remaining pins. It should look something like the attached photo when you are finished (but with relays and such!)

Step 4 – Double Check Your Work!

Before we power anything up, it is always a good idea to go through and make sure there are no issues with the work that has been done. Make sure all the solder joints are clean, with no un-intended bridging. Once the board has been checked over, install it on top of the Pi.

Step 5 – HAT Hardware

Step3

When using HATs with the Raspberry Pi, we recommend some form of standoff hardware to keep everything firmly connected. In this tutorial, depending on whether you are using the Pi Zero with the Zero Relay HAT or the Pi 3+ with the 4 Channel Relay HAT, we have recommended using headers with different heights, meaning different hardware will be needed for each.

For the Pi Zero, using the GPIO header we recommend using the Raspberry Pi HAT Hardware Kit. When using the tall header (as we did with the Pi 3+) you will need something quite a bit taller. We recommend going to the 5/8” Nylon Standoffs with 4/40 Nylon Nuts and 4/40 x 1/4″ Screws.

Step 6 – Getting Ready To Power Up Your Pi

Before we get the Pi powered up, you should have a microSD card pre-installed with Raspbian. We are using the installation image dated June 27, 2018. Using a different version than this *could* require additional steps during the installation process – so for this reason we recommend using the exact version we are. At the time of writing this tutorial, this is the most current version available from the Raspberry Pi Foundation and it can be found here

Once this has been completed, we are going to insert the microSD card into the Pi. Your keyboard, mouse and monitor should also be connected at this time. Power up the Pi by plugging in the Power Supply. Once the Pi has done its initial boot of the operating system you should arrive at a desktop. A dialogue should appear for initial configuration – follow through the steps as instructed.

Step 7 – Controlling The HAT

Step4

Conveniently, the software library we need is included in the default Raspbian image. We won’t need to do much configuration in this tutorial to control the HAT! To keep things consistent with our other tutorials we are going to use Python 2.7 . Click the terminal logo up top and type the following command into the box to open the Python 2.7 editor.

idle

A new window should have opened for the Python 2.7 Shell. We are now done with the terminal window so it can be minimized or moved out of the way (but don’t close it!).

Step 8 – Write some of our own code

step10

In the Python Shell window we just opened, click “File” and “New File” to start a new Python file. This will open another window that we will write all of our code in. We aren’t going to write anything too complicated – just a simple program to turn each relay on and off. This code can then be used going forwards to work the Relay HAT into your project!

Step 9 – Starting The Code

Before we can trigger any of the relays, we will need to create the basic framework of the program. This means we need to import the time library, create our loop, and set the interval we want to pause the code. In this instance we are going to pause between each relay trigger event. For those unfamiliar with Python, note that the white spaces (tabs and spaces) are very important in this language so be sure to format exactly as shown in the example.


import time

interval = 1  #How long we want to wait (seconds)


while True:

    time.sleep(interval)

Libraries are added to the project using the import command. On the first line we are importing the time library. On line 3 we define a variable to store the length of time in seconds that we want to sleep the program during each loop of the code. Next, we want to create the loop itself – in Python this can be done in a variety of ways; “While true:” works well. Note that when you press “Enter” the next line becomes indented. Finally, on line 8 we “sleep” or pause our program using the variable we set above. The end result is a program that starts, and then runs whatever code we stick in the loop once a second.

Note: Because we are going to be adding code over the next few steps, each new line we add will be highlighted in the example code and the line number will correspond to notes below the code.

Step 10 – Import the libraries

We are now going to import the Raspberry Pi GPIO library. This is done the same way as we imported the time library.


import time
import RPi.GPIO as GPIO


interval = 1 # How long we want to wait (seconds)

while True:
    
    time.sleep(interval)

  • (2) import the GPIO library

Step 11 – Configure GPIO

We now need to configure the GPIO library and each of the pins we are using.


import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(4, GPIO.OUT) #set Relay 1 output 
GPIO.setup(17, GPIO.OUT) #set Relay 2 output

#Pi Zero users do not need to configure the pins for Relays 3 & 4, these are unused. 
GPIO.setup(27, GPIO.OUT) #set Relay 3 output 
GPIO.setup(22, GPIO.OUT) #set Relay 4 output 

interval = 1 # How long we want to wait (seconds)

while True:
    
    time.sleep(interval)

  • (3) Set to the Broadcom pin-numbering scheme
  • (4-5) Set pins 4 and 17 as outputs (Relays 1 & 2)
  • (8-9) Set pins 27 and 22 as outputs (Relays 3 & 4 , not used for Zero HAT)

Step 12 – Trigger a Relay

With everything configured, we can now trigger relays on and off with a simple line of code.


import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(4, GPIO.OUT) #set Relay 1 output 
GPIO.setup(17, GPIO.OUT) #set Relay 2 output

#Pi Zero users do not need to configure the pins for Relays 3 & 4, these are unused. 
GPIO.setup(27, GPIO.OUT) #set Relay 3 output 
GPIO.setup(22, GPIO.OUT) #set Relay 4 output 

interval = 1 # How long we want to wait (seconds)

while True:
    
    GPIO.output(4, GPIO.HIGH) #turn relay 1 on
    time.sleep(interval) 
    GPIO.output(4, GPIO.LOW) #turn relay 1 off
    time.sleep(interval)

  • (15) Set pin for relay 1 HIGH to turn on relay
  • (16) Wait 1 second
  • (17) Set pin for relay 1 LOW to turn off relay



Hit “F5” on the keyboard – this will prompt you to save the file. Once saved, the program should start running. As soon as the program starts, Relay 1 should begin clicking and the indicator LED for Relay 1 flashing on and off every second.

Step 13 – Trigger The Remaining Relays

And now we can add the remaining relays using very similar code:


import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(4, GPIO.OUT) #set Relay 1 output 
GPIO.setup(17, GPIO.OUT) #set Relay 2 output

#Pi Zero users do not need to configure the pins for Relays 3 & 4, these are unused. 
GPIO.setup(27, GPIO.OUT) #set Relay 3 output 
GPIO.setup(22, GPIO.OUT) #set Relay 4 output 

interval = 1 # How long we want to wait (seconds)

while True:
    
    GPIO.output(4, GPIO.HIGH) #turn relay 1 on
    time.sleep(interval) 
    GPIO.output(4, GPIO.LOW) #turn relay 1 off
    time.sleep(interval)

    GPIO.output(17, GPIO.HIGH) #turn relay 2 on
    time.sleep(interval) 
    GPIO.output(17, GPIO.LOW) #turn relay 2 off
    time.sleep(interval)

    #Not Used For Pi Zero
    GPIO.output(27, GPIO.HIGH) #turn relay 3 on
    time.sleep(interval) 
    GPIO.output(27, GPIO.LOW) #turn relay 3 off
    time.sleep(interval)

    GPIO.output(22, GPIO.HIGH) #turn relay 4 on
    time.sleep(interval) 
    GPIO.output(22, GPIO.LOW) #turn relay 4 off
    time.sleep(interval)
  • (20-23) Turn Relay 2 On and then Off
  • (26-29) Turn Relay 3 On and then Off
  • (31-34) Turn Relay 4 On and then Off



Hit “F5” on the keyboard – save the file once again. This time when the program runs, each relay should cycle on and off in order. This code can be modified to use external triggers, sensor readings, etc. to control the relays as needed.

Step 14 – External Connections

Step3
Step3

Now that we have everything working, how do the screw terminals for each of the relays work? They are fairly simple: these are Single Pole Dual Throw (SPDT) Relays, meaning they have a contact that is normally closed and a contact that is normally open. The third screw terminal connection is for a common pin. This configuration allows you to have circuits that are “Off” until the relay turns them on and circuits that are “On” until the relay turns them off.

Step 15 – Using Other GPIO Pins

Step3
Step3

We thought it might be useful to allow users to disconnect the default Raspberry Pi GPIO pins 7,11,13,15 (BCM numbers 4, 17, 22, 27) just in case they are needed for something else. Each relay has a solder jumper that will allow it to be disabled and a solder-able connection that could be connected to another GPIO pin. Alternatively, we do offer the same 4 Channel Relay circuit in an external breakout board that can be connected to any GPIO pin as well.

Have A Question?
If you have any questions, or need further clarification please post in the comments section below; this way future users of this tutorial can see the questions and answers!

Parts Used In This Tutorial:

3 comments

  1. John
  2. Joe Goldthwaite

Leave a Reply