I´m building a weather staion with an Esp32 and a thermal resister. I want to display the value in Celsius. For the calculation i need the logarithem but when i use it, it always display "nan".
int ThermistorPin = 27;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
void setup() {
Serial.begin(115200);
}
void loop() {
Vo = analogRead(A0);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
Serial.println(logR2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
Tc = T - 273.15;
Serial.print("Temperature: ");
Serial.print(Tc);
Serial.println(" C");
delay(500);
}
I am not a mathematician, but the problem lies with the number 1023. That's the 10 bit resolution used by most Arduino's (Uno, Mega). The ESP32 has a resolution of 12 bits, which gives the number 4095. So, replace 1023 by 4095, because the ESP32 will generates numbers above 1023, up to 4095.
R2 = R1 * (4095/ (float)Vo - 1.0);
A small tip: c3*logR2*logR2*logR2
can be written as c3*pow(logR2 ,3)