|
26 | 26 | A thermistor is a resistor that varies with temperature. This driver takes the
|
27 | 27 | parameters of that resistor and its series resistor to determine the current
|
28 | 28 | 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. |
30 | 34 |
|
31 | 35 | * Author(s): Scott Shawcroft
|
32 | 36 |
|
|
52 | 56 | class Thermistor:
|
53 | 57 | """Thermistor driver"""
|
54 | 58 |
|
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): |
56 | 60 | self.pin = analogio.AnalogIn(pin)
|
57 | 61 | self.series_resistor = series_resistor
|
58 | 62 | self.nominal_resistance = nominal_resistance
|
59 | 63 | self.nominal_temperature = nominal_temperature
|
60 | 64 | self.b_coefficient = b_coefficient
|
| 65 | + self.high_side = high_side |
61 | 66 |
|
62 | 67 | @property
|
63 | 68 | def temperature(self):
|
64 | 69 | """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) |
68 | 78 |
|
69 | 79 | steinhart = reading / self.nominal_resistance # (R/Ro)
|
70 | 80 | steinhart = math.log(steinhart) # ln(R/Ro)
|
|
0 commit comments