SparkFun Qwiic Dual Solid State Relay Hookup Guide

Pages
Contributors: El Duderino, Englandsaurus
Favorited Favorite 1

Arduino Examples

The examples included with the SparkFun Qwiic Relay Arduino Library work with all Qwiic Relay boards with one exception. Example 7 - Slow PWM was added specifically for the two Qwiic Solid State Relay boards to demonstrate how to use the setSlowPWM(uint8_t relay, uint8_t pwmValue); function. As mentioned in the previous section, this function will only work on the SSR boards and not the EMR boards. In this section we'll explore this example to explain how it works and how to modify it.

Also, take note that depending on which Qwiic Relay board you are using, you'll need to alter the I2C address definition in the beginning of all of the examples. Below you can see the proper definition for the default address on the Qwiic Dual Solid State Relay:

language:c
#define RELAY_ADDR 0x0A

If you have altered the ADR jumper or changed the I2C address using the bool changeAddress(uint8_t newAddress); function, adjust the above definition accordingly.

One last note before we move into the example. This example was originally written for the Quad Solid State Relay Kit and defines the relay object, Qwiic_Relay, as quadRelay(RELAY_ADDR); which may be a bit confusing if you are not using that kit. The code will work just fine with that object definition but if you would prefer, you can alter that object to something else. If you do alter this definition, you will need to replace that object call in each function that uses the old one. The code below demonstrates these modifications to Example 7.

Example 7 - Slow PWM

If you are not familiar with how Pulse Width Modulation works or what we mean by "duty cycle", head on over to our Pulse Width Modulation Tutorial for a detailed introduction to how this type of signal works.

This example generates a Pulse Width Modulation (PWM) signal from the ATTiny84 on the board to quickly switch a selected relay on and off. Note, the code below is NOT identical to Example7_Slow_PWM. This example was originally written for the Qwiic Quad Solid State Relay Kit and sets slowPWM values for four relays so we've included a modified version below that sets up only two relays and also uses the default I2C address for the Dual Solid State Relay.

You can copy the code below and paste it into a new Arduino sketch or, if you you prefer, you can open the default example in Arduino by navigating to File > Examples > SparkFun Qwiic Relay Arduino Library > Example7_Slow_PWM. Next, open the Tools menu and select your board (in our case, Arduino Uno) and the correct Port your board enumerated on.

language:c
#include <Wire.h>
#include "SparkFun_Qwiic_Relay.h"

#define RELAY_ADDR 0x0A // Alternate address 0x0B


Qwiic_Relay dualRelay(RELAY_ADDR); 

void setup()
{
  Wire.begin(); 
  Serial.begin(115200);

  // Let's make sure the hardware is set up correctly.
  if(!dualRelay.begin())
    Serial.println("Check connections to Qwiic Relay.");
  else
    Serial.println("Ready to flip some switches.");

  Serial.println("Let's turn each relay on, one at a time.");
  // To turn on a relay give the function the number you want to turn on (or
  // off).

 dualRelay.setSlowPWM(1, 75);
 dualRelay.setSlowPWM(2, 75);
 Serial.println(dualRelay.getSlowPWM(1));
 Serial.println(dualRelay.getSlowPWM(2));


}

void loop()
{
}

Upload the code and open your serial monitor with the baud set to 115200. The code prints out whether or not the initialization of the Qwiic Dual SSR was successful and then prints the PWM value for each relay set using the setSlowPWM(); function. Let's take a closer look at the code.

The code sets up each relay to run at a default PWM value of 75 (62.5% duty-cycle). Try playing around with the PWM values set in the setSlowPWM(); function for each relay to see how it affects the behavior of your load(s).

language:c
// To turn on a relay give the function the number you want to turn on (or
// off).
quadRelay.setSlowPWM(1, 75);
quadRelay.setSlowPWM(2, 75);

Note, as we mentioned in the previous section, the PWM resolution is capped at 120 since the relay cannot switch more times than that in one second on a 60Hz signal.

The code also demonstrates how to use the getSlowPWM(); function by printing out the PWM value set for each relay:

language:c
Serial.println(quadRelay.getSlowPWM(1));
Serial.println(quadRelay.getSlowPWM(2));

If Arduino isn't your jam, head on to the next section where we cover the Python package for this and our other Qwiic Relay boards.