Qwiic Single Relay Hookup Guide
Introduction
The Qwiic Single Relay is SparkFun's easiest to use relay yet. The single relay can handle up to 5.5A at 240 VAC for long periods of time. The Qwiic connectors and screw terminals also mean that no soldering is necessary.
Required Materials
To get started, you'll need a microcontroller to control everything. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
Now to get into the Qwiic ecosystem, the key will be one of the following Qwiic shields to match your preference of microcontroller:
You will also need a Qwiic cable to connect the shield to your Qwiic Single Relay, choose a length that suits your needs.
Tools
You will need a flush cutter and wire stripper to remove the sheath and insulation from a cable. A Phillips head screwdriver will be required to connect the load's to a screw terminal.
Suggested Reading
If you aren't familiar with the Qwiic system, we recommend reading here for an overview.
Qwiic Connect System |
We would also recommend taking a look at the following tutorials if you aren't familiar with them.
Serial Communication
Serial Terminal Basics
Qwiic Shield for Arduino & Photon Hookup Guide
Hardware Overview
First let's check out some of the characteristics listed in the relay's datasheet that we're dealing with, so we know what to expect out of the board.
Characteristic | Range |
---|---|
Operating Voltage | 1.7V-3.6V |
Supply Current | 100mA |
Coil Resistance | 23.5Ω |
I2C Address | 0x18 (Default), (Jumper changes to 0x19) |
Max Current (Through Relay) | 5.5A (240 VAC) |
Pins
The following table lists all of the relay's pins and their functionality.
Pin | Description | Direction |
---|---|---|
GND | Ground | In |
3.3V | Power | In |
SDA | Data | Bi-directional |
SCL | Clock | In |
NC | Normally Closed | Switch |
NO | Normally Open | Switch |
COM | Switch Common | Switch |
Optional Features
The Qwiic Relay has pull up resistors attached to the I2C bus; if multiple sensors are connected to the bus with the pull-up resistors enabled, the parallel equivalent resistance will create too strong of a pull-up for the bus to operate correctly. As a general rule of thumb, disable all but one pair of pull-up resistors if multiple devices are connected to the bus. If you need to disconnect the pull up resistors they can be removed by cutting the traces on the corresponding jumpers highlighted below.
The Power LED will light up when the board is powered. The Status LED will light up when the relay has been triggered and the switch is closed, both are highlighted in the below image
The onboard screw terminal should be used to connect your high-power load, it is highlighted below. The middle COM
pin should be hooked up to the Live wire (Usually black) coming from the wall, while NO
or NC
should be connected to the Live wire on the device side of things.
Hardware Assembly
If you haven't yet assembled your Qwiic Shield, now would be the time to head on over to that tutorial. Depending on the microcontroller and shield you've chosen, your assembly may be different, but here's a handy link to the Qwiic Shield for Arduino and Photon Hookup Guide to get you started!
With the shield assembled, SparkFun's new Qwiic environment means that connecting the relay could not be easier. Just plug one end of the Qwiic cable into the Qwiic Relay, the other into the Qwiic Shield and you'll be ready to upload a sketch and start turning things on and off. It seems like it's too easy too use, but that's why we made it that way!
You'll also need to place the relay in line with the AC powered item you're attempting to control. You'll have to cut your live AC line (usually black or red) and connect one end of the cut wire to COM
and the other to NC
or NO
, depending on what you want the resting state of your device to be. If your AC device is going to be on for most of the time, and you occasionally want to turn it off, you should connect one end to COM
and the other to NC
. Connect to NO
if the device will be off for most of the time. Check out the picture below for a visual aid.
Looking for information about safety and insulation? Check out the notes about Safety and Insulation from our Beefcake Relay Control Kit.
Arduino Library
We've written a library to make it even easier to get started with the SparkFun Qwiic Single Relay. The library will give you the full functionality of the Qwiic Single Relay without the hub bub of the I²C data transactions. You can click the link below to download the file or navigate through the Arduino Library Manager by searching SparkFun Qwiic Relay. You can also go the Github page and get it directly.
This library also works with our Qwiic Quad Relay board. If you need more relays on one PCB, then go check it out!
Library Functions Overview
The list below outlines the functions of the Qwiic Relay Arduino Library designed to work with the Qwiic Single Relay along with short descriptions of what they do. The examples cover nearly all of these functions so take a look at them for help integrating them into your own code.
bool begin(TwoWire &wirePort = Wire);
- Initialize the Qwiic Single Relay on the I2C busfloat singleRelayVersion();
- Returns the firmware version present on the Qwiic Single Relay.void turnRelayOn();
- Turn the relay on.void turnRelayOff();
- Turn the relay off.void toggleRelay()
- Toggles the relay to the opposite state. The function first checks the status of the relay and is toggled to eitheron
oroff
depending on what the status check returns.uint8_t getState();
- Returns the status of the relay. Returns1
if on or0
if off.bool changeAddress(uint8_t newAddress);
- Changes the I2C address of the Qwiic Relay to the value set fornewAddress
. ValidnewAddress
values can be between 0x07 and 0x78. The new address is written to the memory location in EEPROM that determines the address.
Example Code
The Qwiic Relay is pretty simple, so all of the functions to control it are simply contained in the first example of the SparkFun Qwiic Relay Arduino Library.
Example 1 - Single Relay Basics
Go ahead and unzip the folder to a directory of your choosing and open up Example1_Single_Relay_Basics
.
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 relay
. You'll notice that in parentheses we have given it the board's address. If you have changed this address 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 0x18 // Alternate address 0x19
Qwiic_Relay relay(RELAY_ADDR);
Let's look at the setup. First we check that we can communicate correctly with the Single Qwiic Relay with the relay.begin()
function call. If there are some connection issues we'll find out about them here. Note that the code will not stop if there is an error, so keep an eye out for an eror message. Next we check the version of the firmware that is on your product with the relay.singleRelayVersion()
function.
language:c
void setup()
{
Wire.begin();
Serial.begin(115200);
// Let's see
if(!relay.begin())
Serial.println("Check connections to Qwiic Relay.");
else
Serial.println("Ready to flip some switches.");
float version = relay.singleRelayVersion();
Serial.print("Firmware Version: ");
Serial.println(version);
Now we turn on and off the relay by using two functions. First the more obvious function turnRelayOn()
turns on the relay, which can then be turned off with turnRelayOff
. Next the relay is toggled on and off, with the toggleRelay()
function. The difference here is that the relay is turned off or on depending on the current state of the relay: if off ---> turn on and vice versa.
language:c
// Let's turn on the relay...
relay.turnRelayOn();
delay(500);
// Let's turn that relay off...
relay.turnRelayOff();
delay(500);
// Let's 'toggle' the relay; if it's off turn it on and vice versa.
relay.toggleRelay();
delay(500);
// Toggle the relay back off.
relay.toggleRelay();
delay(500);
You're probably wondering why I'm doing this entirely in the setup? In the chance that you're running this for the first time without reading, you won't have a relay constantly turning on and off. Now at the end of the setup we see the final feature of the SparkFun Qwiic Arduino Library.
language:c
Serial.print("The Relay is now: ");
// Is the relay on or off?
int state = relay.getState();
if(state == 1)
Serial.print("On!");
else if(state == 0)
Serial.print("Off!");
}
Using the getState
function we can check whether or not the relay is on or off without looking at the board. This can help you programmatically make decisions based on your project's needs.
Resources and Going Further
Now that you've successfully got your Qwiic Single Relay up and running, it's time to incorporate it into your own project!
For more information, check out the resources below:
- Schematic (PDF)
- Eagle Files (ZIP)
- Datasheet (PDF)
- Default Firmware
- SparkFun Qwiic Relay Arduino Library Github Repo
- SparkFun Qwiic Relay Python Package GitHub Repo
- Example Code
- GitHub Repository
- SFE Product Showcase
Need some inspiration for your next project? Check out some of these related tutorials using relays. Be sure to check your current rating when handling the Qwiic Single Relay when browsing some of the other tutorials using relays.
Photon Remote Water Level Sensor
Blynk Board Project Guide
ESP8266 Powered Propane Poofer
Blynk Board Bridge Widget Demo
Beefcake Relay Control Hookup Guide
How to Build a Remote Kill Switch
Qwiic Quad Relay Hookup Guide
Or check out these blog posts for inspiration.