• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

analogRead() for NTC thermistor with ESP32

Q&A Forum › Category: Other › analogRead() for NTC thermistor with ESP32
0 Vote Up Vote Down
Xabi Alaez asked 7 years ago

Hello everyone,

I want to use NTC thermistors to read surface temperature of metals, but I have 2 questions:

  1. Are these NTC thermistors appropiate for measuring surface temperature? They are used for measuring 3D printers extruder temperature.
  2. Will the analogRead() function of the ESP32 let me read the temperature properly? I have read that the ADC pins are not linear, so I’m worried the values won’t be correct.

I will appreciate any advice to help me measure surface temperatures with the ESP32 (or other boards).

Thanks in advance!

Xabi

Question Tags: #ESP32 #NTC
7 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

Hi Xabi,

  1. Yes, using an NTC thermistor like that is probably the best way of measuring surface temperature for most applications
  2. As you also said, unfortunately the analogRead() is non-linear (read the “ADC Non-linear” section): https://rntlab.com/esp32-reading-analog-inputs/

Here’s the graph, so the ESP32 is not accurate and it will produce wrong readings:

If it never reaches “higher voltages” (around 3.1V), it will probably give you results that are good enough for most applications.

Regards,
Rui

0 Vote Up Vote Down
Xabi Alaez answered 7 years ago

Thank you very much for your answer Rui! 
I’ll try it 🙂

0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

You’re welcome!

0 Vote Up Vote Down
Xabi Alaez answered 7 years ago

Hi Rui and everyone,
I’m trying to read the NTC thermistor from aliexpress but I’m not getting any values (I’m getting super high values that don’t change). I’m using an example I found on the internet:

 

//Pins
#define THERMISTORPIN 25

// Series resistor value
#define SERIESRESISTOR 100000​

// Number of samples to average
#define SAMPLERATE 5

// Nominal resistance at 25C
#define THERMISTORNOMINAL 100000

// Nominal temperature in degrees
#define TEMPERATURENOMINAL 25

// Beta coefficient
#define BCOEFFICIENT 3950

void setup()
{
 Serial.begin(115200);
}

int getTemp() {
 
 double thermalSamples[SAMPLERATE];
 double average, kelvin, resistance, celsius;
 int i;
 
 // Collect SAMPLERATE (default 5) samples
 for (i=0; i<SAMPLERATE; i++) {
 thermalSamples[i] = analogRead(THERMISTORPIN);
 delay(10);
 }
 
 // Calculate the average value of the samples
 average = 0;
 
 for (i=0; i<SAMPLERATE; i++) {
 average += thermalSamples[i];
 }
 
 average /= SAMPLERATE;

// Convert to resistanceresistance = 4095 / average - 1;resistance = SERIESRESISTOR/resistance;
 /*
 * Use Steinhart equation (simplified B parameter equation) to convert resistance to kelvin
 * B param eq: T = 1/( 1/To + 1/B * ln(R/Ro) )
 * T = Temperature in Kelvin
 * R = Resistance measured
 * Ro = Resistance at nominal temperature
 * B = Coefficent of the thermistor
 * To = Nominal temperature in kelvin
 */
 
 kelvin = resistance/THERMISTORNOMINAL; // R/Ro
 kelvin = log(kelvin); // ln(R/Ro)
 kelvin = (1/BCOEFFICIENT) * kelvin; // 1/B * ln(R/Ro)
 kelvin = (1/(TEMPERATURENOMINAL+273.15)) + kelvin; // 1/To + 1/B * ln(R/Ro)
 kelvin = 1/kelvin; // 1/( 1/To + 1/B * ln(R/Ro) )​
 
 // Convert Kelvin to Celsius
 celsius = kelvin - 273.15;

// Send the value back to be displayed
 return celsius;
}

void loop() {
 int temp;
 
 // Call the function to get the temperature in degrees celsius
 temp = getTemp();
 
 // Output the temp to serial
 Serial.print("Temp: ");
 Serial.print(temp);
 Serial.println(" C");
}

 

And I’m wiring it with a 100k ohm resistor like this:

In my case, the blue wire goes to the D25 pin of the ESP32 DOIT Board.
What am I doing wrong? Could you please help me with a simple example or anything? I would appreciate it so much.
Thanks in advance!
Xabi

0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

Hi Xabi,
I don’t have any example on that exact subject at the moment, but what’s the values that are being printed in the serial monitor?
I recommend that you take a look and use this example to see, if it works with your circuit: https://learn.adafruit.com/thermistor/using-a-thermistor

0 Vote Up Vote Down
Xabi Alaez answered 7 years ago

Thanks Rui!
I made my thermistor work properly with the example you told me and this great video of G6EJD explaining the ADC in ESP32. I think you should take a look a it since it can be nice information to add to the ADC pins Unit of the course 🙂

0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

Thank you, I’ll definitely try to add a few more details about ADC in the future. I’m glad you made it work.
Regards,
Rui

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.