An image of an Arduino device and other Electric components

Driving a Relay Board from Python Using GPIO Zero

Print Friendly and PDF

Posted: March 25, 2017 | Categories: Internet of Things (IoT)

As you may have noticed, I've been working on a few projects lately that use relays. A while back, I bought some single, double and quad relay boards from (in China, I think). They were pretty inexpensive and I thought they'd work well for some of my projects. I never got around to using them – mostly because there were some jumpers on the board and no instructions and I really didn't have the patience to figure out how they worked.

Anyway I got this cool Paddle Breakout HAT from ModMyPi, so I had an easy way to wire up the Pi to the relay board. I connected the relay board to the Pi's 5V output, Ground, and GPIO pin 18 then wrote a simple app to test it out. It worked! First try. Here's a picture of the setup, the board on the left is the relay testing jig I wrote about here.

Figure 1

I created a GitHub Gist with the code, you can find that here in an easily digestible format:

The code uses the GPIO Zero library to interact with the relay board. You basically define an output device using the following code:

relay = gpiozero.OutputDevice(18, active_high=False, initial_value=False)

Then, you turn the relay on by executing:


And turn the relay off by executing:

It couldn't be simpler. Toggling the relay is easy too, just make a call to:


The GPIO Zero library takes care of tracking relay status automatically.

The relay board comes configured by default to trigger the relay when the input voltage goes low, so that's why the output device initialization includes the active_high=False parameter. The initial_value=False simply makes sure the relay starts in an off status.

Here's the complete source code listing for the app:


# A simple Python application for controlling a relay board from a Raspberry Pi
# The application uses the GPIO Zero library (
# The relay is connected to one of the Pi's GPIO ports, then is defined as an Output device
# in GPIO Zero:

import sys
import time

# Make sure you install required libraries:
import gpiozero

# change this value based on which GPIO port the relay is connected to

# create a relay object.
# Triggered by the output pin going low: active_high=False.
# Initially off: initial_value=False
relay = gpiozero.OutputDevice(RELAY_PIN, active_high=False, initial_value=False)

def set_relay(status):
if status:
print("Setting relay: ON")
print("Setting relay: OFF")

def toggle_relay():
print("toggling relay")

def main_loop():
# start by turning the relay off
while 1:
# then toggle the relay every second until the app closes
# wait a second

if __name__ == "__main__":
except KeyboardInterrupt:
# turn the relay off
print("\nExiting application\n")
# exit the application

Next Post: Ridiculous IBM Password Form

Previous Post: Tim the Enchanter

If this content helps you in some way, please consider buying me a coffee.

Header image: Photo by Robin Glauser on Unsplash.