It's easy to use a thermistor with CircuitPython and your board's built-in analog to digital converters.  Just like with the Arduino example on the previous page you can connect the thermistor to a board's analog input and read the resistance.  As the temperature changes the resistance changes and you can convert that resistance into a precise temperature value with Python code!

Before you get started it will help to familiarize yourself with analog inputs in CircuitPython.

Next wire up a thermistor to your board exactly as shown on the previous page.  You need a fixed resistor (typically 10 kilo-ohms) connected from an analog input up to 3.3V.  Then connect one pin from the thermistor to the same analog input and the other pin to your board's ground.  In this example we'll use analog input A1 on a Feather M0 basic.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

You can import the necessary board and analogio modules by running:

```import board
import analogio```

Now create an analog input using the pin you've connected to the thermistor:

`thermistor = analogio.AnalogIn(board.A1)`

At this point you can read the raw ADC value from the thermistor:

`thermistor.value`

The raw value isn't very interesting to us though, we really want to convert it to a resistance and temperature value.  One thing to note though is that this raw value will always be in the range from 0 to 65535, unlike in Arduino where it ranges from 0 to 1023.  As the resistance of the thermistor changes (based on temperature changes) this raw value will change too.

Using the same equation as the previous page you can calculate the resistance of the thermistor:

```R = 10000 / (65535/thermistor.value - 1)
print('Thermistor resistance: {} ohms'.format(R))```

Remember if you're using a different size resistor your might need to change the equation above and the 10000 value inside it!

Convert to Temperature

Converting the thermistor's resistance to temperature is just like you saw on the previous page with Arduino.  You can use a special equation and some known parameters about the thermistor to perform this conversion in Python code.

First make sure you know these values for your thermistor (check its datasheet if available):

• Ro - The resistance at a nominal temperature value.  This is typically 10,000 ohms.
• To - The temperature (in Celsius) at the nominal resistance value above.  This is typically 25.0 degrees C.
• Beta - The beta coefficient value for the thermistor.  Typically this is a value in the range of 3000-4000, for example 3950.

We can now solve the simplified B coefficient Steinhart-Hart equation mentioned on the previous page.  Here's a function you can define to perform this math:

```def steinhart_temperature_C(r, Ro=10000.0, To=25.0, beta=3950.0):
import math
steinhart = math.log(r / Ro) / beta      # log(R/Ro) / beta
steinhart += 1.0 / (To + 273.15)         # log(R/Ro) / beta + 1/To
steinhart = (1.0 / steinhart) - 273.15   # Invert, convert to C
return steinhart```

Now call the function and pass it the thermistor resistance that you calculated.  You can pass in explicit Ro, To, and beta parameters too or use the defaults (10k, 25.0C, 3950):

```R = 10000 / (65535/thermistor.value - 1)
steinhart_temperature_C(R)```

Or if you're passing in explicit Ro, To, beta parameters:

`steinhart_temperature_C(R, Ro=10000.0, To=25.0, beta=3950)`

Now you have the temperature from the thermistor as a value in degrees Celsius!

Thermistor Module

If you just want to read the value of a thermistor you can actually use a handy CircuitPython module to perform all the above calculations for you automatically.  To use the thermistor module sensor with your Adafruit CircuitPython board you'll need to install the Adafruit_CircuitPython_Thermistor module on your board. Remember this module is for Adafruit CircuitPython firmware and not MicroPython.org firmware!

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  For example the Circuit Playground Express guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:

Before continuing make sure your board's lib folder or root filesystem has the adafruit_thermistor.mpy module copied over.

Usage

To demonstrate the usage of the thermistor  module you can connect to your board's serial REPL and run Python code to read the temperature and humidity.

First connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Next import the board and adafruit_thermistor modules, these are necessary modules to initialize and access the sensor:

```import board

Now create an instance of the Thermistor class from the module.  You'll need to know the Ro, To, and beta parameters for your thermistor just like when performing the math yourself.  For example with the same thermistor setup as before you would run:

`thermistor = adafruit_thermistor.Thermistor(board.A1, 10000.0, 10000.0, 25.0, 3950.0, high_side=False)`

Let's break down all the parameters sent to the Thermistor initializer:

• Analog input - The first parameter is the name of the analog input connected to the thermistor, board pin A1 in this case.
• Series resistance - The second parameter is the value of the series resistor connected to the thermistor.  If you're following this guide you want a value of 10,000 ohms.
• Nominal resistance (Ro) - The third parameter is the value of the thermistor's resistance at a nominal temperature.  For the thermistor in this guide use the same 10,000 ohms value.
• Nominal temperature (To) - The fourth parameter is the value of the thermistor's temperature (in degrees Celsius) at a nominal resistance value.  For the thermistor in this guide use the same 25.0 degree value.
• Beta coefficient - The fifth parameter is the beta coefficient for your thermistor, 3950 in this case.
• High_side boolean - The sixth parameter is optional and indicates if the thermistor is connected on the high side or low side of the resistor voltage divider.  For this guide we've actually wired up the thermistor on the low side, or from the ADC input down to ground.  However for other boards and usage you might wire the thermistor the opposite way from the high side, or from ADC input up to 3.3V or 5V.  The default value for this high_side parameter is true but for the wiring in this guide we need to tell it that we're using low side wiring by setting high_side to false.

Once the thermistor instance is created you can read the temperature property to get the temperature value in degrees Celsius:

`thermistor.temperature`

That's all you need to read the temperature of a thermistor with the thermistor module!  Internally the module will do all the necessary Steinhart-Hart equation math for you.  You can grab the temperature result and use it in your own programs to add temperature sensing!

This guide was first published on Jul 29, 2012. It was last updated on Jul 29, 2012.