Skip to content

Commit 0e288a6

Browse files
committed
updated class and added examples
1 parent 106b2e4 commit 0e288a6

7 files changed

+274
-25
lines changed

examples/example1_basic_control.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040

4141
try:
4242
while True:
43-
relay.on()
43+
relay.relay_on()
4444
sleep(2)
45-
relay.off()
45+
relay.relay_off()
4646
sleep(2)
4747

4848
except KeyboardInterrupt:
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# This is example is for the SparkFun Qwiic Single Relay.
2+
# SparkFun sells these at its website: www.sparkfun.com
3+
# Do you like this library? Help support SparkFun. Buy a board!
4+
# https://www.sparkfun.com/products/15093
5+
6+
"""
7+
Qwiic Relay Example 2 - example2_change_i2c_address.py
8+
Written by Gaston Williams, June 13th, 2019
9+
Based on Arduino code written by
10+
Wes Furuya @ SparkFun Electronics, February 5th, 2019
11+
The Qwiic Relay is a I2C controlled analog relay
12+
13+
Example 2 - Change I2C Address:
14+
This program uses the Qwiic Relay CircuitPython Library to change
15+
the I2C address for the device. You enter in the DEC value (8-119) or
16+
HEX value (0x08-0x77) for the new Relay address. After the address is
17+
You can run i2c_scanner.py to validate the address after the change.
18+
19+
Syntax: python3 change_i2c_address.py [address]
20+
where address is an optional address value in decimal or hex
21+
The default value for the address is 24 [0x18]
22+
"""
23+
24+
import sys
25+
import board
26+
import busio
27+
import sparkfun_qwiicrelay
28+
29+
# The default QwiicRelay i2c address is 0x18 (24)
30+
i2c_address = 0x18
31+
32+
# print('Arguement count: ' , len(sys.argv))
33+
# print('List: ' + str(sys.argv))
34+
35+
# If we were passed an arguement, then use it as the address
36+
if len(sys.argv) > 1:
37+
try:
38+
# check to see if hex or decimal arguement
39+
if '0x' in sys.argv[1]:
40+
i2c_address = int(sys.argv[1], 16)
41+
else:
42+
i2c_address = int(sys.argv[1])
43+
except ValueError:
44+
print('Ignoring invalid arguement: ' + str(sys.argv[1]))
45+
46+
# Show the initial address
47+
print('Current i2c address = ' + str(i2c_address)
48+
+ ' [' + hex(i2c_address) + ']')
49+
50+
# Create library object using our Bus I2C port
51+
i2c = busio.I2C(board.SCL, board.SDA)
52+
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c, i2c_address)
53+
54+
if relay.connected:
55+
print('Qwiic Relay Example.')
56+
else:
57+
# if we can't connecct, something is wrong so just quit
58+
print('Relay does not appear to be connected. Please check wiring.')
59+
exit()
60+
61+
print('Address: ' + str(i2c_address) + ' [' + hex(i2c_address) + ']')
62+
63+
text = input('Enter a new I2C address (as a decimal from 8 to 119 or hex 0x08 to 0x77):')
64+
65+
# check to see if hex or decimal value
66+
if '0x' in text:
67+
new_address = int(text, 16)
68+
else:
69+
new_address = int(text)
70+
71+
print('Changing address to ' + str(new_address)
72+
+ ' [' + hex(new_address) + ']')
73+
74+
result = relay.set_i2c_address(new_address)
75+
76+
if result:
77+
print('Address changed to ' + str(new_address)
78+
+ ' [' + hex(new_address) + ']')
79+
# After the change check the new connection and show firmware version
80+
if relay.connected:
81+
print('Connected to Relay after address change.')
82+
else:
83+
print('Error after address change. Cannot connect to Relay.')
84+
85+
else:
86+
print('Address change failed.')
87+
88+
# good advice whether the address changed worked or not
89+
print('Run example3_i2c_scanner.py to verify the Qwiic Relay address.')

examples/example3_i2c_scanner.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This is example is for the SparkFun Qwiic Single Relay.
2+
# SparkFun sells these at its website: www.sparkfun.com
3+
# Do you like this library? Help support SparkFun. Buy a board!
4+
# https://www.sparkfun.com/products/15093
5+
6+
"""
7+
Qwiic Relay Example 3 - example3_i2c_Scanner.py
8+
Written by Gaston Williams, June 17th, 2019
9+
The Qwiic Single Relay is an I2C controlled relay produced by sparkfun
10+
11+
Example 3 - I2C Scanner
12+
This progam uses CircuitPython BusIO library to find the current
13+
address of the Qwiic Relay. It uses the I2C Scanner Example from
14+
https://learn.adafruit.com/circuitpython-basics-i2c-and-spi/i2c-devices
15+
16+
The factory default address is 0x18.
17+
"""
18+
19+
import time
20+
21+
import board
22+
import busio
23+
24+
i2c = busio.I2C(board.SCL, board.SDA)
25+
26+
# For some reason i2c.scan() returns all addresses above the relay address
27+
# So we will manually look for the really address
28+
29+
def test_i2c_address(addr):
30+
"test an address to see if there's a device there"""
31+
while not i2c.try_lock():
32+
pass
33+
34+
try:
35+
i2c.writeto(addr, b'')
36+
except OSError:
37+
# some OS's dont like writing an empty bytesting...
38+
# Retry by reading a byte
39+
try:
40+
result = bytearray(1)
41+
i2c.readfrom_into(addr, result)
42+
except OSError:
43+
return False
44+
finally:
45+
i2c.unlock()
46+
47+
return True
48+
49+
50+
addresses = []
51+
52+
for address in range(0x08, 0x80):
53+
if(test_i2c_address(address)):
54+
print('Found relay at address ' + hex(address))
55+
exit()
56+
57+
print('No I2C device found.')
58+
59+

examples/example4_get_relay_status.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# This is example is for the SparkFun Qwiic Single Relay.
2+
# SparkFun sells these at its website: www.sparkfun.com
3+
# Do you like this library? Help support SparkFun. Buy a board!
4+
# https://www.sparkfun.com/products/15093
5+
6+
"""
7+
Qwiic Relay Example 4 - example4_get_relay_status.py
8+
Written by Gaston Williams, June 13th, 2019
9+
Based on Arduino code written by
10+
Kevin Kuwata @ SparkX, March 21, 2018
11+
The Qwiic Single Relay is an I2C controlled relay produced by sparkfun
12+
13+
Example 4 - Get Relay Status:
14+
This program uses the Qwiic Relay CircuitPython Library to
15+
get the current status of the Qwiic Relay. The relay responds
16+
with a 1 for on and a 0 for off.
17+
18+
Default Qwiic relay address is 0x18.
19+
"""
20+
21+
from time import sleep
22+
import board
23+
import busio
24+
import sparkfun_qwiicrelay
25+
26+
# Create bus object using our board's I2C port
27+
i2c = busio.I2C(board.SCL, board.SDA)
28+
29+
# Create relay object
30+
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c)
31+
32+
print('Qwiic Relay Example 4 Get Relay Status')
33+
34+
# Check if connected
35+
if relay.connected:
36+
print('Relay connected.')
37+
else:
38+
print('Relay does not appear to be connected. Please check wiring.')
39+
exit()
40+
41+
print('Type Ctrl-C to exit program.')
42+
43+
try:
44+
while True:
45+
relay.relay_on()
46+
print('The relay status is ', relay.status)
47+
sleep(2)
48+
relay.relay_off()
49+
print('The relay status is ', relay.status)
50+
sleep(2)
51+
52+
except KeyboardInterrupt:
53+
pass
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This is example is for the SparkFun Qwiic Single Relay.
2+
# SparkFun sells these at its website: www.sparkfun.com
3+
# Do you like this library? Help support SparkFun. Buy a board!
4+
# https://www.sparkfun.com/products/15093
5+
6+
"""
7+
Qwiic Relay Example 5 - example5_get_firmware_version.py
8+
Written by Gaston Williams, June 13th, 2019
9+
Based on Arduino code written by
10+
Kevin Kuwata @ SparkX, April 3, 2018
11+
The Qwiic Single Relay is an I2C controlled relay produced by sparkfun
12+
13+
Example 5 - Get Firmware Version:
14+
This program uses the Qwiic Relay CircuitPython Library to get the
15+
firmware version of Qwiic Single Relay breakout. If using version prior
16+
to (excluding) 1.0 the version number will be 25.5 or 26.5. Starting at
17+
version 1.0, the relay will respond with the correct firmware version.
18+
19+
Default Qwiic relay address is 0x18.
20+
"""
21+
22+
from time import sleep
23+
import board
24+
import busio
25+
import sparkfun_qwiicrelay
26+
27+
# Create bus object using our board's I2C port
28+
i2c = busio.I2C(board.SCL, board.SDA)
29+
30+
# Create relay object
31+
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c)
32+
33+
print('Qwicc Relay Example 5 Get Firmware Version')
34+
35+
# Check if connected
36+
if relay.connected:
37+
print('Relay connected.')
38+
else:
39+
print('Relay does not appear to be connected. Please check wiring.')
40+
exit()
41+
42+
print('Type Ctrl-C to exit program.')
43+
44+
try:
45+
while True:
46+
print('Firmware version: ' + relay.version)
47+
sleep(2)
48+
49+
except KeyboardInterrupt:
50+
pass

examples/qwiicrelay_simpletest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
i2c = busio.I2C(board.SCL, board.SDA)
2323

2424
# Create joystick object
25-
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c, debug=True
26-
)
25+
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c)
2726

2827
# Check if connected
2928
if relay.connected:
@@ -32,14 +31,16 @@
3231
print('Relay does not appear to be connected. Please check wiring.')
3332
exit()
3433

35-
print('Relay status ', relay.status)
34+
# Print firmware version and current status
35+
print('Firmware version ' + relay.version)
36+
print('Relay status ', relay.status)
3637

3738
# Turn the relay on and off
3839
print('Press Ctrl-C to exit program')
3940
while True:
40-
relay.on()
41+
relay.relay_on()
4142
print('Relay status ', relay.status)
4243
sleep(2)
43-
relay.off()
44+
relay.relay_off()
4445
print('Relay status ', relay.status)
4546
sleep(2)

0 commit comments

Comments
 (0)