Measuring Temperature using an NTC Thermistor

copyright, Peter H Anderson, Baltimore, MD, May, '07


Discussion to be added.


// NTC_Thermistor (Arduino)
//
// Illustrates measuring temperature using an NTC thermistor in
// a voltage divider configuration.
//
//
// copyright, Peter H. Anderson, Baltimore, MD, May, '07

#include <math.h>

int measTemperature(int pin);
float calcRthermistor(int ADVal);
float calcTc(float Rtherm);
float square(float x);
float cube(float x);

void setup()
{

  Serial.begin(9600);
  delay(100);
}    

void loop()
{
  int Tc_100, Whole, Fract;
  Tc_100 = measTemperature(5); 
  Whole = Tc_100 / 100;
  Fract = Tc_100 % 100;
  Serial.print(Whole);
  Serial.print(".");
  if (Fract < 10)
  {
    Serial.print("0");
  }
  Serial.print(Fract);
  Serial.print("\n");
  delay(5000);
}

int measTemperature(int pin)
{
    int ADVal;
    float RThermistor, Tc;
    
    ADVal = analogRead(pin);
    RThermistor = calcRthermistor(ADVal);
    Tc = calcTc(RThermistor);
    return((int) (Tc * 100.0));
}

float calcRthermistor(int ADVal)
{
    float Rtherm;
    if (ADVal <=0) // avoid trouble conditions
    {
       ADVal = 10;
    }
    Rtherm = 10.0e3 / (1024.0 /((float) ADVal) - 1.0);
    return(Rtherm);
}

float calcTc(float RTherm)
{
  const float A_const = 3.354016e-3;
  const float B_const = 2.569107e-4;
  const float C_const = 2.626311e-6;
  const float D_const = 0.675278e-7;
    
  float x, TKelvin, Tc;
  
  x = log(RTherm / 10.0e3);
  TKelvin = 1.0 / (A_const + B_const * x
              + C_const * square(x) + D_const * cube(x));
  Tc = TKelvin - 273.15;
  return(Tc);
}
                
float square(float x)
{
  return(x * x);
}

float cube(float x)
{
  return(square(x) * x);
}