BC Robotics

Getting Started With The Raspberry Pi Relay HAT

  PRODUCT TUTORIAL

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. 

We make four versions of this relay board, two for the standard Raspberry Pi with 4 relays, and two for the Raspberry Pi Zero with 2 relays, with one fully assembled version and one without headers for each. 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.

This tutorial can be used for all versions of our Raspberry Pi Relay HATs:

The code for controlling the relays is also applicable to both versions of our Raspberry Pi Power Relay HATs:

Share on facebook
Share on twitter
Share on reddit
Share on email
Share on print

Recommended Parts & Compatibility

Compatibility:

  • 4 Channel Relay HATs are compatible with all versions of the Raspberry Pi A+ and B+ onwards. This includes the Raspberry Pi A+, 3A+, B+, 2, 3, 3+, and all versions of the Raspberry Pi 4.
  • Zero Relay HATs are compatible with all versions of the Raspberry Pi Zero.
0%

Step 1 - A Quick Overview

There isn’t much in the way of assembly required with these boards. For those versions without a header, one will need to be soldered in. For versions with the header already installed, feel free to jump to Step 5. 

Header Selection:  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. 

7.7%

Step 2 – Soldering The Header

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. 

15.3%

Step 3 – Soldering the header (continued)

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!) 

23%

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.

30.7%

Step 5 – HAT Hardware

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 an assembled board or one with headers of your choice.

38.4%

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 but any version newer than that should work just as well.

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.

46.1%

Step 7 – Controlling The HAT

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! While this tutorial is using Python 2.7 , this code can be used in Python 3 as well – luckily the code is cross compatible. Click the terminal logo up top and type “Idle”

A new window should have opened for the Python 2.7 / Python 3 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!). 

53.8%

Step 8 – Write some of our own code

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! 

61.5%

Step 9 – Starting The Code

				
					import time

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

while True:
    time.sleep(interval)
				
			

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.

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 6 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.

69.2%

Step 10 – Import the libraries

				
					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

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

 

76.9%

Step 11 – Configure GPIO

				
					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)
				
			
  • (4) Set to the Broadcom pin-numbering scheme so the correct pins are triggered
  • (5-6) Set pins 4 and 17 as output pins (Relays 1 & 2)
  • (9-10) Set pins 27 and 22 as output pins (Relays 3 & 4 , not used for Zero HAT)

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

84.6%

Step 12 – Trigger a Relay

				
					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)
				
			
  • (16) Set pin for relay 1 HIGH to turn on relay
  • (17) Wait 1 second
  • (18) Set pin for relay 1 LOW to turn off relay 

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

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.

 

92.3%

Step 13 – Trigger The Remaining Relays

				
					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)

#Not Used For Pi Zero
GPIO.output(22, GPIO.HIGH) #turn relay 4 on
time.sleep(interval)
GPIO.output(22, GPIO.LOW) #turn relay 4 off
time.sleep(interval)
				
			
  • (21-24) Turn Relay 2 On and then Off
  • (27-30) Turn Relay 3 On and then Off
  • (33-36) Turn Relay 4 On and then Off

And now we can add the remaining relays using very similar code. 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.

Going forwards this code can be modified to use external triggers, sensor readings, etc. to control the relays as needed. 

100%

Step 14 – External Connections

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 (RAS-075 / RAS-109 Only)

On both header-less versions of the board 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. 

7 thoughts on “Getting Started With The Raspberry Pi Relay HAT”

Leave a Reply

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the compare bar
Compare
Wishlist 0
Open wishlist page Continue shopping