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.

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:


import sys
import time
import gpiozero

relay = gpiozero.OutputDevice(18, 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():
    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

Overall Rating (0)

0 out of 5 stars

Post comment as a guest



Rate this article :
  • No comments found