Converting digitalRead from NTC to temperature

Hi!

I have an equation that I can use to convert the analogRead from my Bosch automotive NTC thermistor to temperature in Celsius.
I just cant get the eguation to work. All I got to my LCD was “nan”…

The equation I’m trying to write is:

T= we are trying to solve
T0= 20
B= 3458
R0=2500

I have everything else working, lcd, getting a good analogRead value etc. I just do not know how to put that equation in to such a format that it works.
I tried: lcd.print(1/((1/293,15)+(1/3458)*log(analogRead(A0)/2500)))-273,15) this should give me the temperature in degrees of celcius but I get “nan”
At room temp the analogRead is somewhere around 280 using a 1K resistor as suggested by Bosch (1K or 3K)

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
lcd.print(“GBW’s 240 16VT”);
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(“OT”);
lcd.setCursor(3, 1);
lcd.print(1/((1/293,25)+(1/3458)*log(analogRead(A0)/2500)))-273,15;
delay(1000);
}

Yes I’m a noob still, but If someone can show me the correct way to write the equation I will learn from it and I will pass on the knowledge!

GBW

Dot versus Comma. A floating point number is in english format 123.45

If that's all there was wrong then I really feel dumb now sigh I also forgot to convert the analogRead to Ohms, but that's no biggie.

One shouldn't write code so late at night. You get so tired you miss the simplest of errors.

Thank you for the correction, I will try the code again today!

GBW

hi again.

for some reason, I cannot calculate correct results on arduino as I can on a calculator.

The equation for R is:
R= 1000/(1023/analogRead(A0)-1)

R I get 2089.something with a calculator
And when I lcd.print(R) it with Arduino I get a solid 1000.00

And because of that I can’t get the main equation to work to get a temp reading in Celcius:

I just get a solid reading= 66 when I lcd.print(temp) and when I lcd.print((1/((1/298.15)+(1/3368.4)*(log(R/4000))))-273.15); I get 66.70.
Neither will change when I heat the NTC sensor.
if I lcd.print(analogRead(A0)) it works and changes as I heat the NTC sensor.

AnalogRead(A0) = 351
T0 = +298.15C
R0=4000

#include <LiquidCrystal.h>
#include <math.h>

int mintemp=255;
int maxtemp=-255;

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  lcd.print("GBW Diagnostics");
  delay(3000);
}
 
void loop() {
  float R=1000/(1023/analogRead(A0)-1);
  int temp=(1/((1/298.15)+(1/3368.4)*(log(R/4000))))-273.15;
  if(maxtemp < temp)
 maxtemp=temp;
  if(mintemp > temp)
 mintemp=temp;
  lcd.setCursor(0,1);
  lcd.print((1/((1/298.15)+(1/3368.4)*(log(R/4000))))-273.15);
  delay(1000);
}

Could someone help me and maybe correct any flaws in my code? I project is stuck until the code works…
I’m really sorry for the trouble…

GBW

integer versus float
http://arduino.cc/en/Reference/int
http://arduino.cc/en/Reference/Float

Since you do calculations in float, it is best to do all those calculations in float.
Only analogRead() returns an integer, but make that float as soon as possible.
To prevent any confusion, let’s overdo it and write every number as a floating point number. So “1” becomes “1.0”.

I didn’t check the code, only made everything float…

#include <LiquidCrystal.h>
#include <math.h>

float mintemp = 255.0;
float maxtemp = -255.0;

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  lcd.print("GBW Diagnostics");
  delay(3000);
}
 
void loop() {
  float R = 1000.0 / (1023.0 / (float) analogRead(A0) - 1.0);
  float temp = (1.0 / ((1.0 / 298.15) + (1.0 / 3368.4) * (log( R / 4000.0)))) - 273.15;
  if(maxtemp < temp)
 maxtemp=temp;
  if(mintemp > temp)
 mintemp=temp;
  lcd.setCursor(0,1);
  lcd.print((1.0 / ((1.0 / 298.15) + (1.0 / 3368.4) * (log(R / 4000.0)))) - 273.15);
  delay(1000);
}

You also need more parentheses.

For this part, it is not clear.
Do you want the “-1.0” to do with “analogRead()” or with “1023.0/analogRead()” ?

float R = 1000.0 / (1023.0 / (float) analogRead(A0) - 1.0);

lcd.print(1/((1/293,15)+(1/3458)*log(analogRead(A0)/2500)))-273,15)

sometimes it makes sense to use variables to be able to read the formula (or at least add some spaces for readability )

And if you find the need to use numbers you can pre-calculate parts of them, especially divisions as these are expensive , and what’s more integer division (1/3458) causes errors

to keep it readable like your original formula

float T0 = 298.15;
float B = 3368.4;
float R0 = 4000.0;
float K2C = 273.15; // Kelvin2Celcius

float temp = 1.0 / (1.0 / T0 + 1.0 / B * log( R / R0) )  - K2C;

moving divisions to “const vars”

float T0 = 1.0/298.15;
float B = 1.0/3368.4;
float R0 = 1.0/4000.0;
float K2C = 273.15;  
int R = analogRead(A0);

float temp = 1.0 / (T0 + B * log( R * R0) )  - K2C;

finally if you use this formula in multiple places why not make a function out of it…

float temperature(int R)
{
  float T0 = 1.0/298.15;
  float B = 1.0/3368.4;
  float R0 = 1.0/4000.0;
  float K2C = 273.15;  
  return 1.0 / (T0 + B * log( R * R0) )  - K2C;
}