Skip to content

Commit 0339da3

Browse files
committed
Add high_side boolean to switch logic between high and low side thermistor. Defaults to high side (old behavior) for compatibility.
1 parent 2d57c0b commit 0339da3

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

adafruit_thermistor.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
A thermistor is a resistor that varies with temperature. This driver takes the
2727
parameters of that resistor and its series resistor to determine the current
2828
temperature. To hook one up, connect an analog input pin to the connection
29-
between the resistor and the thermistor.
29+
between the resistor and the thermistor. Be careful to note if the thermistor
30+
is connected on the high side (from analog input up to high logic level/3.3 or
31+
5 volts) or low side (from analog input down to ground). The initializer takes
32+
an optional high_side boolean that defaults to True and indicates if that the
33+
thermistor is connected on the high side vs. low side.
3034
3135
* Author(s): Scott Shawcroft
3236
@@ -52,19 +56,25 @@
5256
class Thermistor:
5357
"""Thermistor driver"""
5458

55-
def __init__(self, pin, series_resistor, nominal_resistance, nominal_temperature, b_coefficient):
59+
def __init__(self, pin, series_resistor, nominal_resistance, nominal_temperature, b_coefficient, high_side=True):
5660
self.pin = analogio.AnalogIn(pin)
5761
self.series_resistor = series_resistor
5862
self.nominal_resistance = nominal_resistance
5963
self.nominal_temperature = nominal_temperature
6064
self.b_coefficient = b_coefficient
65+
self.high_side = high_side
6166

6267
@property
6368
def temperature(self):
6469
"""The temperature of the thermistor in celsius"""
65-
reading = self.pin.value / 64
66-
reading = (1023 * self.series_resistor) / reading
67-
reading -= self.series_resistor
70+
if self.high_side:
71+
# Thermistor connected from analog input to high logic level.
72+
reading = self.pin.value / 64
73+
reading = (1023 * self.series_resistor) / reading
74+
reading -= self.series_resistor
75+
else:
76+
# Thermistor connected from analog input to ground.
77+
reading = self.series_resistor / (65535.0/self.pin.value - 1.0)
6878

6979
steinhart = reading / self.nominal_resistance # (R/Ro)
7080
steinhart = math.log(steinhart) # ln(R/Ro)

0 commit comments

Comments
 (0)