Skip to content

Commit a43a926

Browse files
committed
add NIST temp comp
1 parent 5f51a02 commit a43a926

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

adafruit_max31855.py

Lines changed: 79 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+
from math import exp, pow
4647
try:
4748
import struct
4849
except ImportError:
@@ -89,3 +90,81 @@ 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 temperatureNIST(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+
# temperature of remote thermocouple junction
102+
TR = self.temperature
103+
# temperature of device (cold junction)
104+
TAMB = self.reference_temperature
105+
# thermocouple voltage based on MAX31855's uV/degC for type K (table 1)
106+
VOUT = 0.041276 * (TR - TAMB)
107+
# cold junction equivalent thermocouple voltage
108+
if TAMB >= 0:
109+
VREF =(-0.176004136860E-01 +
110+
0.389212049750E-01 * TAMB +
111+
0.185587700320E-04 * pow(TAMB,2) +
112+
-0.994575928740E-07 * pow(TAMB,3) +
113+
0.318409457190E-09 * pow(TAMB,4) +
114+
-0.560728448890E-12 * pow(TAMB,5) +
115+
0.560750590590E-15 * pow(TAMB,6) +
116+
-0.320207200030E-18 * pow(TAMB,7) +
117+
0.971511471520E-22 * pow(TAMB,8) +
118+
-0.121047212750E-25 * pow(TAMB,9) +
119+
0.1185976 * exp(-0.1183432E-03 * pow(TAMB - 0.1269686E+03, 2)))
120+
else:
121+
VREF =( 0.394501280250E-01 * TAMB +
122+
0.236223735980E-04 * pow(TAMB, 2) +
123+
-0.328589067840E-06 * pow(TAMB, 3) +
124+
-0.499048287770E-08 * pow(TAMB, 4) +
125+
-0.675090591730E-10 * pow(TAMB, 5) +
126+
-0.574103274280E-12 * pow(TAMB, 6) +
127+
-0.310888728940E-14 * pow(TAMB, 7) +
128+
-0.104516093650E-16 * pow(TAMB, 8) +
129+
-0.198892668780E-19 * pow(TAMB, 9) +
130+
-0.163226974860E-22 * pow(TAMB, 10))
131+
# total thermoelectric voltage
132+
VTOTAL = VOUT + VREF
133+
# determine coefficients
134+
# https://srdata.nist.gov/its90/type_k/kcoefficients_inverse.html
135+
if -5.891 <= VTOTAL <=0:
136+
DCOEF = (0.0000000E+00,
137+
2.5173462E+01,
138+
-1.1662878E+00,
139+
-1.0833638E+00,
140+
-8.9773540E-01,
141+
-3.7342377E-01,
142+
-8.6632643E-02,
143+
-1.0450598E-02,
144+
-5.1920577E-04)
145+
elif 0 < VTOTAL <= 20.644:
146+
DCOEF = (0.000000E+00,
147+
2.508355E+01,
148+
7.860106E-02,
149+
-2.503131E-01,
150+
8.315270E-02,
151+
-1.228034E-02,
152+
9.804036E-04,
153+
-4.413030E-05,
154+
1.057734E-06,
155+
-1.052755E-08)
156+
elif 20.644 < VTOTAL <= 54.886:
157+
DCOEF = (-1.318058E+02,
158+
4.830222E+01,
159+
-1.646031E+00,
160+
5.464731E-02,
161+
-9.650715E-04,
162+
8.802193E-06,
163+
-3.110810E-08)
164+
else:
165+
raise RuntimeError("Total thermoelectric voltage out of range:{}".format(VTOTAL))
166+
# compute temperature
167+
TEMPERATURE = 0
168+
for n, c in enumerate(DCOEF):
169+
TEMPERATURE += c * pow(VTOTAL, n)
170+
return TEMPERATURE

0 commit comments

Comments
 (0)