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:
Compatibility:
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.
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.
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.
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.
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.
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!).
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!
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.
import time
import RPi.GPIO as GPIO
interval = 1 #How long we want to wait (seconds)
while True:
time.sleep(interval)
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
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)
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:
GPIO.output(4, GPIO.HIGH) #turn relay 1 on
time.sleep(interval)
GPIO.output(4, GPIO.LOW) #turn relay 1 off
time.sleep(interval)
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.
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)
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.
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.
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”
John
I would encourage your followers of this tutorial to consider the Raspberry Pi Stacking Header for $3.95 to leave open the option of adding another hat such as the 16-channel Analog/Digital Converter for sensors such as a Moisture or Temperature Sensor. (Sensors/devices that output an analog signal.)
Even if another hat is not added, the stacking header provides easy access to the unused GPIO pins on the RaspberryPi for an expanded or future project.
Joe Goldthwaite
I’m using the PI Zero Relay HAT. For my use it might work better to mount it next to the PI instead of hooked up to the top and use jumper pins between the two.
I assume you’re using the power and ground pins. Are any of the other pins required for it to function?
Chris @ BCR
Pins used are GPIO 4 (Pin 7), GPIO 17 (Pin 11), 5V Power (Pin 2 or 4) and Ground (Pin 39)
casper
Is there a way to disable the indicator LED for Relays?
Chris @ BCR
Easiest way would be to remove all 4 resistors labeled “2200” – these are the current limiting resistors for the LEDs. Let us know if you have any further questions!
Wolfgang
Got PI Zero Relay HAT, but it looks different than the one in the tutorial and the header is already soldered. I don’t find any soldered jumper (Step 15); how can I change the GPIO-Pins??
William @ BC Robotics
Hi Wolfgang,
It sounds like you have the assembled version (RAS-194) of this board – the assembled version does not allow for the changing of IO pins as there would be no where to solder the needed jumper wires to in the header.