Skip to content

Commit e32d372

Browse files
committed
Update examples
Previous examples were effectively hard coded for quad relays. Update with the following: Ex1: single relay Ex2: dual relay Ex3: quad relay Ex4: PWM Also add a title to each example so it's clear what each example is for
1 parent 453a3c8 commit e32d372

File tree

4 files changed

+295
-80
lines changed

4 files changed

+295
-80
lines changed

examples/qwiic_relay_ex1.py renamed to examples/qwiic_relay_ex1_single.py

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
#!/usr/bin/env python
2-
#-----------------------------------------------------------------------------
3-
# qwiic_relay_ex1.py
2+
#-------------------------------------------------------------------------------
3+
# qwiic_relay_ex1_single.py
44
#
5-
# Example that shows the basics of using the quad and dual relays.
6-
#------------------------------------------------------------------------
5+
# Example that shows the basics of using the single relay.
6+
#-------------------------------------------------------------------------------
77
#
8-
# Written by SparkFun Electronics, July 2020
8+
# Written by SparkFun Electronics, November 2023
99
#
10-
# This python library supports the SparkFun Electronics qwiic
11-
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatible) single
12-
# board computers.
10+
# This python library supports the SparkFun Electronics Qwiic sensor/board
11+
# ecosystem on Python compatible devices, such as the Raspberry Pi, MicroPython
12+
# and CircuitPython enabled microcontrollers, etc.
1313
#
1414
# More information on qwiic is at https://www.sparkfun.com/qwiic
1515
#
1616
# Do you like this library? Help support SparkFun. Buy a board!
1717
#
18-
#==================================================================================
19-
# Copyright (c) 2020 SparkFun Electronics
18+
#===============================================================================
19+
# Copyright (c) 2023 SparkFun Electronics
2020
#
2121
# Permission is hereby granted, free of charge, to any person obtaining a copy
2222
# of this software and associated documentation files (the "Software"), to deal
@@ -35,61 +35,45 @@
3535
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3636
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3737
# SOFTWARE.
38-
#==================================================================================
39-
# Example 1
40-
#
38+
#===============================================================================
39+
# Example 1 - Single
4140

4241
from __future__ import print_function
4342
import qwiic_relay
4443
import time
4544
import sys
4645

47-
48-
QUAD_RELAY = 0x6D
49-
SINGLE_RELAY = 0x18
50-
QUAD_SOLID_STATE_RELAY = 0x08
51-
52-
#Be sure to initialize your relay with the proper address.
53-
myRelays = qwiic_relay.QwiicRelay(QUAD_SOLID_STATE_RELAY)
46+
# Be sure to initialize your relay with the proper address.
47+
myRelay = qwiic_relay.QwiicRelay(qwiic_relay.SINGLE_RELAY_DEFUALT_ADDR)
48+
# myRelay = qwiic_relay.QwiicRelay(qwiic_relay.SINGLE_RELAY_JUMPER_CLOSE_ADDR)
5449

5550
def runExample():
51+
print("\nSparkFun Qwiic Relay Example 1 - Single\n")
5652

57-
print("\nSparkFun Qwiic Relay Example 1\n")
58-
59-
if myRelays.begin() == False:
53+
if myRelay.begin() == False:
6054
print("The Qwiic Relay isn't connected to the system. Please check your connection", \
6155
file=sys.stderr)
6256
return
6357

64-
#Turn on relays one and three
65-
myRelays.set_relay_on(1)
66-
myRelays.set_relay_on(3)
67-
time.sleep(1)
68-
69-
#Print the status of all relays
70-
for relayNum in range(4):
71-
current_status = None
72-
if myRelays.get_relay_state(relayNum) is True:
73-
current_status = "On"
74-
else:
75-
current_status = "Off"
76-
print("Status 1: " + current_status + "\n")
77-
78-
#Turn off 1 and 3, turn on 2 and 4
79-
myRelays.set_relay_off(1)
80-
myRelays.set_relay_on(2)
81-
myRelays.set_relay_off(3)
82-
myRelays.set_relay_on(4)
83-
time.sleep(1)
84-
58+
# Repeat a couple times
59+
for i in range(2):
60+
# Turn relay on
61+
myRelay.set_relay_on()
8562

86-
#Turn all relays on, then turn them all off
87-
myRelays.set_all_relays_on()
88-
time.sleep(1)
89-
90-
myRelays.set_all_relays_off()
91-
63+
# Print state of relay
64+
print("Relay state: " + str(myRelay.get_relay_state()))
65+
66+
# Wait a moment
67+
time.sleep(1)
68+
69+
# Turn relay off
70+
myRelay.set_relay_off()
71+
72+
# Print state of relay
73+
print("Relay state: " + str(myRelay.get_relay_state()))
9274

75+
# Wait a moment
76+
time.sleep(1)
9377

9478
if __name__ == '__main__':
9579
try:

examples/qwiic_relay_ex2_dual.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python
2+
#-----------------------------------------------------------------------------
3+
# qwiic_relay_ex2_dual.py
4+
#
5+
# Example that shows the basics of using the dual relays.
6+
#------------------------------------------------------------------------
7+
#
8+
# Written by SparkFun Electronics, November 2023
9+
#
10+
# This python library supports the SparkFun Electronics Qwiic sensor/board
11+
# ecosystem on Python compatible devices, such as the Raspberry Pi, MicroPython
12+
# and CircuitPython enabled microcontrollers, etc.
13+
#
14+
# More information on qwiic is at https://www.sparkfun.com/qwiic
15+
#
16+
# Do you like this library? Help support SparkFun. Buy a board!
17+
#
18+
#==================================================================================
19+
# Copyright (c) 2023 SparkFun Electronics
20+
#
21+
# Permission is hereby granted, free of charge, to any person obtaining a copy
22+
# of this software and associated documentation files (the "Software"), to deal
23+
# in the Software without restriction, including without limitation the rights
24+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
# copies of the Software, and to permit persons to whom the Software is
26+
# furnished to do so, subject to the following conditions:
27+
#
28+
# The above copyright notice and this permission notice shall be included in all
29+
# copies or substantial portions of the Software.
30+
#
31+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
# SOFTWARE.
38+
#==================================================================================
39+
# Example 2 - Dual
40+
41+
from __future__ import print_function
42+
import qwiic_relay
43+
import time
44+
import sys
45+
46+
# Be sure to initialize your relay with the proper address.
47+
myRelays = qwiic_relay.QwiicRelay(qwiic_relay.DUAL_SOLID_STATE_RELAY_DEFUALT_ADDR)
48+
# myRelays = qwiic_relay.QwiicRelay(qwiic_relay.DUAL_SOLID_STATE_RELAY_JUMPER_CLOSE_ADDR)
49+
50+
def runExample():
51+
52+
print("\nSparkFun Qwiic Relay Example 2 - Dual\n")
53+
54+
if myRelays.begin() == False:
55+
print("The Qwiic Relay isn't connected to the system. Please check your connection", \
56+
file=sys.stderr)
57+
return
58+
59+
# Turn on relay one
60+
myRelays.set_relay_on(1)
61+
62+
# Print the status of all relays
63+
print("Relay 1 state: " + str(myRelays.get_relay_state(1)))
64+
print("Relay 2 state: " + str(myRelays.get_relay_state(2)))
65+
print()
66+
67+
# Wait a moment
68+
time.sleep(1)
69+
70+
# Turn on relay two
71+
myRelays.set_relay_on(2)
72+
73+
# Print the status of all relays
74+
print("Relay 1 state: " + str(myRelays.get_relay_state(1)))
75+
print("Relay 2 state: " + str(myRelays.get_relay_state(2)))
76+
print()
77+
78+
# Wait a moment
79+
time.sleep(1)
80+
81+
# Turn off relay one
82+
myRelays.set_relay_off(1)
83+
84+
# Print the status of all relays
85+
print("Relay 1 state: " + str(myRelays.get_relay_state(1)))
86+
print("Relay 2 state: " + str(myRelays.get_relay_state(2)))
87+
print()
88+
89+
# Wait a moment
90+
time.sleep(1)
91+
92+
# Turn off relay two
93+
myRelays.set_relay_off(2)
94+
95+
# Print the status of all relays
96+
print("Relay 1 state: " + str(myRelays.get_relay_state(1)))
97+
print("Relay 2 state: " + str(myRelays.get_relay_state(2)))
98+
print()
99+
100+
# Wait a moment
101+
time.sleep(1)
102+
103+
104+
if __name__ == '__main__':
105+
try:
106+
runExample()
107+
except (KeyboardInterrupt, SystemExit) as exErr:
108+
print("\nEnding Example 2")
109+
sys.exit(0)

examples/qwiic_relay_ex3_quad.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env python
2+
#-----------------------------------------------------------------------------
3+
# qwiic_relay_ex2_quad.py
4+
#
5+
# Example that shows the basics of using the quad relays.
6+
#------------------------------------------------------------------------
7+
#
8+
# Written by SparkFun Electronics, November 2023
9+
#
10+
# This python library supports the SparkFun Electronics Qwiic sensor/board
11+
# ecosystem on Python compatible devices, such as the Raspberry Pi, MicroPython
12+
# and CircuitPython enabled microcontrollers, etc.
13+
#
14+
# More information on qwiic is at https://www.sparkfun.com/qwiic
15+
#
16+
# Do you like this library? Help support SparkFun. Buy a board!
17+
#
18+
#==================================================================================
19+
# Copyright (c) 2023 SparkFun Electronics
20+
#
21+
# Permission is hereby granted, free of charge, to any person obtaining a copy
22+
# of this software and associated documentation files (the "Software"), to deal
23+
# in the Software without restriction, including without limitation the rights
24+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
# copies of the Software, and to permit persons to whom the Software is
26+
# furnished to do so, subject to the following conditions:
27+
#
28+
# The above copyright notice and this permission notice shall be included in all
29+
# copies or substantial portions of the Software.
30+
#
31+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
# SOFTWARE.
38+
#==================================================================================
39+
# Example 3 - Quad
40+
41+
from __future__ import print_function
42+
import qwiic_relay
43+
import time
44+
import sys
45+
46+
# Be sure to initialize your relay with the proper address.
47+
myRelays = qwiic_relay.QwiicRelay(qwiic_relay.QUAD_RELAY_DEFUALT_ADDR)
48+
# myRelays = qwiic_relay.QwiicRelay(qwiic_relay.QUAD_RELAY_JUMPER_CLOSE_ADDR)
49+
# myRelays = qwiic_relay.QwiicRelay(qwiic_relay.QUAD_SOLID_STATE_RELAY_DEFUALT_ADDR)
50+
# myRelays = qwiic_relay.QwiicRelay(qwiic_relay.QUAD_SOLID_STATE_RELAY_JUMPER_CLOSE_ADDR)
51+
52+
def runExample():
53+
54+
print("\nSparkFun Qwiic Relay Example 3 - Quad\n")
55+
56+
if myRelays.begin() == False:
57+
print("The Qwiic Relay isn't connected to the system. Please check your connection", \
58+
file=sys.stderr)
59+
return
60+
61+
# Turn on relays one and three
62+
myRelays.set_relay_on(1)
63+
myRelays.set_relay_on(3)
64+
65+
# Print the status of all relays
66+
print("Relay 1 state: " + str(myRelays.get_relay_state(1)))
67+
print("Relay 2 state: " + str(myRelays.get_relay_state(2)))
68+
print("Relay 3 state: " + str(myRelays.get_relay_state(3)))
69+
print("Relay 4 state: " + str(myRelays.get_relay_state(4)))
70+
print()
71+
72+
# Wait a moment
73+
time.sleep(1)
74+
75+
# Turn off 1 and 3, turn on 2 and 4
76+
myRelays.set_relay_off(1)
77+
myRelays.set_relay_on(2)
78+
myRelays.set_relay_off(3)
79+
myRelays.set_relay_on(4)
80+
81+
# Print the status of all relays
82+
print("Relay 1 state: " + str(myRelays.get_relay_state(1)))
83+
print("Relay 2 state: " + str(myRelays.get_relay_state(2)))
84+
print("Relay 3 state: " + str(myRelays.get_relay_state(3)))
85+
print("Relay 4 state: " + str(myRelays.get_relay_state(4)))
86+
print()
87+
88+
# Wait a moment
89+
time.sleep(1)
90+
91+
# Turn all relays on
92+
myRelays.set_all_relays_on()
93+
94+
# Print the status of all relays
95+
print("Relay 1 state: " + str(myRelays.get_relay_state(1)))
96+
print("Relay 2 state: " + str(myRelays.get_relay_state(2)))
97+
print("Relay 3 state: " + str(myRelays.get_relay_state(3)))
98+
print("Relay 4 state: " + str(myRelays.get_relay_state(4)))
99+
print()
100+
101+
# Wait a moment
102+
time.sleep(1)
103+
104+
# Turn all relays off
105+
myRelays.set_all_relays_off()
106+
107+
# Print the status of all relays
108+
print("Relay 1 state: " + str(myRelays.get_relay_state(1)))
109+
print("Relay 2 state: " + str(myRelays.get_relay_state(2)))
110+
print("Relay 3 state: " + str(myRelays.get_relay_state(3)))
111+
print("Relay 4 state: " + str(myRelays.get_relay_state(4)))
112+
print()
113+
114+
if __name__ == '__main__':
115+
try:
116+
runExample()
117+
except (KeyboardInterrupt, SystemExit) as exErr:
118+
print("\nEnding Example 3")
119+
sys.exit(0)

0 commit comments

Comments
 (0)