Arduino Mega - NTC Thermistor Temperature Sensor

How hot is a fresh cup of tea, and how fast does it cool down? A 10K NTC thermistor probe can tell you, and the Arduino Mega needs only one analog pin to read it. The probe is a tiny resistor sealed in a steel cap. It has no chip inside, and it needs no library. This tutorial shows the Arduino Mega turning the resistance of the probe into a temperature you can read. Here is what we will go through in detail:

Arduino Mega 2560 turns a waterproof 10K NTC thermistor probe into a °C and °F thermometer

We suggest:

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×10K NTC Thermistor Waterproof Probe (kit with 10K resistors)
1×Jumper Wires
1×Optionally, Breadboard
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

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 and Its Pinout

Pinout of the DIYables 10K NTC 3950 probe: sealed steel cap, half-meter lead, white JST XH2.54 connector
image source: diyables.io

Think of this sensor as a thermometer without a display. At one end there is a sealed stainless-steel cap, 25mm long and 5mm across, with a rounded tip. The 10K NTC thermistor is hidden inside that cap. Half a meter of thin black cable leads away from it and ends in a little white two-hole connector, a JST XH2.54. That is the whole sensor: no circuit board, no chip and nothing to solder. The DIYables product is a set of five probes, and a matching 10K resistor is packed for every probe, so there is nothing extra to order.

DIYables 10K NTC Thermistor Probe
Thermistor NTC, 10kΩ at 25°C (±1%)
Beta (B) value 3950
Temperature range -20°C to +105°C
Probe waterproof stainless steel, 5mm x 25mm, round tip
Lead 0.5m black cable with two wires
Plug white JST XH2.54, 2 pins
Wire polarity none
Insulation resistance more than 100MΩ
Output a resistance, turned into a voltage by the 10K resistor

Both wires in the cable are black and identical. Each of the two holes in the connector accepts the pin of a male jumper wire. Then:

  • Probe wire #1 (any of the two): must be connected to 5V on the power header of the Mega
  • Probe wire #2: must be connected to A0, one of the sixteen analog inputs of the Arduino Mega
  • Kit resistor (10K): must be connected between A0 and one of the Mega's GND pins (0V). At A0 it meets probe wire #2

※ NOTE THAT:

The holes of the JST connector are small. Push each jumper pin all the way in, then pull it gently to check that it holds. A pin that is only half inside is the most common reason for a jumpy or missing reading. Which wire goes where does not matter, because a thermistor has no + or - side.

Reading a Resistance with the Mega

Put simply, a thermistor is a resistor built to react to heat. In an NTC type, the value goes down when the tip gets warmer. The Arduino Mega cannot read ohms directly, because its analog pins only see volts. That is where the 10K resistor comes in. The probe and the resistor sit in a row between 5V and GND and share the 5V between them. A0 is the point in the middle. When the tip warms up, the probe loses resistance, A0 gets a bigger part of the 5V, and analogRead() returns a bigger number.

Some places where you might use the probe, and what the Mega sees there:

Place Temperature Probe resistance Voltage on A0 analogRead()
Home freezer -18°C about 93.3kΩ 0.48V about 99
Fridge shelf 4°C about 27.3kΩ 1.34V about 274
Living room 20°C about 12.5kΩ 2.22V about 454
Hot bath water 45°C about 4.3kΩ 3.48V about 713
Fresh cup of tea 85°C about 1.1kΩ 4.51V about 923

The sketch walks this path backwards. From the analogRead() value it finds the resistance R of the probe. The beta (B-parameter) equation then returns the temperature T in kelvin:

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

In this equation, B is 3950, R0 is 10000Ω, and T0 is 298.15K, which equals 25°C. The ln part is the natural logarithm. Take T minus 273.15, and the result is in °C. A more exact formula with three coefficients exists, called the Steinhart-Hart equation, but for a 1% hobby probe like this one the simpler B formula does the job. Near 20 to 25°C, a reading is normally within about 1°C of the truth. Any extra error comes from the tolerance of the 10K resistor and from the Mega's ADC.

Wiring Diagram

Only three Mega pins are used: 5V, A0 and GND. The A0 point is shared by probe wire #2 and one leg of the resistor.

The wiring diagram between Arduino Mega 2560  for the 10K NTC thermistor probe

This image is created using Fritzing. Click to enlarge image

Part of the Circuit Arduino Mega Pin
Either probe wire 5V
The other probe wire A0
10K resistor, leg A A0
10K resistor, leg B GND

The drawing shows the resistor fixed right onto the wires that run to A0 and GND. In practice, a few holes of a small breadboard make that joint easy. For a permanent build, solder the resistor straight onto the two wire ends and seal the joint with heat-shrink tubing.

Why 5V? By default, the Mega's ADC uses its own 5V as the ruler for every reading. When the probe hangs from the same 5V, the result depends only on the two resistances, not on the exact supply voltage. If your USB port gives 4.85V, the ruler and the divider both shrink together, and the temperature stays the same.

How To Program For NTC Thermistor

This sketch uses no library at all. The natural-log function, log(), comes with the Arduino core.

  • The first step is to label the analog pin where the probe and the resistor meet:
#define THERMISTOR_ADC_PIN A0
  • Store the numbers of the probe and the circuit as constants:
const float SERIES_OHMS = 10000.0; // the 10K resistor between A0 and GND const float NTC_OHMS_AT_25C = 10000.0; // probe resistance at 25°C const float NTC_BETA = 3950.0; // B value const float KELVIN_AT_25C = 298.15; // 25°C in kelvin const float ADC_FULL_SCALE = 1023.0; // 10-bit ADC of the Mega
  • Read A0 ten times and work out the average. One reading can jump by a step or two, and the average is calmer:
long sum = 0; for (int i = 0; i < 10; i++) { sum += analogRead(THERMISTOR_ADC_PIN); delay(10); } float adcAvg = sum / 10.0;
  • Change the average into the resistance of the probe. Because the probe and the ADC use the same 5V, the ratio is all you need, not the voltage:
float ntcOhms = SERIES_OHMS * (ADC_FULL_SCALE / adcAvg - 1.0);
  • Feed the resistance into the beta equation (log() here means ln), then convert kelvin to °C and °F:
float kelvin = 1.0 / (1.0 / KELVIN_AT_25C + log(ntcOhms / NTC_OHMS_AT_25C) / NTC_BETA); float celsius = kelvin - 273.15; float fahrenheit = celsius * 9.0 / 5.0 + 32.0;

Arduino Mega Code for NTC Thermistor Temperature Sensor

Below is the complete sketch. Roughly once a second, it shows the probe resistance, the averaged ADC count, and the temperature in both units. Two sanity checks run first. An average close to 0 tells the Mega that the probe is unplugged: with nothing on the 5V side, the 10K resistor drags A0 to 0V. An average close to 1023 means A0 is stuck at 5V, which happens when the probe wires touch each other or the resistor is missing. Either way, you get a plain hint on the Serial Monitor, not a meaningless inf or nan.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-ntc-thermistor-temperature-sensor */ #define THERMISTOR_ADC_PIN A0 // Arduino Mega analog input where the probe meets the 10K resistor const float SERIES_OHMS = 10000.0; // the 10K resistor from the kit, between A0 and GND const float NTC_OHMS_AT_25C = 10000.0; // probe resistance at 25°C const float NTC_BETA = 3950.0; // B (beta) value of the probe const float KELVIN_AT_25C = 298.15; // 25°C in kelvin const float ADC_FULL_SCALE = 1023.0; // the Mega ADC is 10-bit: 0 to 1023 const int READINGS = 10; // readings averaged for every line void setup() { Serial.begin(9600); // analogReference() stays at DEFAULT (5V): the probe is fed from the same 5V } // Read A0 several times and return the average, because one reading jumps a little float readAveragedAdc() { long sum = 0; for (int i = 0; i < READINGS; i++) { sum += analogRead(THERMISTOR_ADC_PIN); delay(10); } return sum / (float)READINGS; } void loop() { float adcAvg = readAveragedAdc(); // nearly 0: nothing pulls A0 up, so the 10K resistor holds it at GND -> probe unplugged if (adcAvg < 10.0) { Serial.println("No probe on A0 - push both jumper wires fully into the white plug"); delay(1000); return; } // nearly 1023: A0 is stuck at 5V -> the probe wires touch, or the 10K resistor is missing if (adcAvg > ADC_FULL_SCALE - 10.0) { Serial.println("A0 is stuck at 5V - is the 10K resistor from A0 to GND in place?"); delay(1000); return; } // probe on the 5V side, resistor on the GND side; only the ratio is needed, no volts float ntcOhms = SERIES_OHMS * (ADC_FULL_SCALE / adcAvg - 1.0); // B formula: 1/T = 1/T0 + ln(R/R0) / B, T in kelvin (log() is the natural log) float kelvin = 1.0 / (1.0 / KELVIN_AT_25C + log(ntcOhms / NTC_OHMS_AT_25C) / NTC_BETA); float celsius = kelvin - 273.15; float fahrenheit = celsius * 9.0 / 5.0 + 32.0; Serial.print("Thermistor: "); Serial.print(ntcOhms, 0); Serial.print(" ohm (ADC "); Serial.print(adcAvg, 1); Serial.print(") -> "); Serial.print(celsius, 1); Serial.print(" °C / "); Serial.print(fahrenheit, 1); Serial.println(" °F"); delay(1000); // a new line about once per second }

Detailed Instructions

  • New to the Arduino Mega? Follow how to set up the environment for Arduino Mega in the Arduino IDE before you start.
  • Make the three connections shown in the wiring diagram.
  • Plug the Arduino Mega into your computer with the USB A/B cable.
  • Start the Arduino IDE.
  • Choose Arduino Mega or Mega 2560 as the board, then pick its COM port.
  • Copy the sketch into a new IDE window and press Upload.
  • Set the Serial Monitor to 9600 baud and open it. For the first few lines, the probe just rests on the desk.
  • Now make a mug of tea and dip the steel tip into it. The white connector and the resistor must stay far from the liquid.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Thermistor: 12769 ohm (ADC 449.3) -> 19.6 °C / 67.3 °F Thermistor: 12708 ohm (ADC 450.5) -> 19.7 °C / 67.5 °F Thermistor: 12759 ohm (ADC 449.5) -> 19.6 °C / 67.3 °F Thermistor: 6347 ohm (ADC 625.8) -> 35.6 °C / 96.1 °F Thermistor: 3977 ohm (ADC 731.9) -> 47.3 °C / 117.1 °F Thermistor: 2886 ohm (ADC 793.9) -> 55.9 °C / 132.6 °F Thermistor: 2309 ohm (ADC 831.1) -> 62.1 °C / 143.8 °F Thermistor: 1968 ohm (ADC 854.8) -> 66.7 °C / 152.1 °F Thermistor: 1756 ohm (ADC 870.2) -> 70.1 °C / 158.1 °F Thermistor: 1617 ohm (ADC 880.6) -> 72.5 °C / 162.6 °F Thermistor: 1528 ohm (ADC 887.4) -> 74.3 °C / 165.7 °F Thermistor: 1463 ohm (ADC 892.4) -> 75.6 °C / 168.1 °F
Ln 11, Col 1
Arduino Mega Or... on COM15
2

The first three lines are a cool kitchen at about 19.6°C. Then the tip goes into the tea. The resistance drops from about 12.8kΩ to less than 1.5kΩ, and the temperature shoots up by more than 50°C in about ten seconds. After that, each line adds less and less, because the steel cap needs some time to warm through. In this test it settled at about 79°C some seconds later. Lift the probe out, and the numbers start to fall at once. A good habit: wait for three or four matching lines before you write down a value.

The message "No probe on A0" means a jumper pin came out of the white connector. The message "A0 is stuck at 5V" means you should look at the resistor: one leg must reach A0, the other GND. Do the numbers move the wrong way, colder while the tip sits in hot tea? Then the probe and the resistor have swapped places. The probe belongs between 5V and A0.

Once the readings look right, the Arduino Mega has plenty of free pins for the next step. It can beep a buzzer when a freezer climbs above -15°C, show the temperature on an LCD, switch a heater for a fish tank or a fermentation bucket with a relay, or log a fridge, a freezer and the kitchen at the same time, with one probe, one 10K resistor and one analog pin each.

Additional Knowledge

How Fine Is One Step of the Mega ADC?

The Arduino Mega turns 0 to 5V into 1024 steps. Because the thermistor curve bends, the value of one step changes with the temperature:

Temperature analogRead() change per 1°C About 1 ADC step equals
-18°C about 5.4 steps 0.18°C
0°C about 9.6 steps 0.10°C
25°C about 11.4 steps 0.09°C
80°C about 3.2 steps 0.31°C
100°C about 1.8 steps 0.57°C

It is evident that the probe and the Mega work best around room temperature, where one step is less than a tenth of a degree. Near the ends of the range the steps get bigger. In the tea test above, each step was already worth about 0.3°C, which is one more reason why the sketch averages ten readings.

Keep analogReference() at DEFAULT

The Mega can also measure against an internal 1.1V or 2.56V reference, with analogReference(INTERNAL1V1) or analogReference(INTERNAL2V56). That trick helps with small signals, but do not use it with this circuit. A0 is already at 2.5V at 25°C and goes higher on a warm day, so a 2.56V ruler would cut off every reading above about 26°C. The ratio formula also needs the divider and the ADC to share the same 5V. Leave the reference at its default, and the code never needs to know a voltage at all.

A last practical tip: only the steel cap is waterproof. In a fish tank or a brewing bucket, let the cable hang over the edge, so the connector and the resistor joint stay dry. And never put the probe into anything hotter than 105°C.

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!