Skip to content

Commit d346150

Browse files
author
Elias Santistevan
committed
Adds two examples, updates keywords file, updates library.properties file, changes name of relayStatus function in library
1 parent 6739c89 commit d346150

File tree

7 files changed

+237
-11
lines changed

7 files changed

+237
-11
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
This example code gives you all the functionality of the SparkFun Qwiic Relay
3+
Arduino Library. It shows you how to get the firmware version of the board
4+
you're using, how to turn on and off a relay, toggle a relay, and get the
5+
status of the relay.
6+
By: Elias Santistevan
7+
SparkFun Electronics
8+
Date: July 2019
9+
10+
License: This code is public domain but you buy me a beer if you use
11+
this and we meet someday (Beerware license).
12+
*/
13+
14+
#include <Wire.h>
15+
#include "SparkFun_Qwiic_Relay.h"
16+
17+
#define RELAY_ADDR 0x18 // Alternate address 0x19
18+
19+
20+
Qwiic_Relay relay(RELAY_ADDR);
21+
22+
void setup()
23+
{
24+
Wire.begin();
25+
Serial.begin(115200);
26+
27+
// Let's see
28+
if(!relay.begin())
29+
Serial.println("Check connections to Qwiic Relay.");
30+
else
31+
Serial.println("Ready to flip some switches.");
32+
33+
float version = relay.singleRelayVersion();
34+
Serial.print("Firmware Version: ");
35+
Serial.println(version);
36+
37+
// Let's turn on the relay...
38+
relay.turnRelayOn();
39+
delay(500);
40+
// Let's turn that relay off...
41+
relay.turnRelayOff();
42+
delay(500);
43+
// Let's 'toggle' the relay; if it's off turn it on and vice versa.
44+
relay.toggleRelay();
45+
delay(500);
46+
// Toggle the relay back off.
47+
relay.toggleRelay();
48+
delay(500);
49+
50+
Serial.print("The Relay is now: ");
51+
// Is the relay on or off?
52+
int state = relay.getState();
53+
if(state == 1)
54+
Serial.print("On!");
55+
else if(state == 0)
56+
Serial.print("Off!");
57+
58+
}
59+
60+
void loop()
61+
{
62+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
This example code gives you all the functionality of the SparkFun Qwiic Quad Relay
3+
Arduino Library. It gives you the function to turn on and off each relay, toggle
4+
each relay, turn them all on and off, toggle them all, and how to check their
5+
state (on or off?). In general each function needs the number of the relay
6+
to activate.
7+
By: Elias Santistevan
8+
SparkFun Electronics
9+
Date: July 2019
10+
11+
License: This code is public domain but you buy me a beer if you use
12+
this and we meet someday (Beerware license).
13+
*/
14+
15+
#include <Wire.h>
16+
#include "SparkFun_Qwiic_Relay.h"
17+
18+
#define RELAY_ADDR 0x6D // Alternate address 0x6C
19+
20+
21+
Qwiic_Relay quadRelay(RELAY_ADDR);
22+
23+
void setup()
24+
{
25+
Wire.begin();
26+
Serial.begin(115200);
27+
28+
// Let's make sure the hardware is set up correctly.
29+
if(!quadRelay.begin())
30+
Serial.println("Check connections to Qwiic Relay.");
31+
else
32+
Serial.println("Ready to flip some switches.");
33+
34+
Serial.println("Let's turn each relay on, one at a time.");
35+
// To turn on a relay give the function the number you want to turn on (or
36+
// off).
37+
quadRelay.turnRelayOn(1);
38+
delay(200);
39+
quadRelay.turnRelayOn(2);
40+
delay(200);
41+
quadRelay.turnRelayOn(3);
42+
delay(200);
43+
quadRelay.turnRelayOn(4);
44+
delay(1000);
45+
46+
// You can turn off all relays at once with the following function...
47+
Serial.println("Now let's turn them all off.");
48+
quadRelay.turnAllRelaysOff();
49+
delay(1000);
50+
51+
// Toggle is a bit different then using the on and off functions. It will
52+
// check the current state of the relay (on or off) and turn it to it's
53+
// opposite state: on ---> off or off -----> on.
54+
Serial.println("Toggle relay one and two.");
55+
quadRelay.toggleRelay(1);
56+
delay(200);
57+
quadRelay.toggleRelay(2);
58+
delay(1000);
59+
60+
// Let's turn off relay one and two....
61+
Serial.println("Turn off relay one and two.");
62+
quadRelay.turnRelayOff(1);
63+
delay(200);
64+
quadRelay.turnRelayOff(2);
65+
delay(1000);
66+
67+
// You can turn all the relays on at once with this function.
68+
Serial.println("Turn them all on once again.");
69+
quadRelay.turnAllRelaysOn();
70+
delay(1000);
71+
72+
// And finally you can toggle each relay at once. It's helpful when you have
73+
// two relays on but need them off, and also need the other two relays on.
74+
Serial.println("....and turn them off again.");
75+
quadRelay.toggleAllRelays();
76+
77+
// Finally we can see if the relays are on or off without physically looking
78+
// at them. Once again, give the function the number of the relay you want to
79+
// check.
80+
Serial.print("Relay One is now: ");
81+
// Is the relay on or off?
82+
int state = quadRelay.getState(1);
83+
if(state == 1)
84+
Serial.println("On!");
85+
else if(state == 0)
86+
Serial.println("Off!");
87+
delay(1000);
88+
89+
// Turn on relay two just to get a sense of how this works.
90+
Serial.print("Relay two is now: ");
91+
quadRelay.turnRelayOn(2);
92+
state = quadRelay.getState(2);
93+
if(state == 1)
94+
Serial.println("On!");
95+
else if(state == 0)
96+
Serial.println("Off!");
97+
98+
Serial.println("Now they're all off...");
99+
// Show's over people...
100+
quadRelay.turnAllRelaysOff();
101+
102+
}
103+
104+
void loop()
105+
{
106+
}

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
SparkFun Single and Qwiic Quad Relay Arduino Library
2+
========================================
3+
4+
<table class="table table-hover table-striped table-bordered">
5+
<tr align="center">
6+
<td><a href="https://www.sparkfun.com/products/15093"><img src="https://cdn.sparkfun.com/assets/parts/1/3/4/5/1/15093-SparkFun_Qwiic_Single_Relay-01.jpg"></a></td>
7+
<td><a href="https://www.sparkfun.com/products/15102"><img src="https://cdn.sparkfun.com/assets/parts/1/3/4/6/2/15102-SparkFun_Qwiic_Quad_Relay-01a.jpg"></a></td>
8+
</tr>
9+
<tr align="center">
10+
<td><a href="https://www.sparkfun.com/products/15093">SparkFun Qwiic Single Relay (COM-15093)</a></td>
11+
<td><a href="https://www.sparkfun.com/products/15102">SparkFun Qwiic Quad Relay (COM-15102)</a></td>
12+
</tr>
13+
</table>
14+
15+
The Single and Quad relays make it possible to control high powered devices
16+
using a low powered device like an Arduino. These relays are Qwiic enabled
17+
which means they are plug and play, and now with their very own library it's
18+
easier then ever to get started.
19+
20+
Repository Contents
21+
-------------------
22+
23+
* **/examples** - Example code for the Arduino IDE
24+
* **/src** - Source files for the library (.cpp and .h files).
25+
* **/keywords.txt** - Keywords from the library that are highlighted in Arduino IDE.
26+
* **/library.properties** - General Library properties for the Arduino Package Manager.
27+
28+
Documentation
29+
--------------
30+
31+
* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
32+
33+
Products that use this library
34+
--------------
35+
* [COM-15093](https://www.sparkfun.com/products/15093) - SparkFun Qwiic Single Relay
36+
* [COM-15102](https://www.sparkfun.com/products/15102) - SparkFun Qwiic Quad Relay
37+
38+
License Information
39+
-------------------
40+
41+
This product is _**open source**_!
42+
43+
Various bits of the code have different licenses applied. Anything SparkFun wrote is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round!
44+
45+
Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license.
46+
47+
Distributed as-is; no warranty is given.
48+
49+
- Your friends at SparkFun.
50+

keywords.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
SparkFun_Qwiic_Relay_Arduino_Library KEYWORD1
1+
Qwiic_Relay KEYWORD1
2+
begin KEYWORD2
3+
turnRelayOn KEYWORD2
4+
turnRelayOff KEYWORD2
5+
toggleRelay KEYWORD2
6+
relayStatus KEYWORD2
7+
singleRelayVersion KEYWORD2
8+
turnAllRelaysOn KEYWORD2
9+
turnAllRelaysOff KEYWORD2
10+
toggleAllRelays KEYWORD2
211

3-
begin KEYWORD2

library.properties

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=SparkFun Qwiic Relay Arduino Library
22
version=1.0.0
33
author=Elias Santistevan
4-
maintainer=SparkFun Electronics
4+
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Library for the SparkFun Quad and Single Relay Boards
6-
paragraph=
7-
category=Sensors
8-
url=
9-
architecture=*
6+
paragraph=The SparkFun Qwiic Relay Arduino Library makes it very easy to use the Qwiic enabled relays from SparkFun Electronics. It gives you easy to use functions to turn on and off relays, toggle them, and check their status. Also provided is example code for each board (<a href="https://www.sparkfun.com/products/15093"><SparkFun Single Qwiic Relay</a> and the <a href="https://www.sparkfun.com/products/15102"><SparkFun Qwiic Quad Relay</a>) that gives instructions on how to use the library to its fullest.
7+
category=Device Control
8+
url=https://github.com/sparkfun/SparkFun_Qwiic_Relay_Arduino_Library
9+
architectures=*

src/SparkFun_Qwiic_Relay.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void Qwiic_Relay::toggleRelay()
5555

5656
// This function for the SparkFun Single Relay, gets the status of the relay:
5757
// whether on: 1 or off: 0;
58-
uint8_t Qwiic_Relay::relayStatus()
58+
uint8_t Qwiic_Relay::getState()
5959
{
6060
uint8_t status = _readCommand(STATUS);
6161
return status;
@@ -121,7 +121,7 @@ void Qwiic_Relay::toggleAllRelays()
121121

122122
// This function for the SparkFun Quad Relay, gets the status of the relay:
123123
// whether on: 1 or off: 0;
124-
uint8_t Qwiic_Relay::relayStatus(uint8_t relay)
124+
uint8_t Qwiic_Relay::getState(uint8_t relay)
125125
{
126126

127127
uint8_t status;

src/SparkFun_Qwiic_Relay.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Qwiic_Relay
8080

8181
// This function for the SparkFun Single Relay, gets the status of the relay:
8282
// whether on: 1 or off: 0;
83-
uint8_t relayStatus();
83+
uint8_t getState();
8484

8585
// This function gets the version number of the SparkFun Single Relay.
8686
float singleRelayVersion();
@@ -112,7 +112,7 @@ class Qwiic_Relay
112112

113113
// This function for the SparkFun Quad Relay, gets the status of the relay:
114114
// whether on: 1 or off: 0;
115-
uint8_t relayStatus(uint8_t relay);
115+
uint8_t getState(uint8_t relay);
116116

117117
private:
118118

0 commit comments

Comments
 (0)