Hi Everyone, I have used this code before to get the temperature from the thermistor sensor connected to ESP32 (wifi included). However, after I modified something I can't get the temperature anymore and the output shows " nan degree celsius". Can someone help to fix it or if u have another code to convert the thermistor reding to temperature in degree celsius.
#define THERMISTORPIN 34
const double VCC = 3.3; // NodeMCU on board 3.3v vcc
const double R2 = 10000; // 10k ohm series resistor
const double adc_resolution = 1023; // 10-bit adc
const double A = 0.001129148; // thermistor equation parameters
const double B = 0.000234125;
const double C = 0.0000000876741;
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
}
void loop() {
double Vout, Rth, temperature, adc_value;
adc_value = analogRead(THERMISTORPIN);
Vout = (adc_value * VCC) / adc_resolution;
Rth = (VCC * R2 / Vout) - R2;
/* Steinhart-Hart Thermistor Equation:
* Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)
temperature = (1 / (A + (B * log(Rth)) + (C * pow((log(Rth)),3)))); // Temperature in kelvin
temperature = temperature - 273.15; // Temperature in degree celsius
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" degree celsius");
delay(500);
}
Why are you using doubles on a ESP32. Doubles on a ESP32 are 64 bit numbers.
That is incorrect. The default A:D setting for the ESP32 is for 12 bits not 10. const int adc_resolution = 4096; or const float adc_resolution = 4096.0;
nan = not a number. The IDE is trying to tell you something - possibly about all those doubles. There must be something simpler than all this stuff you are using. If you need to get that complicated, you should consider using a DS18B20. It's easier.
Thanks for your reply, I have did your suggestion and get better results but the issue now is that the sensor shows opposite result.. ex, when I put it on hot surface the temprature decrease.. What do u think?
I think you are using a formula for a voltage divider with the NTC being pulled down to GND by R2, while your undisclosed circuit has the NTC being pulled up toward VCC by R2.
Have you solved it?
As mentioned before, esp32 has an ADC resolution of 12bit so your
const double adc_resolution = 1023;
needs to be changed as mentioned earlier.
Or if you want to use 10bit resolution (1023) add the following, which changes the resolution to what you are trying to work with.
analogReadResolution(10);
Also, you have probably copied those constants from somewhere, it needs to be calculated based on the thermal resistance table for the thermistor you are using, it should be in the datasheet.
const double A = 0.001129148; // thermistor equation parameters
const double B = 0.000234125;
const double C = 0.0000000876741;
Also yes, your original problem NaN in temperature value does not seem to be there if your code is exactly like above, NaN means not a number, it happens when doing arithmetic operations between variables and one or more of them are not of type number, example string, undefined, etc.
The NaNs can come from having the resolution wrong. if you get the 0-4095 ADC readings and they happen to be above 1024, and then this code would produce negative values of Rth:
Negative Rth would choke the log() and produce the NaNs.
Since @Metooo22 says they made the resolution change in #6, I poked the code into Wokwi and made the change @dlloyd suggested in #7. This swaps the sense of the voltage divider from R2 pulling up Rth to Rth pulling up R2, which is what that voltage divider formula is designed for.
// https://wokwi.com/projects/359721102307523585
// for https://forum.arduino.cc/t/thermistor-output-connected-to-nodemcu-esp32/1074739
#define THERMISTORPIN 34
const double VCC = 3.3; // NodeMCU on board 3.3v vcc
const double R2 = 10000; // 10k ohm series resistor
const double adc_resolution = 4096; // 12-bit adc
const double A = 0.001129148; // thermistor equation parameters
const double B = 0.000234125;
const double C = 0.0000000876741;
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
}
void loop() {
double Vout, Rth, temperature, adc_value;
adc_value = adc_resolution-analogRead(THERMISTORPIN)+0.5; // switch direction
Vout = (adc_value * VCC) / adc_resolution;
Rth = (VCC * R2 / Vout) - R2; // Formula for R2 as Pull-down: Vcc-Rth-R2-GND
/* Steinhart-Hart Thermistor Equation:
* Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)
*/
temperature = (1 / (A + (B * log(Rth)) + (C * pow((log(Rth)),3)))); // Temperature in kelvin
temperature = temperature - 273.15; // Temperature in degree celsius
Serial.print("Rth:");
Serial.print(Rth);
Serial.print(" Temperature = ");
Serial.print(temperature);
Serial.println(" degree celsius");
delay(500);
}
By clicking on the NTC you can get a temperature slider and see that the code isn't quite calibrated with the device at the limits.
I added a Serial.print(Rth); which made the negative Rth easy to see, and I switched to an (analogRead(..)+0.5)/4096; form to protect against a division by zero and to follow the logic in https://www.gammon.com.au/forum/?id=12779&reply=1#reply1
Since it prints out the resistance measurement, you could calibrate the Steinhart-Hart coefficients against another thermometer or standard to improve the fit by using SRS Thermistor Calculator or this sketch
For the Wokwi NTC and the ESP32 system, I get:
//Measured Data:
// data 0 T:-24.00 R:134479.71
// data 1 T:25.10 R:9946.43
// data 2 T:80.00 R:1269.78
// Fitted Coefficients
// float c1 = 1.0260460e-03; //coefficient A
// float c2 = 2.5245882e-04; //coefficient B
// float c3 = 3.8081867e-09; //coefficient C
An ESP has the wrong type of A/D to measure thermistors.
That makes all the above posts bad advice.
An ESP should only be used with digital temp sensors.
Leo..