SparkFun Qwiic Quad Solid State Relay Kit Hookup Guide
Now that you have the Qwiic Relay Py package installed, it's time to check out the examples included with the package.
Example 1 - The Basics
This example demonstrates the basics of turning relays on and off. The code initializes the Qwiic Quad Solid State Relay on the I2C bus, turns on relays 1 and 3 and prints the status of all relays.
language:python 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 three myRelays.set_relay_on(1) myRelays.set_relay_on(3) time.sleep(1) #Print the status of all relays for relayNum in range(4): current_status = None if myRelays.get_relay_state(relayNum) is True: current_status = "On" else: current_status = "Off" print("Status 1: " + current_status + "\n")
Next, the code turns off relays 1 and 3 and turns on relays 2 and 4.
language:python #Turn off 1 and 3, turn on 2 and 4 myRelays.set_relay_off(1) myRelays.set_relay_on(2) myRelays.set_relay_off(3) myRelays.set_relay_on(4) time.sleep(1)
Finally, the code turns all relays on for one second and then turns them all off.
language:python #Turn all relays on, then turn them all off myRelays.set_all_relays_on() time.sleep(1) myRelays.set_all_relays_off()
With the basics of toggling all the relays, it's time to move on to using the Slow PWM function to pulse our Solid State Relays.
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 Quad SSR Kit 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:
language:python myRelays.set_slow_pwm(1, 30) #25% duty cycle myRelays.set_slow_pwm(2, 60) #50% duty cycle myRelays.set_slow_pwm(3, 90) #75% duty cycle myRelays.set_slow_pwm(4, 120) #100% duty cycle for relay_num in range(4): pwm_value = myRelays.get_slow_pwm(relay_num) print("PWM Value for relay ") print(relay_num) print(": ") print(pwm_value) #Let the slow PWM run for a while time.sleep(15)
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.
After our 15 seconds is up, the code turns all relays off by setting the PWM value to 0 and prints out the new PWM values:
language:python myRelays.set_slow_pwm(1, 0) myRelays.set_slow_pwm(2, 0) myRelays.set_slow_pwm(3, 0) myRelays.set_slow_pwm(4, 0) for relay_num in range(4): pwm_value = myRelays.get_slow_pwm(relay_num) print("PWM Value for relay ") print(relay_num) print(": ") print(pwm_value)
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.