Cannot get 10k thermistor to work

Hi,

Using the Steinhart-Hart Thermistor equation I'm trying to read the temperature from a 10k thermistor and I have calculated the values for A = 1.7196E-03, B = 2.2811E-4 and C = 1.8119E-7 by using the data sheet from the RS website (2.0860, 1.2683 and 0.79420 at 10, 20 and 30 degrees with respect)

All I get is 307 degrees C regardless of whether the circuit is connected to pin 5 or not. I've tried new resistors and thermistors.

Can anyone help guide me to as where I've gone worng?

#include <math.h>
double Thermistor(int RawADC) {
 
 //    Inputs ADC Value from Thermistor and outputs Temperature in Celsius
 //    Below is the Steinhart-Hart Thermistor equation being used:
 //    Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
 //    A = 1.7196E-03, B = 2.2811E-4 and C = 1.8119E-7
 //    Calculated using the data sheet from the RS website and w-w-w.unb.ca/civil/hydro/S-H%20coefficients.xls
 
 long Resistance;
 int Temperature; 
 int Temporary;

 Resistance = ((1024/RawADC));                    // Resistance for the 10k Thermistor.  Resistance = (1024/ADC)
 Temporary = log(Resistance);                         // Saving the Log(resistance) so it doesn't need to be calculated again in the equation below
 Temperature = 1 / (0.0017196 + (0.0002811 * Temporary) + (0.00000018119 * Temporary * Temporary * Temporary));   
 Temperature = Temperature - 273.15;                     // Convert Kelvin to Celsius
 
 return Temperature;                                // Return the Temperature
}

void printDouble(double val, byte precision) {

  
  Serial.print (int(val));                          //Prints int
  
  if(precision > 0) {
    Serial.print("."); 
    unsigned long frac; 
    unsigned long mult = 1;
    byte padding = precision -1;
    while(precision--)
      mult *=10;
    
      if(val >= 0)
        frac = (val - int(val)) * mult;
      else 
        frac = (int(val) - val) * mult;
      
      unsigned long frac1 = frac;
      while(frac1 /= 10)
        padding--;
      while(padding--) 
        Serial.print("0");            
    Serial.print(frac,2) ;                          //(frac,2 <---  The two is for the amount of decimal points)
  }
}

void setup() {
 Serial.begin(9600);
}

#define ThermistorPIN 5  // Analog Pin 5
double Temperature;
void loop() {
 Temperature=Thermistor(analogRead(ThermistorPIN));     // Reads the ADC and converts it to Celsius
 Serial.print("The temperature in Celsius is: "); 
 printDouble (Temperature,2);                       // Display temperature in Celsius (same decimal place rule as before)
 Serial.println("");                                    // Add a new line
 delay(1000);                                           // Delays the time between readings.  nb.  Don't make it so fast that it can't be transmitted
}

can you describe how you have it hooked up?

also, integers only hold...well...integers (whole numbers). you should really be using float values.

Have you tested the thermistor with a meter to make sure it is working and find the values it gives at different temperatures?

As pointed out earlier, you need to be using floats where appropriate, and not ints.

For example, Temporary = log(Resistance); will result in 0 for any analogRead() value of > 375.

!c

Thanks for the replies and not replying myself, I've scrapped the code and gone with a different route.

I've now sent a digital Voltage value across from an end node and then calculating the temperature at the coordinator.

Basically my overall aim was to read a Thermistor, send it using an XBee shield over and read it at the other end. I'll try and draw the schematic:

XBee End device:

Ground - 10k Resistor -- 10k Thermistor - 5V
|
Analog Pin 5
|
Arduino
|
XBee

XBee End device code:

int Vdigital;
void setup()

{
  Serial.begin(9600);  
}

#define ThermistorPIN 5   // Analog Pin 5

void loop() 
{  
  Vdigital = analogRead(ThermistorPIN);
  Vdigital = (Vdigital/4);
  Serial.print(Vdigital, BYTE);
  delay(1000);  
}

This works fine and I get good values from it. I decided that doing the calculations at the Co-ordinator would be easier as sending the Vdigital as a byte would be easier. I'm having trouble with the coordinator and although below isn't my final code, I was trying to read vDigital and then assign it to another variable. vDigital prints out with an integer which is fine. I'm struggling to assign it to another variable (T2) and print it out with decimal places such as .00 formatting. The code works fine when I comment out the decimal calculations with the I/O monitor showing the correct value and when I try to use the decimal code I get a value such as 111000000 for 448.

XBee Coordinator:

XBee
|
Arduino (I/O monitor)

The code used for the coordinator is:

#include <math.h>

int vDigital;
int B;
int RT1;
int T1;
double T2;

void printDouble( double val, byte precision){
  // prints val with number of decimal places determine by precision
  // precision is a number from 0 to 6 indicating the desired decimial places
  // example: printDouble( 3.1415, 2); // prints 3.14 (two decimal places)

  Serial.print (int(val));  //prints the int part
  if( precision > 0) {
    Serial.print("."); // print the decimal point
    unsigned long frac;
    unsigned long mult = 1;
    byte padding = precision -1;
    while(precision--)
       mult *=10;

    if(val >= 0)
      frac = (val - int(val)) * mult;
    else
      frac = (int(val)- val ) * mult;
    unsigned long frac1 = frac;
    while( frac1 /= 10 )
      padding--;
    while(  padding--)
      Serial.print("0");
    Serial.print(frac,DEC) ;
  }
}


void setup()
{
  Serial.begin(9600);
  B = 4300;
  RT1 = 10000;
  T1 = 298.15;
}



 
void loop()
{
  if (Serial.available()) 
  {
    vDigital = Serial.read();
  }
    {
    vDigital = (vDigital*4);
    T2 = vDigital;
    Serial.println(vDigital);
    delay(1000);
    Serial.println(T2,2);
    delay(1000);
    }
}

I haven't introduced the calculations into it as I'm trying to get this step working before I do but the calculation coding I have is (cut down to just show calculations):

#include <math.h>

  int B;          //Declaring values of the known with
  long RT1;       //others being added when received 
  int RT2;       //over ZigBee.  RT1 is known resistance
  float T1;       //at known temperature (T1).  T2 is the
  float T2;       //temperature recieved from RT2.
  double RT;      //So I can do ln(RT) instead of ln(RT1/RT2)
  int Vdigital;
  int input;

void setup()
{
  Serial.begin(9600);
  B = 4300;
  T1 = 298.15;
  RT1 = 10000; 
}

    void loop()
{
  if (Serial.available());                        
    {
      input = Serial.read();
      Vdigital = (input * 4);
      Serial.println(Vdigital);
      delay(1000); 
    }

    RT2 = ((10000 * Vdigital) / (1024 - Vdigital));
    RT = (RT1/RT2);
    T2 = ((B*T1) / ((B-T1) * (log(RT))));

   Serial.print("The temperature in Celsius is: ");   
   printDouble (T2,2);                                 // display temperature in Celsius (same decimal place rule as before)
   Serial.println("");                                 // Add a new line
   delay(1000);                                        // Delays the time between readings.  Don't make it so fast that it can't be transmitted

}