Skip to content

Commit 99b60f9

Browse files
committed
added set relay status property
1 parent 52278c0 commit 99b60f9

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

docs/examples.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Ecamples
1616
:caption: examples/example1_basic_control.py
1717
:linenos:
1818

19-
2. Change I2C Addres - Change the device I2C address.
19+
2. Change I2C Address - Change the device I2C address.
2020

2121
.. literalinclude:: ../examples/example2_change_i2c_address.py
2222
:caption: example2_change_i2c_address.py
@@ -34,8 +34,15 @@ Ecamples
3434
:caption: examples/example4_get_relay_status.py
3535
:linenos:
3636

37-
1. Firmware Version - Display the firmware version string.
37+
5. Firmware Version - Display the firmware version string.
3838

3939
.. literalinclude:: ../examples/example5_get_firmware_version.py
4040
:caption: examples/example5_get_firmware_version.py
4141
:linenos:
42+
43+
6. Set Relay Status - Set relay status to turn relay on and off.
44+
45+
.. literalinclude:: ../examples/example6_set_relay_status.py
46+
:caption: examples/example6_set_relay_status.py
47+
:linenos:
48+

examples/example6_set_relay_status.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 6 - example6_set_relay_status.py
8+
Written by Gaston Williams, June 21st, 2019
9+
The Qwiic Single Relay is an I2C controlled relay produced by sparkfun
10+
11+
Example 6 - Set Relay Status:
12+
This program uses the Qwiic Relay CircuitPython Library to
13+
set the relay status property to turn the relay on and off.
14+
15+
16+
Default Qwiic relay address is 0x18.
17+
"""
18+
19+
from time import sleep
20+
import board
21+
import busio
22+
import sparkfun_qwiicrelay
23+
24+
# Create bus object using our board's I2C port
25+
i2c = busio.I2C(board.SCL, board.SDA)
26+
27+
# Create relay object
28+
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c)
29+
30+
print('Qwiic Relay Example 6 Set Relay Status')
31+
32+
# Check if connected
33+
if relay.connected:
34+
print('Relay connected.')
35+
else:
36+
print('Relay does not appear to be connected. Please check wiring.')
37+
exit()
38+
39+
print('Type Ctrl-C to exit program.')
40+
41+
try:
42+
while True:
43+
# Set status = 1/True is the same as relay.relay_on()
44+
relay.status = True
45+
print('The relay status is', bool(relay.status))
46+
sleep(2)
47+
# Set status = 0/False is the same as relay.relay_off()
48+
relay.status = False
49+
print('The relay status is', bool(relay.status))
50+
51+
sleep(2)
52+
53+
except KeyboardInterrupt:
54+
pass

sparkfun_qwiicrelay.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ def status(self):
109109

110110
return status[0] & 0xFF
111111

112+
@status.setter
113+
def status(self, value):
114+
"""Setting the status True turns relay on, False turns relay off."""
115+
if bool(value):
116+
self._write_command(_RELAY_ON)
117+
else:
118+
self._write_command(_RELAY_OFF)
112119

113120
# public functions
114121

0 commit comments

Comments
 (0)