/ adafruit_thermistor.py
adafruit_thermistor.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  """
 23  `adafruit_thermistor`
 24  ===========================================================
 25  
 26  A thermistor is a resistor that varies with temperature. This driver takes the
 27  parameters of that resistor and its series resistor to determine the current
 28  temperature. To hook one up, connect an analog input pin to the connection
 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.
 34  
 35  * Author(s): Scott Shawcroft
 36  
 37  Implementation Notes
 38  --------------------
 39  
 40  **Hardware:**
 41  
 42  * Adafruit `10K Precision Epoxy Thermistor - 3950 NTC <https://www.adafruit.com/products/372>`_
 43    (Product ID: 372)
 44  
 45  * Adafruit `Circuit Playground Express <https://www.adafruit.com/products/3333>`_
 46    (Product ID: 3333)
 47  
 48  **Software and Dependencies:**
 49  
 50  * Adafruit CircuitPython firmware: https://github.com/adafruit/circuitpython/releases
 51  
 52  **Notes:**
 53  
 54  #. Check the datasheet of your thermistor for the values.
 55  """
 56  
 57  import math
 58  import analogio
 59  
 60  __version__ = "0.0.0-auto.0"
 61  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Thermistor.git"
 62  
 63  
 64  class Thermistor:
 65      """Thermistor driver"""
 66  
 67      def __init__(
 68          self,
 69          pin,
 70          series_resistor,
 71          nominal_resistance,
 72          nominal_temperature,
 73          b_coefficient,
 74          *,
 75          high_side=True
 76      ):
 77          # pylint: disable=too-many-arguments
 78          self.pin = analogio.AnalogIn(pin)
 79          self.series_resistor = series_resistor
 80          self.nominal_resistance = nominal_resistance
 81          self.nominal_temperature = nominal_temperature
 82          self.b_coefficient = b_coefficient
 83          self.high_side = high_side
 84  
 85      @property
 86      def temperature(self):
 87          """The temperature of the thermistor in celsius"""
 88          if self.high_side:
 89              # Thermistor connected from analog input to high logic level.
 90              reading = self.pin.value / 64
 91              reading = (1023 * self.series_resistor) / reading
 92              reading -= self.series_resistor
 93          else:
 94              # Thermistor connected from analog input to ground.
 95              reading = self.series_resistor / (65535.0 / self.pin.value - 1.0)
 96  
 97          steinhart = reading / self.nominal_resistance  # (R/Ro)
 98          steinhart = math.log(steinhart)  # ln(R/Ro)
 99          steinhart /= self.b_coefficient  # 1/B * ln(R/Ro)
100          steinhart += 1.0 / (self.nominal_temperature + 273.15)  # + (1/To)
101          steinhart = 1.0 / steinhart  # Invert
102          steinhart -= 273.15  # convert to C
103  
104          return steinhart