SparkFun Qwiic Dual Solid State Relay Hookup Guide

Pages
Contributors: El Duderino, Englandsaurus
Favorited Favorite 2

Python Examples

Now that you have the Qwiic Relay Py package installed, it's time to check out the examples included with the package.

Qwiic_Relay_ex1 - The Basics

This example demonstrates the basics of turning relays on and off and reading relays' statuses. The code initializes the Dual SSR on the I2C bus and steps through the different ways to turn both relays on and off and shows how to read the status of both relays.

The example was written to work by default with the Qwiic Quad Relay Kit so we'll need to modify the I2C address call and we'll also want to remove references to a third and fourth relay. The code below is NOT the same as qwiic_relay_ex1.py and has been modified to work with the Dual SSR board. Copy the code below or you can open the example from your install of the Qwiic Relay Python package:

language:python
from __future__ import print_function
import qwiic_relay
import time
import sys


QUAD_RELAY = 0x6D
SINGLE_RELAY = 0x18
QUAD_SOLID_STATE_RELAY = 0x08
DUAL_SOLID_STATE_RELAY = 0x0A

#Be sure to initialize your relay with the proper address.
myRelays = qwiic_relay.QwiicRelay(DUAL_SOLID_STATE_RELAY)

def runExample():

    print("\nSparkFun Qwiic Relay Example 1\n")

    if myRelays.begin() == False:
        print("The Qwiic Relay isn't connected to the system. Please check your connection", \
            file=sys.stderr)
        return

    #Turn on relays one and two
    myRelays.set_relay_on(1)
    myRelays.set_relay_on(2)
    time.sleep(1)

    #Print the status of all relays
    for relayNum in range(2):
        current_status = None
        if myRelays.get_relay_state(relayNum) is True:
            current_status = "On"
        else:
            current_status = "Off"
        print("Status 1: " + current_status + "\n")

    #Turn off 1 and turn on 2
    myRelays.set_relay_off(1)
    myRelays.set_relay_on(2)
    time.sleep(1)


    #Turn all relays on, then turn them all off
    myRelays.set_all_relays_on()
    time.sleep(1)

    myRelays.set_all_relays_off()



if __name__ == '__main__':
    try:
        runExample()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example 1")
        sys.exit(0)

Example 2 - Slow PWM

The second example included with the Python package demonstrates how to use the set_slow_pwm() function. The example starts just like the first by initializing the Dual SSR on the I2C bus and then sets the PWM duty-cycle for each relay and uses the get_slow_pwm() function to print the PWM value set for each relay.

Just like Example 1, this was written to work by default with the Qwiic Quad Relay Kit so modify the I2C address call and remove references to a third and fourth relay like above. Similarly, the code below is NOT the same as qwiic_relay_ex2.py and has been modified to work with the Dual SSR board. Copy the code below or you can open the example from your install of the Qwiic Relay Python package and modify it to work with your Dual SSR:

language:python
from __future__ import print_function
import qwiic_relay
import time
import sys

QUAD_RELAY = 0x6D
SINGLE_RELAY = 0x18
QUAD_SOLID_STATE_RELAY = 0x08
DUAL_SOLID_STATE_RELAY = 0x0A

#Be sure to initialize your relay with the proper address.
myRelays = qwiic_relay.QwiicRelay(DUAL_SOLID_STATE_RELAY)

def runExample():

    print("\nSparkFun Qwiic Relay Example 2\n")

    if myRelays.begin() == False:
        print("The Qwiic Relay isn't connected to the system. Please check your connection", \
            file=sys.stderr)
        return

    #Note that our range is 0-120 for setting a PWM value as there are only 120 times where the zero crossing relay can switch in one second

    myRelays.set_slow_pwm(1, 30) #25% duty cycle
    myRelays.set_slow_pwm(2, 60) #50% duty cycle

    #Print out our PWM values 
    for relayNum in range(1, 5):
        pwmValue = myRelays.get_slow_pwm(relayNum)
        print("PWM Value for relay " + str(relayNum) + ": " + str(pwmValue))
    #Let the slow PWM run for a while
    time.sleep(15)


    #Set all relays off 
    myRelays.set_slow_pwm(1, 0)
    myRelays.set_slow_pwm(2, 0)

if __name__ == '__main__':
    try:
        runExample()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example 1")
        sys.exit(0)

Each relay is set to a different duty cycle to demonstrate how that affects the behavior of the load and will run for 15 seconds. Want your relay to pulse at a 50% duty cycle? Set the slow PWM value to 60. Note: Just like the Arduino Library, the PWM resolution is limited to 0-120 since there are only 120 times where the zero crossing relay can switch in one second.

That's all for the basics of our Python package! Try using the various functions included here to write your own code for your next relay project.