Raspberry Pi - NTC Thermistor Temperature Sensor

A Raspberry Pi can stay switched on for months and run a script in the background the whole time. That makes it a patient keeper for slow changes: the water in a hydroponics tank, the soil of a greenhouse bed, or a battery box that should never get warm. All it needs is a probe that does not mind water. This tutorial instructs you how to use Raspberry Pi to read the temperature from a waterproof 10K NTC thermistor probe. The probe gives a resistance, not a number, and the 40-pin header of the Pi has no analog input. So an ADS1115 ADC module measures the voltage for the Pi and sends the result over I2C. In detail, we will learn:

https://newbiely.com/images/cover/raspberry-pi-ntc-thermistor-temperature-sensor.jpg

We suggest:

Hardware Preparation

1×Raspberry Pi 5
1×10K NTC Thermistor Waterproof Probe (with 10K resistor)
1×ADS1115 ADC Module
1×Jumper Wires
1×Optionally, Breadboard
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of NTC Thermistor Temperature Sensor

The DIYables NTC thermistor temperature sensor is a small stainless-steel tube, 5mm wide and 25mm long with a round tip, on a 0.5m black cable. The tube is sealed, so the tip can sit in water, in wet soil or in the air. There is no chip inside it, only a thermistor: a tiny resistor whose value changes a lot with temperature. NTC stands for Negative Temperature Coefficient, which simply means that the resistance goes down when the temperature goes up. The Raspberry Pi measures that resistance and turns it into degrees. One kit holds 5 probes and 5 matching 10K resistors, so you never need to buy the resistor separately.

10K NTC thermistor waterproof probe with 2-pin JST plug and 10K resistor
image source: diyables.io

The cable ends in a white JST XH2.54 2-pin plug. There is no adapter board: you push two male jumper wires straight into the two openings of the plug. A thermistor is just a resistor, so its two wires have no polarity, and it does not matter which one goes where. In this tutorial, the probe and the 10K resistor are used like this:

  • Wire 1 (either wire): This wire must be connected to the 3.3V supply. We take it from the VDD pin of the ADS1115 module, which already gets 3.3V from the Raspberry Pi
  • Wire 2: This wire must be connected to input A0 of the ADS1115 module, never directly to a GPIO pin of the Pi
  • 10K resistor: One leg must be connected to A0 too, and the other leg must be connected to ground (0V)

※ NOTE THAT:

The probe has no polarity, but ADS1115 modules are not all the same: the order of the VDD, GND, SCL, SDA and A0 pins can change from one manufacturer to another. Always follow the labels printed on your ADS1115 board, not only the position of the pins in the picture.

Key facts about the probe:

NTC Thermistor Temperature Sensor
Thermistor 10K NTC (10kΩ at 25°C), ±1%
B value (beta) 3950
Measuring range -20°C to 105°C
Probe waterproof stainless steel, 5mm x 25mm, round tip
Cable 0.5m, two black wires
Plug JST XH2.54, 2 pins
Dissipation constant 5mW/°C (typical)

The Raspberry Pi cannot measure ohms directly. The thermistor and the 10K resistor form a voltage divider between 3.3V and GND, and the ADS1115 measures the voltage in the middle, at A0. When the probe warms up, its resistance falls, so A0 moves up toward 3.3V. When the probe cools down, A0 moves down toward 0V. From that voltage, the script works out the resistance of the thermistor, then it uses the B formula (also called the beta equation):

1/T = 1/T0 + (1/B) × ln(R/R0)

  • T is the temperature in kelvin. Subtract 273.15 to get °C.
  • T0 is 298.15K, which is 25°C.
  • R is the resistance of the thermistor right now, and R0 is its resistance at 25°C (10000Ω).
  • B is 3950, the value given for this thermistor.

Python makes the three-coefficient Steinhart-Hart equation easy too, but it only pays off over very wide ranges; the probe's B value of 3950 covers -20°C to 105°C well. In a room at 20°C to 30°C, the script is normally within about a degree. The two things that move it are the tolerance of the 10K resistor and the real voltage of the Pi's 3.3V rail.

Here are a few points of the curve for this circuit, read by the ADS1115 with GAIN = 1:

Temperature Thermistor resistance Voltage on A0 ADS1115 raw value
0°C 33.6kΩ 0.757V 6052
15°C 15.8kΩ 1.277V 10218
25°C 10.0kΩ 1.650V 13200
40°C 5.3kΩ 2.157V 17253
70°C 1.76kΩ 2.806V 22449

Wiring Diagram

The wiring diagram between Raspberry Pi and  for the NTC thermistor probe, the 10K resistor and the ADS1115

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

First, connect the ADS1115 module to the I2C pins of the Raspberry Pi:

  • ADS1115 VDD pin to Raspberry Pi 3.3V (pin 1)
  • ADS1115 GND pin to Raspberry Pi GND (pin 39)
  • ADS1115 SCL pin to Raspberry Pi GPIO 3 (pin 5)
  • ADS1115 SDA pin to Raspberry Pi GPIO 2 (pin 3)

Then connect the thermistor probe and the 10K resistor to the ADS1115 module:

  • Thermistor wire 1 (either one) to the ADS1115 VDD pin, so it shares the 3.3V that comes from pin 1
  • Thermistor wire 2 to ADS1115 A0
  • 10K resistor leg 1 to ADS1115 A0
  • 10K resistor leg 2 to ADS1115 GND

Three points of this circuit hold two wires each: VDD, A0 and GND of the ADS1115. A small breadboard makes these joints easy. Without one, twist the resistor legs and the wire ends together firmly, or solder them and cover them with heat-shrink tube.

Use the 3.3V pin here, not a 5V pin. Most ADS1115 modules pull their SDA and SCL lines up to VDD, so 5V on VDD would pull GPIO 2 and GPIO 3 toward 5V, and the GPIO pins of the Pi are made for 3.3V only. Also, an input of the ADS1115 must never go above the module's own supply. A hot probe pushes A0 close to the voltage that feeds the thermistor, so the thermistor must get the same 3.3V as the ADS1115.

Leave the ADDR pin unconnected (most modules pull it to GND on the board) or tie it to GND, so the I2C address stays 0x48, the default of the library. A1, A2 and A3 stay free for more probes from the kit, each with its own 10K resistor.

How To Program for Raspberry Pi to Read Temperature from the NTC Thermistor

The script asks the ADS1115 for 10 readings of A0, takes the average, and then does the math in three small steps:

voltage = raw / 32767.0 * 4.096 r_ntc = 10000 * (3.3 - voltage) / voltage kelvin = 1.0 / (1.0 / 298.15 + math.log(r_ntc / 10000) / 3950)
  • The first line turns the raw value into volts. gain=1 sets the input range of the ADS1115 to ±4.096V, so 32767 means 4.096V. The UV sensor tutorial uses gain=2 (±2.048V), but a hot probe pushes A0 up to about 3.1V, and gain=2 would cut off every temperature above about 36°C.
  • The second line is the voltage divider rule turned around. It gives the resistance of the thermistor in ohms.
  • The third line is the B formula. In Python, math.log() is the natural log (ln). The script then subtracts 273.15 for °C and converts °C to °F.
  • The ADS1115 measures against its own built-in reference, not against the 3.3V pin, so the script needs the divider supply as a number (VCC = 3.3). The 3.3V pin of the Pi comes from a regulator and stays very steady. If your multimeter shows a slightly different value between pin 1 and pin 39, for example 3.28V, put that value into VCC.
  • Before any math, the script checks the voltage. Almost 0V means the probe is unplugged. Almost 3.3V means the probe wires touch or the 10K resistor is missing. Then it prints a hint instead of a strange temperature.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • The ADS1115 needs the I2C interface. Open the Raspberry Pi configuration tool:
sudo raspi-config
sudo reboot
  • After the reboot, open the SSH connection again.
  • Install the Adafruit_ADS1x15 library with this command:
sudo pip install Adafruit-ADS1x15
  • On Raspberry Pi OS Bookworm or newer, pip may refuse with an "externally-managed-environment" message. In that case, run sudo pip install --break-system-packages Adafruit-ADS1x15 instead, or install the library inside a Python virtual environment.
  • Wire the probe, the 10K resistor and the ADS1115 module as shown in the wiring diagram above.
  • Check that the Pi can see the ADS1115 module:
i2cdetect -y 1
  • The grid should show the number 48. If the command is not found, install it first with sudo apt-get install i2c-tools.
  • Create a Python script file ntc_thermistor.py (for example with nano ntc_thermistor.py) and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-ntc-thermistor-temperature-sensor """ import math import time import Adafruit_ADS1x15 # The Raspberry Pi has no analog input, so the ADS1115 module measures the # voltage in the middle of the thermistor + 10K resistor divider over I2C adc = Adafruit_ADS1x15.ADS1115() NTC_CHANNEL = 0 # the junction of the thermistor and the 10K resistor goes to ADS1115 input A0 # GAIN = 1 gives an input range of +/-4.096 V. On a hot probe the divider # output climbs to about 3.1 V, which does not fit the +/-2.048 V range of GAIN = 2 GAIN = 1 FULL_SCALE = 4.096 # volts that match a raw value of 32767 at GAIN = 1 VCC = 3.3 # the divider gets its power from the 3.3V pin of the Raspberry Pi R_FIXED = 10000.0 # the 10K resistor from the kit, between A0 and GND R0 = 10000.0 # resistance of the thermistor at 25 degrees Celsius B = 3950.0 # B value (beta) of the thermistor T0 = 298.15 # 25 degrees Celsius in kelvin SAMPLES = 10 # readings averaged for each result, a thermistor reading is a bit noisy def read_average_raw(): # Read channel A0 several times and return the average value total = 0 for _ in range(SAMPLES): total += adc.read_adc(NTC_CHANNEL, gain=GAIN) time.sleep(0.01) return total / SAMPLES try: print("Reading the NTC thermistor on ADS1115 A0. Press Ctrl + C to stop.") while True: raw = read_average_raw() # Turn the raw value into the voltage on A0 voltage = raw / 32767.0 * FULL_SCALE if voltage < 0.05: # Only the 10K resistor is left, and it pulls A0 down to 0V: # the probe is not plugged in, or its wire to VDD is loose print("Thermistor not found - check the probe plug and its wire to VDD") elif voltage > VCC - 0.05: # A0 is at the supply voltage: the two probe wires touch, # or the 10K resistor is not connected to GND print("Reading too high - check for a short or a missing 10K resistor") else: # Voltage divider: thermistor on the 3.3V side, 10K resistor on the GND side r_ntc = R_FIXED * (VCC - voltage) / voltage # B formula (beta equation): 1/T = 1/T0 + ln(R/R0) / B, T in kelvin kelvin = 1.0 / (1.0 / T0 + math.log(r_ntc / R0) / B) celsius = kelvin - 273.15 fahrenheit = celsius * 9.0 / 5.0 + 32.0 print("Raw: {:5.0f} | {:.3f} V | {:6.0f} ohm | {:5.1f}°C | {:5.1f}°F".format( raw, voltage, r_ntc, celsius, fahrenheit)) time.sleep(1) # one result per second except KeyboardInterrupt: print("\nExiting the program.")
  • Save the file (in nano: Ctrl + O, Enter, then Ctrl + X) and start the script:
python3 ntc_thermistor.py
  • Leave the probe in the air for a few seconds, then lower the steel tip into a glass of cold tap water. Only the steel tip is waterproof, so keep the white plug and the resistor joints dry.
  • Check the terminal:
PuTTY - Raspberry Pi
Reading the NTC thermistor on ADS1115 A0. Press Ctrl + C to stop. Raw: 12235 | 1.529 V | 11577 ohm | 21.7°C | 71.1°F Raw: 12211 | 1.526 V | 11619 ohm | 21.7°C | 71.0°F Raw: 12226 | 1.528 V | 11593 ohm | 21.7°C | 71.1°F Raw: 11529 | 1.441 V | 12898 ohm | 19.4°C | 66.9°F Raw: 10591 | 1.324 V | 14926 ohm | 16.3°C | 61.3°F Raw: 10042 | 1.255 V | 16289 ohm | 14.4°C | 57.9°F Raw: 9793 | 1.224 V | 16957 ohm | 13.6°C | 56.4°F Raw: 9689 | 1.211 V | 17247 ohm | 13.2°C | 55.8°F Raw: 9651 | 1.206 V | 17354 ohm | 13.1°C | 55.6°F Raw: 9660 | 1.208 V | 17328 ohm | 13.1°C | 55.6°F

The first three lines show the air of the room, about 21.7°C. Once the tip is in the water, the resistance goes up and the temperature goes down. The steel tube needs a few seconds to take on the temperature of the water, so the numbers fall step by step and then settle at about 13.1°C. A new line appears about every second (as set by the time.sleep(1) line) until you press Ctrl + C keys in the terminal.

Now pull the plug off the jumper wires while the script is running. The check in the script catches it:

PuTTY - Raspberry Pi
Thermistor not found - check the probe plug and its wire to VDD Thermistor not found - check the probe plug and its wire to VDD

If Python stops with "Remote I/O error" (OSError: [Errno 121]), the Pi cannot reach the ADS1115. Make sure I2C is turned on, and that SDA goes to GPIO 2 (pin 3) and SCL to GPIO 3 (pin 5). If the temperature is far off, for example 40°C in a cool room, check that the resistor is the 10K one from the kit.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Additional Knowledge

NTC Thermistor Probe vs DS18B20 Waterproof Probe

Both probes look almost the same: a steel tube on a cable. Let's compare how they work with a Raspberry Pi:

NTC Thermistor Probe DS18B20 Waterproof Probe
Type of signal analog, a resistance digital, 1-Wire
Extra parts on the Pi ADS1115 module and a 10K resistor one 4.7kΩ pull-up resistor
Range -20°C to 105°C -55°C to 125°C
Getting °C in Python your own math with the B formula the Linux 1-Wire driver does it
Several probes one ADS1115 input each (4 per module) many sensors on the same GPIO pin

Evidently, the DS18B20 is the easier choice if you only want a temperature, because the Pi reads it without an extra chip. The NTC thermistor is a good choice when an ADS1115 is already in your project, when you want the cheapest probe, or when you want to see every step from a voltage to a temperature.

Project Ideas with the NTC Thermistor

  • Water logger: add the date, the time and the temperature of a hydroponics tank to a CSV file every minute, then open it in a spreadsheet.
  • Always-on sensor: start the script with a systemd service or a cron @reboot line, so it keeps measuring after a power cut.
  • Top and bottom: put a second probe on A1 to compare the top and the bottom of a water tank or a compost bin.
  • Small web page: show the latest temperature on a Flask page served by the Pi, so you can check it from your phone.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!