Skip to content

Commit 447c961

Browse files
authored
Merge pull request #13 from caternuson/iss12
Add NIST based temperature calculation
2 parents 5f51a02 + 750d9a1 commit 447c961

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

adafruit_max31855.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
https://github.com/adafruit/circuitpython/releases
4444
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4545
"""
46+
import math
4647
try:
4748
import struct
4849
except ImportError:
@@ -89,3 +90,82 @@ def temperature(self):
8990
def reference_temperature(self):
9091
"""Internal reference temperature in degrees Celsius."""
9192
return self._read(True) * 0.0625
93+
94+
@property
95+
def temperature_NIST(self):
96+
"""
97+
Thermocouple temperature in degrees Celsius, computed using
98+
raw voltages and NIST approximation for Type K, see:
99+
https://srdata.nist.gov/its90/download/type_k.tab
100+
"""
101+
# pylint: disable=bad-whitespace, bad-continuation, invalid-name
102+
# temperature of remote thermocouple junction
103+
TR = self.temperature
104+
# temperature of device (cold junction)
105+
TAMB = self.reference_temperature
106+
# thermocouple voltage based on MAX31855's uV/degC for type K (table 1)
107+
VOUT = 0.041276 * (TR - TAMB)
108+
# cold junction equivalent thermocouple voltage
109+
if TAMB >= 0:
110+
VREF =(-0.176004136860E-01 +
111+
0.389212049750E-01 * TAMB +
112+
0.185587700320E-04 * math.pow(TAMB, 2) +
113+
-0.994575928740E-07 * math.pow(TAMB, 3) +
114+
0.318409457190E-09 * math.pow(TAMB, 4) +
115+
-0.560728448890E-12 * math.pow(TAMB, 5) +
116+
0.560750590590E-15 * math.pow(TAMB, 6) +
117+
-0.320207200030E-18 * math.pow(TAMB, 7) +
118+
0.971511471520E-22 * math.pow(TAMB, 8) +
119+
-0.121047212750E-25 * math.pow(TAMB, 9) +
120+
0.1185976 * math.exp(-0.1183432E-03 * math.pow(TAMB - 0.1269686E+03, 2)))
121+
else:
122+
VREF =( 0.394501280250E-01 * TAMB +
123+
0.236223735980E-04 * math.pow(TAMB, 2) +
124+
-0.328589067840E-06 * math.pow(TAMB, 3) +
125+
-0.499048287770E-08 * math.pow(TAMB, 4) +
126+
-0.675090591730E-10 * math.pow(TAMB, 5) +
127+
-0.574103274280E-12 * math.pow(TAMB, 6) +
128+
-0.310888728940E-14 * math.pow(TAMB, 7) +
129+
-0.104516093650E-16 * math.pow(TAMB, 8) +
130+
-0.198892668780E-19 * math.pow(TAMB, 9) +
131+
-0.163226974860E-22 * math.pow(TAMB, 10))
132+
# total thermoelectric voltage
133+
VTOTAL = VOUT + VREF
134+
# determine coefficients
135+
# https://srdata.nist.gov/its90/type_k/kcoefficients_inverse.html
136+
if -5.891 <= VTOTAL <=0:
137+
DCOEF = (0.0000000E+00,
138+
2.5173462E+01,
139+
-1.1662878E+00,
140+
-1.0833638E+00,
141+
-8.9773540E-01,
142+
-3.7342377E-01,
143+
-8.6632643E-02,
144+
-1.0450598E-02,
145+
-5.1920577E-04)
146+
elif 0 < VTOTAL <= 20.644:
147+
DCOEF = (0.000000E+00,
148+
2.508355E+01,
149+
7.860106E-02,
150+
-2.503131E-01,
151+
8.315270E-02,
152+
-1.228034E-02,
153+
9.804036E-04,
154+
-4.413030E-05,
155+
1.057734E-06,
156+
-1.052755E-08)
157+
elif 20.644 < VTOTAL <= 54.886:
158+
DCOEF = (-1.318058E+02,
159+
4.830222E+01,
160+
-1.646031E+00,
161+
5.464731E-02,
162+
-9.650715E-04,
163+
8.802193E-06,
164+
-3.110810E-08)
165+
else:
166+
raise RuntimeError("Total thermoelectric voltage out of range:{}".format(VTOTAL))
167+
# compute temperature
168+
TEMPERATURE = 0
169+
for n, c in enumerate(DCOEF):
170+
TEMPERATURE += c * math.pow(VTOTAL, n)
171+
return TEMPERATURE

0 commit comments

Comments
 (0)