Skip to content

Commit aec528b

Browse files
authored
Merge pull request #17 from jposada202020/adding_critical_temp_features
Adding critical temp features
2 parents e08a56f + 15e2861 commit aec528b

File tree

3 files changed

+91
-18
lines changed

3 files changed

+91
-18
lines changed

README.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ To install in a virtual environment in your current project:
5555
Usage Example
5656
=============
5757

58-
.. code-block:: python
58+
.. code-block:: python3
5959
60-
import time
61-
import board
62-
import adafruit_adt7410
60+
import time
61+
import board
62+
import adafruit_adt7410
6363
64-
i2c_bus = board.I2C()
65-
adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
66-
adt.high_resolution = True
64+
i2c = board.I2C() # uses board.SCL and board.SDA
65+
adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
66+
adt.high_resolution = True
6767
68-
while True:
69-
print(adt.temperature)
70-
time.sleep(0.5)
68+
while True:
69+
print(adt.temperature)
70+
time.sleep(0.5)
7171
7272
7373
Documentation

adafruit_adt7410.py

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,28 @@
99
CircuitPython driver for reading temperature from the Analog Devices ADT7410
1010
precision temperature sensor
1111
12-
* Author(s): ladyada
12+
* Author(s): ladyada, Jose David M.
1313
1414
Implementation Notes
1515
--------------------
1616
1717
**Hardware:**
1818
19-
* `Adafruit's ADT7410 analog temperature Sensor Breakout:
19+
* `Adafruit ADT7410 analog temperature Sensor Breakout
2020
<https://www.adafruit.com/product/4089>`_ (Product ID: 4089)
2121
22+
2223
**Software and Dependencies:**
2324
2425
* Adafruit CircuitPython firmware for the supported boards:
25-
https://github.com/adafruit/circuitpython/releases
26+
https://circuitpython.org/downloads
27+
28+
* Adafruit's Bus Device library:
29+
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2630
27-
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
31+
* Adafruit's Register library:
32+
https://github.com/adafruit/Adafruit_CircuitPython_Register
2833
29-
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
3034
"""
3135

3236

@@ -44,6 +48,13 @@
4448
_ADT7410_TEMPLSB = const(0x1)
4549
_ADT7410_STATUS = const(0x2)
4650
_ADT7410_CONFIG = const(0x3)
51+
_ADT7410_THIGHMSB = const(0x4)
52+
_ADT7410_THIGHLSB = const(0x5)
53+
_ADT7410_TLOWMSB = const(0x6)
54+
_ADT7410_TLOWLSB = const(0x7)
55+
_ADT7410_TCRITMSB = const(0x8)
56+
_ADT7410_TCRITLSB = const(0x9)
57+
_ADT7410_THYST = const(0x0A)
4758
_ADT7410_ID = const(0xB)
4859
_ADT7410_SWRST = const(0x2F)
4960

@@ -85,6 +96,10 @@ class ADT7410:
8596
intpin_polarity = RWBit(_ADT7410_CONFIG, 3)
8697
comparator_mode = RWBit(_ADT7410_CONFIG, 4)
8798
high_resolution = RWBit(_ADT7410_CONFIG, 7)
99+
# Status Information configuration
100+
temp_over_critiq = ROBit(_ADT7410_STATUS, 6)
101+
temp_over_high = ROBit(_ADT7410_STATUS, 5)
102+
temp_under_low = ROBit(_ADT7410_STATUS, 4)
88103

89104
def __init__(self, i2c_bus, address=0x48):
90105
self.i2c_device = I2CDevice(i2c_bus, address)
@@ -116,7 +131,7 @@ def configuration(self):
116131

117132
@configuration.setter
118133
def configuration(self, val):
119-
return self._write_register(_ADT7410_CONFIG, val)
134+
self._write_register(_ADT7410_CONFIG, val)
120135

121136
def reset(self):
122137
"""Perform a software reset"""
@@ -139,3 +154,61 @@ def _write_register(self, addr, data=None):
139154
end = 2
140155
with self.i2c_device as i2c:
141156
i2c.write(self._buf, end=end)
157+
158+
@property
159+
def high_temperature(self):
160+
"""The over temperature limit value in Celsius"""
161+
temp = self._read_register(_ADT7410_THIGHMSB, 2)
162+
return struct.unpack(">h", temp)[0] / 128
163+
164+
@high_temperature.setter
165+
def high_temperature(self, value):
166+
value = struct.pack(">h", int(value * 128))
167+
self._write_register(_ADT7410_THIGHMSB, value[0])
168+
self._write_register(_ADT7410_THIGHLSB, value[1])
169+
170+
@property
171+
def low_temperature(self):
172+
"""The over temperature limit value in Celsius. Only works when
173+
comparator mode is selected"""
174+
temp = self._read_register(_ADT7410_TLOWMSB, 2)
175+
return struct.unpack(">h", temp)[0] / 128
176+
177+
@low_temperature.setter
178+
def low_temperature(self, value):
179+
value = struct.pack(">h", int(value * 128))
180+
self._write_register(_ADT7410_TLOWMSB, value[0])
181+
self._write_register(_ADT7410_TLOWLSB, value[1])
182+
183+
@property
184+
def critical_temperature(self):
185+
"""The critical temperature limit value in Celsius. Only works when
186+
comparator mode is selected"""
187+
temp = self._read_register(_ADT7410_TCRITMSB, 2)
188+
return struct.unpack(">h", temp)[0] / 128
189+
190+
@critical_temperature.setter
191+
def critical_temperature(self, value):
192+
"""The over temperature limit value in Celsius
193+
There is a bug in the sensor, so the address 0x09 could no be written to 0x00
194+
for this reason only odd numbers could be given. We could make the 0x09 with
195+
a value of 0x01, however make the logic more complex. Only works when
196+
comparator mode is selected
197+
"""
198+
value = struct.pack(">h", int(value * 128))
199+
self._write_register(_ADT7410_TCRITMSB, value[0])
200+
self._write_register(_ADT7410_TCRITLSB, value[1])
201+
202+
@property
203+
def hysteresis(self):
204+
"""The hysteresis temperature limit value in Celsius. Only works when
205+
comparator mode is selected. From 0 to 15 Celsius"""
206+
temp = self._read_register(_ADT7410_THYST)[0]
207+
return temp
208+
209+
@hysteresis.setter
210+
def hysteresis(self, value):
211+
if value > 15 or isinstance(value, float):
212+
raise Exception("Hysteresis value must be an integer lower than 15 Celsius")
213+
214+
self._write_register(_ADT7410_THYST, value)

examples/adt7410_simpletest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import board
66
import adafruit_adt7410
77

8-
i2c_bus = board.I2C()
9-
adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
8+
i2c = board.I2C() # uses board.SCL and board.SDA
9+
adt = adafruit_adt7410.ADT7410(i2c, address=0x48)
1010
adt.high_resolution = True
1111

1212
while True:

0 commit comments

Comments
 (0)