Getting the resistance of a thermistor

Hello. I have a 100k thermistor and a 100k resistor in a voltage divider. What I want to do is get the resistance of my thermistor so I can feed it to the Steinhart-Hart equation.
what I have so far is this…

rawADC = analogRead(0);
res = (500000/((rawADC/1024)*5)) - 100000;
temperature  = (1 / (thermA + (thermB * log(res)) + (thermC * log(res) * log(res) * log(res)))) - 273;

where res (initialised as a float) should be the resistance of the thermistor. I am trying to use R1=(R2.Vin)/Vout - R2 - sounds simple, but when I get the program to send rawADC and res to serial, I get values that change as expected for rawADC (so not hardware), but res always results in a value of -100001.00
I think I must be doing something obvious wrong but I can’t see what it is.

Incidentally the temperature equation works nicely given dummy values for res.

You have probably declared rawADC as an integer type, in which case the expression rawADC/1024 will always be 0. In C++, division of two integers gives an integer result. You need to write:




to force a floating-point computation.

-- The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons

That is exactly what I did, declaring rawADC as an int. Problem solved - Thankyou.

Just out of interest, does it store read values in ram as an integer too then? because my first iteration was something like

res = (500000/((analogRead(0)/1024)*5)) - 100000;

and the result was the same.

The return type of analogRead() is also an integer which is why you would be seeing the same problem. Thinking about variable type and function return type is what you want to think about rather than how your program “stores values in RAM”.

The Quick Shield: breakout all 28 pins to quick-connect terminals

Without any qualifiers, 500000 is treated as though it is an int. Read the documentation on the int data type, and see if this value can reasonably fit in an int.