Qwiic Quad Relay Hookup Guide

Pages
Contributors: Elias The Sparkiest
Favorited Favorite 4

Example Code

Example 4 - Relay Control using Buttons

This is the code used for the lamp example below. Unzip and open up example four under ... > SparkFun Qwiic_Relay_Arduino Library-master > Example Code > Example4_Quad_Relay_Buttons to follow along. Starting at the top, we have #include-ed the path to the library's header file as well as to Arduino's I²C library: Wire.h. To use the functions in the SparkFun Qwiic Relay Library we create a version of it, and name it quadRelay. You'll notice that in parentheses we have given it the board's address. If you have changed it or closed the address jumper, than change the RELAY_ADDR to your address.

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

#define RELAY_ADDR 0x6D

Qwiic_Relay quadRelay(RELAY_ADDR);

Next we setup three buttons: yellow, red, and blue on three pins: 2, 3, and 4.

language:c
const int yellow_btn = 2;
const int red_btn    = 3;
const int blue_btn   = 4;

First we check that we can communicate correctly with the Quad Relay with the quadRelay.begin() function call. If there are some connection issues we'll find out about them here. Notice that his won't stop our code from running so keep an eye out for an error message. Next we use pullup resistors on our buttons to put them into a known HIGH state.

language:c
void setup()
{
  Wire.begin(); 
  Serial.begin(115200);

  if(!quadRelay.begin())
    Serial.println("Check connections to Qwiic Relay.");
  else
    Serial.println("Ready to flip some switches.");

    //Use internal resitors to keep them in a known high state. 
  pinMode(yellow_btn, INPUT_PULLUP);
  pinMode(blue_btn, INPUT_PULLUP);
  pinMode(red_btn, INPUT_PULLUP);

}

Finally in the loop the buttons are constantly being checked for a button press. For example, if the blue button is pressed then relay number one turns on. The small 400ms delay is there for debounce. Without it each of our casual presses would be read a couple hundred times before we finally took our finger off of the button.

language:c
void loop()
{
    // Button one turns on relay one....
  if(digitalRead(yellow_btn) == LOW){
        delay(400);
    quadRelay.turnRelayOn(1);
    Serial.println("Yellow Button");
  }

    // Button two turns on relay two....
  if(digitalRead(blue_btn) == LOW){
        delay(400);
    quadRelay.turnRelayOn(2);
    Serial.println("Blue Button");
  }

  // Button three turns off relay one and two...
  if(digitalRead(red_btn) == LOW){
        delay(400);
    quadRelay.turnRelayOff(1);
    quadRelay.turnRelayOff(2);
    Serial.println("Red Button");
  }

}

Now let's upload some code via the Arudino IDE. Before uploading, be sure to remove power to the load when uploading to safely handle the relay. Then connect the Arduino to your computer to upload. Select the board (in this case the Arduino/Genuino Uno) and COM port that your Arduino has enumerated to. Click the upload button. When the code has finished uploading, place the Arduino and relay on a non-conductive surface to test. Remember to not touch the relay's contacts when the system is powered.