ESP32 - NTC Thermistor Temperature Sensor

This tutorial instructs you how to use ESP32 to read the temperature value from a 10K NTC thermistor (B value 3950), and print it to the Serial Monitor in Celsius and Fahrenheit. The thermistor hides in a small steel tube on a thin black cable, so it can go where an ESP32 board cannot: into water, into soil, or behind a fridge door. There is no chip inside it, and you do not install any library. The ESP32 measures a voltage on GPIO36, finds the probe's resistance from it, and changes that number into degrees.

ESP32 DevKit reading a 10K NTC thermistor steel probe on GPIO36 as Celsius and Fahrenheit

This tutorial shows how to program the ESP32 using the Arduino language (C/C++) via the Arduino IDE. If you’d like to learn how to program the ESP32 with MicroPython, visit this ESP32 MicroPython - NTC Thermistor Temperature Sensor tutorial.

Hardware Used In This Tutorial

1×38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
1×Alternatively, 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×10K NTC Thermistor Probe Kit, Waterproof, with 10K Resistor
1×Optionally, Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables .

There is no separate resistor to order: every probe in the DIYables five-pack comes with a matching 10K resistor.

Introduction to the NTC Thermistor Temperature Sensor

NTC thermistor probe pinout: steel tip, two-wire cable and 2-pin JST XH2.54 plug
image source: diyables.io

Inside the round end of a stainless-steel tube sits one small part: a thermistor. It is a kind of resistor, but its value moves a lot with heat. The ESP32 measures this value and converts it to degrees. The probe is an NTC type (the letters mean Negative Temperature Coefficient). Simply put: more heat, fewer ohms. The tube is sealed, so water cannot get in. Its black two-wire cable is 0.5m long and ends in a white plug. There is no module board here: the pins of two male jumper wires slide straight into the plug.

10K NTC Thermistor Probe
The resistance at 25°C 10kΩ, ±1%
The B value 3950
The temperature range -20°C to 105°C
The tip 5mm x 25mm stainless steel, round, waterproof
The cable 0.5m long, two black wires
The plug white, 2-hole JST XH2.54 (2.54mm pitch)
The dissipation constant about 5mW/°C
The pack 5 sets (probe + 10K resistor)

The dissipation constant tells how much the probe warms itself with the current that flows through it. In this circuit, the probe never gets more than about 0.3mW, so it warms itself by less than 0.1°C. You will never see it in the readings.

NTC Thermistor Probe Pinout

Nothing is printed on the two wires or on the plug, because both sides are the same:

  • Pin 1 of the plug: connect this pin to 3V3 on the ESP32.
  • Pin 2 of the plug: connect this pin to GPIO36 on the ESP32. The 10K resistor also joins here.
  • The resistor's other leg: connect this pin to GND (0V).

A thermistor has no polarity, just like any other resistor. If you swap pin 1 and pin 2, the reading stays the same.

From resistance to temperature

The ESP32 cannot measure ohms. It can only measure volts. So the code needs a helper: the 10K resistor. The probe sits on top, between 3V3 and GPIO36, and the resistor sits below, between GPIO36 and GND. The two parts share the 3.3V like two people share a cake: the part with more ohms takes the bigger piece. GPIO36 measures the piece of the resistor. As the probe heats up and loses ohms, the piece of the resistor grows, and the voltage on GPIO36 rises. This kind of circuit is called a voltage divider. Some points of the curve:

Probe Temperature Probe Resistance Voltage on GPIO36
5°C, inside a fridge 25.9kΩ 0.92V
20°C, a cool room 12.5kΩ 1.46V
33°C, inside a closed hand 7.1kΩ 1.93V
60°C, hot tap water 2.5kΩ 2.64V
100°C, boiling water 0.7kΩ 3.09V

The code works backwards in two steps. First, it finds the probe resistance from the millivolts on GPIO36:

R = 10000 × (3300 − mV) / mV

Then the B (beta) formula turns the resistance into a temperature:

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

Here T0 = 298.15K, which is 25°C; R0 = 10000Ω; B = 3950. The answer is in kelvin: subtract 273.15 and you have °C. Other thermistor projects often show the Steinhart-Hart equation. It needs three constants, not one, and it is slightly more precise. For this probe, the one-constant B formula is fine. Between 20°C and 30°C, expect the reading to be typically within about 1°C. Most of the small error that is left comes from the ESP32 ADC and from the exact value of the 10K resistor.

Good places for this probe: an aquarium, a fermenting bucket, an egg incubator, a greenhouse bed, or the side of a battery pack or a hot water pipe. Prefer a probe that hands over a finished number in °C? The waterproof DS18B20 is digital and needs no formula: see ESP32 - Temperature Sensor. Measuring an oven or a pizza stone, far above 105°C? Pick a thermocouple: see ESP32 - MAX6675 Thermocouple Module.

Wiring Diagram

Only three pins of the ESP32 are used: 3V3, GPIO36 and GND. The 10K resistor joins the GPIO36 wire to the GND wire.

ESP32 and NTC thermistor wiring diagram with the 10K resistor

This image is created using Fritzing. Click to enlarge image

NTC Thermistor Probe ESP32
One probe wire (any of the two) → 3V3
Other probe wire → GPIO36 (VP)
10K resistor, leg A → GPIO36 (VP)
10K resistor, leg B → GND

In the diagram, the resistor legs touch the wires directly. On a real desk, use a small breadboard for this joint: push the resistor into it, then plug the GPIO36 wire and the GND wire into the rows of its two legs. Without a breadboard, solder each resistor leg to the stripped tip of the matching wire, and cover the joint with tape or heat-shrink tube.

Why GPIO36? It is an ADC1 pin. The ESP32 cannot use its ADC2 pins while WiFi is on, so an ADC1 pin keeps your thermometer working when you later share the temperature over WiFi. GPIO36 can only be an input, and that is fine here, because the code only reads it. On many boards, it is printed as VP or SVP.

WARNING

Power the probe from 3V3 only, never from 5V (VIN). A warm probe has few ohms, so GPIO36 rises toward the supply voltage. On 5V, the pin would see more than 3.3V once the probe goes above about 41°C, and that can damage the ESP32. The math in the code also counts on a 3300 mV supply.

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

How To Program For NTC Thermistor

No library is needed. The ESP32 core for Arduino IDE already has the functions we use.

  • Let GPIO36 measure the whole voltage range of the divider, up to about 3.1V. This line goes in setup():
analogSetAttenuation(ADC_11db);
  • Read the pin many times in millivolts and average the results, because one thermistor reading alone can jump a little:
uint32_t sum = 0; for (int i = 0; i < 20; i++) { sum += analogReadMilliVolts(36); delay(3); } float pinMv = sum / 20.0;
  • Find the resistance of the probe in ohms:
float ntcOhms = 10000.0 * (3300.0 - pinMv) / pinMv;
  • Apply the B formula. It gives kelvin, and two more lines change it to °C and °F. Note: log() in Arduino code means ln (base e), not log10:
float kelvin = 1.0 / (1.0 / 298.15 + log(ntcOhms / 10000.0) / 3950.0); float celsius = kelvin - 273.15; float fahrenheit = celsius * 9.0 / 5.0 + 32.0;

Why analogReadMilliVolts() and not analogRead()? No two ESP32 chips have exactly the same ADC: two boards can give two different raw numbers for the same voltage. Espressif tests every chip in the factory and writes its own correction values into the chip's eFuse memory. analogReadMilliVolts() uses these values and gives you the voltage directly. The raw scale has a second problem: its steps are not equal in size at the bottom and at the top of the scale, and the last number, 4095, comes at around 3.1V, not at 3.3V. So dividing the raw number by 4095 can shift the temperature by a few degrees.

The code takes the 3V3 pin as exactly 3300 mV. Most boards are within a few percent of that. If you have a multimeter, measure between 3V3 and GND, and put that number, for example 3285, in the SUPPLY_MV line. Your readings will be a little more exact.

ESP32 Code - NTC Thermistor Temperature Sensor

About once a second, the code below prints one line with four values: °C, °F, the probe's ohms and the millivolts on GPIO36. Two checks come before the math. If GPIO36 is under 150 mV, the probe would be colder than -30°C, which is outside its range, so the code says that the probe is unplugged. If the resistance is under 600Ω, the probe would be hotter than 105°C, so the code says that the wires touch. In both cases, you get a hint instead of a wrong temperature.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ntc-thermistor-temperature-sensor */ // ESP32 reads a waterproof 10K NTC thermistor probe (B value 3950) and prints the temperature in Celsius and Fahrenheit // Circuit: 3V3 -> thermistor probe -> GPIO36 (VP, ADC1) -> 10K resistor -> GND #define PROBE_PIN 36 // ESP32's GPIO36 (ADC1) connected to the probe and to the 10K resistor #define READINGS 20 // number of readings averaged for each result #define SUPPLY_MV 3300.0 // voltage of the 3V3 pin in millivolts (type your multimeter value here) #define FIXED_RESISTOR 10000.0 // the 10K resistor from the kit, between GPIO36 and GND #define THERMISTOR_R25 10000.0 // resistance of the thermistor at 25°C (R0) #define T25_KELVIN 298.15 // 25°C written in kelvin (T0) #define B_COEFFICIENT 3950.0 // B (beta) value of the thermistor #define NO_PROBE_MV 150.0 // under 150 mV the probe would be colder than -30°C: it must be unplugged #define SHORT_OHMS 600.0 // under 600 ohms the probe would be hotter than 105°C: the wires must be shorted // read GPIO36 many times and return the average voltage in millivolts float readProbeMilliVolts() { uint32_t sum = 0; for (int i = 0; i < READINGS; i++) { sum += analogReadMilliVolts(PROBE_PIN); // calibrated reading, uses the correction values stored in the chip delay(3); } return sum / (float)READINGS; } // B (beta) formula: 1/T = 1/T0 + ln(R/R0) / B, with T in kelvin float ohmsToCelsius(float ohms) { float kelvin = 1.0 / (1.0 / T25_KELVIN + log(ohms / THERMISTOR_R25) / B_COEFFICIENT); // log() is the natural log return kelvin - 273.15; } void setup() { Serial.begin(9600); analogSetAttenuation(ADC_11db); // let GPIO36 measure from 0V up to about 3.1V } void loop() { float pinMv = readProbeMilliVolts(); if (pinMv < NO_PROBE_MV) { // nothing connects GPIO36 to 3V3, so the 10K resistor pulls it down to 0V Serial.println("GPIO36 is near 0 V: plug in the probe and check the wire on 3V3"); } else { // voltage divider: pinMv = SUPPLY_MV * FIXED_RESISTOR / (FIXED_RESISTOR + ntcOhms), solved for ntcOhms float ntcOhms = 0; if (pinMv < SUPPLY_MV) ntcOhms = FIXED_RESISTOR * (SUPPLY_MV - pinMv) / pinMv; if (ntcOhms < SHORT_OHMS) { // GPIO36 is at (or very near) the top of the divider Serial.println("GPIO36 is near 3.3 V: the probe wires touch, or the 10K resistor is not on GND"); } else { float celsius = ohmsToCelsius(ntcOhms); float fahrenheit = celsius * 9.0 / 5.0 + 32.0; Serial.print("Celsius: "); Serial.print(celsius, 1); // one decimal Serial.print(" | Fahrenheit: "); Serial.print(fahrenheit, 1); Serial.print(" | NTC: "); Serial.print(ntcOhms, 0); // whole ohms Serial.print(" Ohm | GPIO36: "); Serial.print(pinMv, 0); // whole millivolts Serial.println(" mV"); } } delay(1000); // about one line per second }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as in the diagram above: the probe between 3V3 and GPIO36, the 10K resistor between GPIO36 and GND.
  • Connect the ESP32 to your PC with the USB cable.
  • Open Arduino IDE. Pick the ESP32 board (e.g. ESP32 Dev Module) and the COM port of your board.
  • Copy the code and paste it into a new sketch in Arduino IDE.
  • Click to the Upload button on Arduino IDE to compile the code and upload it to the ESP32.
How to upload ESP32 code on Arduino IDE
  • Open the Serial Monitor, with the baud rate at 9600.
How to open serial monitor on Arduino IDE
  • Keep the probe still on the table while the first three lines appear. Then hold the steel tip tightly in your closed hand. After about ten seconds, open your hand again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Celsius: 20.3 | Fahrenheit: 68.6 | NTC: 12358 Ohm | GPIO36: 1476 mV Celsius: 20.4 | Fahrenheit: 68.7 | NTC: 12318 Ohm | GPIO36: 1479 mV Celsius: 20.3 | Fahrenheit: 68.5 | NTC: 12380 Ohm | GPIO36: 1474 mV Celsius: 23.1 | Fahrenheit: 73.7 | NTC: 10868 Ohm | GPIO36: 1581 mV Celsius: 26.0 | Fahrenheit: 78.8 | NTC: 9558 Ohm | GPIO36: 1687 mV Celsius: 28.2 | Fahrenheit: 82.8 | NTC: 8677 Ohm | GPIO36: 1767 mV Celsius: 29.9 | Fahrenheit: 85.8 | NTC: 8068 Ohm | GPIO36: 1826 mV Celsius: 31.1 | Fahrenheit: 87.9 | NTC: 7680 Ohm | GPIO36: 1866 mV Celsius: 31.7 | Fahrenheit: 89.1 | NTC: 7461 Ohm | GPIO36: 1890 mV Celsius: 32.2 | Fahrenheit: 89.9 | NTC: 7326 Ohm | GPIO36: 1905 mV Celsius: 32.3 | Fahrenheit: 90.2 | NTC: 7277 Ohm | GPIO36: 1910 mV Celsius: 31.8 | Fahrenheit: 89.3 | NTC: 7435 Ohm | GPIO36: 1893 mV
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Lines 1 to 3 show a cool room at about 20°C. Then the hand closes around the steel tip: the millivolts rise, the ohms drop, and the temperature climbs for several seconds, since the heat needs time to pass through the steel. Why does it stop near 32°C and not at 37°C? 37°C is the temperature deep inside the body, but the skin of the hand is cooler, usually between 30°C and 34°C. The last line comes just after the hand opens: in the air, the tip cools down slowly. So after each change, wait until the numbers stop moving.

※ NOTE THAT:

  • The steel tip is the only waterproof part. Hang the plug and the resistor joint over the edge of the tank or glass, out of the water.
  • A thermistor does not mind a long cable, so you can add extra wire to reach a tank or a freezer. Run long wires away from motors and mains cables, because they can disturb the reading.

What next? Switch a fan with a relay when a home-brew bucket gets warmer than 24°C, or sound a piezo buzzer when an egg incubator falls below 37°C.

Line-by-line Code Explanation

The ESP32 code above has comments on its important lines. Please read the comments in the code!

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Language References

※ OUR MESSAGES