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.

⚡ Warning! The Qwiic single relay is great for switching loads like motors, batteries, solenoids, pumps, and more! Keep in mind controlling high voltage (110/220VAC) requires certain precautions. A beginner can do it but if you're unsure then please consider the pre-made IoT Power Relay. It's not I2C but the IoT Power Relay contains shielding to prevent accidental shock.

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.

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.

Pull-up Jumper

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

Power LED

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.

Screw Terminal

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!

SparkFun RedBoard and Qwiic Shield with the Qwiic Relay attached

Note: Not sure about what color insulation wiring is used in you region? Check out the standard wire insulation colors listed online for reference. If you are unsure about the standard wiring color in your region, please consult a certified electrician to connect to the AC input voltage side.

⚡ Warning! Make sure the cable is not plugged into the wall as you cut into the wire in the following section.

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.

Relay Example Connection

⚡ Warning! Make sure that your wires connecting to the wall outlet are secure and are rated to handle the current! Please be careful when handling the contacts when the cable is plugged into a wall outlet. Touching the contacts while powered could result in injury.

⚡ Each relay takes just over 100 mA to power Daisy chaining these boards together on the same bus will result in pretty large power spikes, you may need a separate 3.3V source depending on what else is included in your project

Example Code

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.

The Qwiic Relay is pretty simple, so all of the functions to control it are simply contained in the examples, which can be downloaded from the GitHub repo by clicking the button below.

Example 1 - Basic Control

Go ahead and unzip the folder to a directory of your choosing and open up Example1-Basic_control. In this example, we’ll simply have the relay toggle on for 2 seconds, then off for two seconds. Let’s first look at the available functions in our sketch. The first two, relayOn() and relayOff(), are pretty self explanatory, they toggle the relay and return a message to the Serial monitor if no slave is found . The testForConnectivity() function simply tests to see if there is a slave at the proper address (0x18). These functions can be found below.

Now that we have these functions declared (Arduino handles prototype declarations for us), we can use them in our void setup() and void loop(). In our setup() function, we simply use the testForConnectivity() function to do just that. We then move on the void loop() where we turn the relay on, wait 2 seconds, turn the relay off, wait two seconds, and repeat. This code from Example 1 is shown below.

Opening your serial monitor to a baud rate of 9600 should show something similar to the output below if everything is connected properly. You should also be able to hear the relay clicking on and off. If things aren’t connected properly, you’ll see the message Check connections. No slave attached..

Example 1 Output

Example 2 - Change I2C Address

To get started with the second example, open up Example2-Change_I2C_Address In this example, we simply change the address to 0x19. First, let’s take a look at the changeAddress() function, which checks to see if the address is valid (Between 0x07 and 0x78) and changes the relay’s address to that. The function returns true if successful.

Now that we have these functions declared (Arduino handles prototype declarations for us), we can use them in our void setup() and void loop(). In our setup() function, we change the address to 0x19, check to see that the address change was a success, and toggle the relay on and off on this new address. The sketch that handles this is shown below.

Example 3 - I2C Scanner

To get started with the third example, open up Example3-I2C_Scanner In this example, we simply scan the I2C bus for devices, useful if we’ve been changing around the address of our relay and have since forgotten what it was. The example comes from Arduino, and the code can be shown below. We basically check for an ACK at each address, and output that address when we get one back.

Opening your serial monitor to a baud rate of 9600 will show what you have on your I2C bus and should look something like the below image.

I2C Scanner

Example 4 - Get Relay Status

The fourth example simply gets the current status of the relay. To get started with this example, open up Example4-Get_Relay_Status. We simply request a byte from the COMMAND_STATUS register on the ATTiny85, and output that byte, this function returns a 1 if the relay is on, 0 if it’s off, and a -1 if there’s an error.

Opening your serial monitor to a baud rate of 9600 will show you the current status of the relay.

Relay Status

Example 5 - Get Firmware Version

The Qwiic relay is controlled by an ATTiny85. SparkFun gives it some firmware to get it started, this final example shows you how to check which firmware version your ATTiny85 is using. Go ahead and open up Example5-Get_Firmware_Version to get started. Checking out the getFirmwareVersion() function, we see that it requests 2 bytes from the COMMAND_FIRMWARE_VERSION register (0x04) and returns them as a float. We simply call this function in our setup() function to get the firmware version.

Opening your serial monitor to a baud rate of 9600 will show you the current firmware version, and should look something like the below image.

Firmware Version

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!

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.

In 2003, CU student Nate Seidle fried a power supply in his dorm room and, in lieu of a way to order easy replacements, decided to start his own company. Since then, SparkFun has been committed to sustainably helping our world achieve electronics literacy from our headquarters in Boulder, Colorado.

No matter your vision, SparkFun's products and resources are designed to make the world of electronics more accessible. In addition to over 2,000 open source components and widgets, SparkFun offers curriculum, training and online tutorials designed to help demystify the wonderful world of embedded electronics. We're here to help you start something.