[SOLVED]Calculating temperature from thermistor

Hi, I want to get the temperature from a thermistor, so I made a simple a simple voltage divider (5V to thermistor, 10k between thermistor and ground, analogRead() between the 10k resistance and the thermistor.
The nominal value of the thermistor is 10K too,
I looked the datasheet (http://www.vishay.com/docs/29049/ntcle100.pdf) and found a formula to get the temperature from the thermistor resistance:

K=3977
r ref=10k
A1=3.35401e-3
B1=2.56985e-4
C1=2.620131e-6
D1=6.383091e-8

T=(A1+B1*ln(r/r ref)+C1*ln^2(r/r ref)+D1*ln^3(R/r ref))^-1

and I found that using analogRead(), the resistance of the thermistor is

R=((1024-val)*10000)/val

Then I made a simple sketch to print the temperature on the serial monitor:

#include <avr/math.h>
//double calctemp(int value);
double temp;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly: 
  temp=calctemp(analogRead(0));
  Serial.println((double)temp);
  delay(1000);
}

double calctemp(int value)
{
  double temp;
 temp=(1.0/(0.00335401+0.000256985*(double)log((((double)((1024-value)*10000)/(double)value))/(double)10000)+0.000002620131*(double)pow(log((((double)((1024-value)*10000)/(double)value))/(double)10000),2.0)+0.00000006383091*(double)pow((double)log((((double)((1024-value)*10000)/(double)value))/(double)10000),3.0)));
  return temp;
  
}

But the problem is that I only get “nan” as output…
Has anyone a clue of what might be wrong?

I would start by splitting that calculation into smaller steps and write the intermediate results to Serial by the time you've done that you will PROB90 see the problem!

and I found that using analogRead(), the resistance of the thermistor is

That depends on how you have wired the thermistor to the analogue input.
How have you done it?

it’s like that:

@mmcp42 I'll try that, thanks :)

it's like that:

So then

R=((1024-val)*10000)/val

is not going to give you a resistance. You need to use ohms law of r = v/R

I re did the maths and found where the error was, now I get correct values for the temperature :slight_smile:

(code if anyone want to look at it)

#include <avr/math.h>
//double calctemp(int value);
#define cA1 0.003354016
#define cB1 0.000256985
#define cC1 0.000002620131
#define cD1 0.00000006383091
double temp;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly: 
  temp=calctemp(analogRead(0));
  Serial.println((double)temp);
  delay(3000);
}

double calctemp(int value)
{
  double temp;
  Serial.print("value: ");Serial.println(value);
  Serial.print("r/ r ref:");Serial.println(((10240000.0/(double)value)-10000.0)/10000.0);
  Serial.print("a1:");Serial.println(cA1,5);
  Serial.print("b1*log(...):");Serial.println(cB1*log((((10240000.0/(double)value)-10000.0)/10000.0)),5);
  Serial.print("c1*log^2(...):");Serial.println(cC1*pow(log(((10240000.0/(double)value)-10000)/10000.0),2),5);
  Serial.print("d1*log^3(...):");Serial.println(cD1*pow(log(((10240000.0/(double)value)-10000)/10000.0),3),5);
  Serial.print("sum:");Serial.println(cA1+cB1*log(((10240000.0/(double)value)-10000)/10000.0)+cC1*pow(log(((10240000.0/(double)value)-10000)/10000.0),2.0)+cD1*pow(log(((10240000.0/(double)value)-10000)/10000.0),3.0),5);
  Serial.print("final:");Serial.println(pow((cA1+cB1*log(((10240000.0/(double)value)-10000)/10000.0)+cC1*pow(log(((10240000.0/(double)value)-10000)/10000.0),2.0)+cD1*pow(log(((10240000.0/(double)value)-10000)/10000.0),3.0)),-1.0)-273.15);
  return temp;
  
}
  Serial.println((double)temp);

Why are you casting a double to a double?

I forgot to take that of, it was when I only got "nan" output ant though maybe there was an error in the data type... anyway that print is always zero in the actual code since the calctemp() function does not return anything ...

since the calctemp() function does not return anything ...

Removing all the useless Serial.print() statements, the function looks like:

double calctemp(int value)
{
  double temp;
  return temp;
  
}

I can't imagine why you expect it to return anything.

It is a modified version of the original code that didn't worked. The actual working code without the debugging output would be

#define cA1 0.003354016
#define cB1 0.000256985
#define cC1 0.000002620131
#define cD1 0.00000006383091
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly: 
  Serial.println(calctemp(analogRead(0)));
  delay(3000);
}

double calctemp(int value)
{
  return (pow((cA1+cB1*log(((10240000.0/(double)value)-10000)/10000.0)+cC1*pow(log(((10240000.0/(double)value)-10000)/10000.0),2.0)+cD1*pow(log(((10240000.0/(double)value)-10000)/10000.0),3.0)),-1.0)-273.15);  
}