Problems using Thermistor4 library

Hi all,
Part of my current project is using a thermistor to measure temperature for a home espresso machine.
I purchased a US Sensors USP11491 probe thermistor as it is +/-0.2°C Accuracy with a max temp of 150°C.

My research led me to believe that using the Steinhart-Hart coefficients for the thermistor would yield the best accuracy over larger ranges. I set up the thermistor as shown in the Thermistor4 Library Arduino Playground - Thermistor4

My thermistor has its own 78L05 regulator with a 10uF capacitor and a 0.1uF capacitor on the input side, and a 0.1uF capacitor on the output side to smooth any supply ripples. I also have a 0.1uF across the RFIXED resistor to smooth any jittery readings as shown in the Thermistor4 example.

For RFIXED, I am using a 1K 25 turn pot tuned to 678?, which on the J-curve (my thermistor is a J curve) corresponds to 100°C (actually 100°C = 678.60?), in order to maximise my sensitivity around boiling point.

I obtained the Steinhart-Hart coefficients from the manufacturer (US Sensors) ![](http://J Curve.JPG)
A = 1.12766979300187 E-03
B = 2.34444184128213 E-04
C = 8.47692130592308 E-08
D = 1.17512193579615 E-11

I measured the output from the 78L05 with 2 meters to get a consensus, both measured 4.95V.
I inputted all of this information into the code.

When I run the Thermistor4 code, my measured temps are consistently high.
Using a 50/50 mix of ice and water slush, that should be 0°C, I measure 8.9°C (48°F)
At body temp, measured with the probe in my mouth, (that should be about 97°F) for 5 min, I measure 41.1°C (106°F)
At boiling point at an altitude of approx 39m (128 ft) I measure 105°C (221°F)

According to US Sensors a typical accuracy at boiling point for this thermistor is +/- 0.9°C

I cannot figure out where I am going wrong, any advice is very welcome!
I am using Arduino 1.0.4, but also tested on Arduino 0022 with same results.

My code is below

//#include <Wire.h>
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(10, 9, 5, 6, 7, 8);

// Thermister4 Library version 4c. For Arduino 0018 or newer.
// (c)20101017 by MODAT7 under GNU GPLv1

//Example code of various functions using my 2 salvaged thermistors.

#include "Thermistor4.h"


//Faster serial means shorter delays in the program.
#define SERIALBAUD 115200

//Inside thermistor on Arduino ADC pin 0, Outside on pin 1.
#define THERMISTORPinInside 0
//#define THERMISTORPinOutside 1

//========================================

//One temperature monitoring thermistor for Inside and Outside.
Thermistor4 ThermistorInside;//, ThermistorOutside;
//various temp variables for testing.
unsigned int i, ADCAverage;
double mytemp;
//unsigned long mytime1, mytime2; //for speed tests


byte degree[8] = {140,146,146,140,128,128,128,128};
//byte degree[8] = {14,31,27,31,14,0,0};


//========================================
void setup() {
  lcd.createChar(0,degree);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Testing....");
  
  /*

//pin floating paranoia, set pullup's (don't mess with 0 and 1 serial).
for(int i=2; i<20; i++) {
  pinMode(i, INPUT);
  digitalWrite(i, HIGH);
  }

*/

Serial.begin(SERIALBAUD); //set up serial port.

//My 2 salvaged thermistors are about 30k at room temperature.
ThermistorInside.Pin = THERMISTORPinInside; //Set the pin number.
ThermistorInside.SetUp(); //Sets up the analog read pin for internal AVR.
//pow() is used elsewhere so might as well be used here.
ThermistorInside.BitResolution=pow(2, 10)-1; //ATmega's have a 10bit ADC (2^10-1).
ThermistorInside.VoltageSupply=4.955; //My USB powers my ATmega325 chip at 4.95v. Meter this for accuracy.
ThermistorInside.ResistanceFixed=678; //Fixed resistor in the divider. Measured in ohms. Meter this for accuracy.
ThermistorInside.Offset=0.0; //adjust temperature in Kelvin up or down a little to account for unforseen variations.
//These numbers were generated from thermistor.sf.net and aren't quite right unless using the full equation.
ThermistorInside.SteinhartA1=1.12766979300187e-003;  //First Steinhart-Hart coefficient.
ThermistorInside.SteinhartA2=2.34444184128213e-004; //Second Steinhart-Hart coefficient.
ThermistorInside.SteinhartA3=8.47692130592308e-008;  //Third Steinhart-Hart coefficient.
ThermistorInside.SteinhartA4=1.17512193579615e-011; //Fourth Steinhart-Hart coefficient.

} //end setup()


//========================================
void loop() {

Serial.print("Inside Temp (F): "); 
lcd.setCursor(0,1);
lcd.print(ThermistorInside.GetFarenheit(), 2);
lcd.setCursor(5,1);
lcd.print((char)0);
lcd.print("F");
Serial.println(ThermistorInside.GetFarenheit(), 2);

//Example of averaging 10 reads. Note that analogRead() isn't very fast.
//Part of this code has to be done manually, then call ReadADC() with the averaged number.
//Note that an unsigned int is only 16 bits. Do not exceed it or it will roll over and return trash.
ADCAverage = 0; //reset the variable for each loop.
for(i=0; i<10; i++) {
  ADCAverage += analogRead(THERMISTORPinInside);
  //delay(50); //add an extra delay to spread the average out a little more?
  }
ThermistorInside.ReadADC(ADCAverage/10); //call ReadADC() with the averaged value.
ThermistorInside.CalculateTemperature(3); //generate the temperature numbers.


} //end loop()

//end of Thermistor4c.pde

Never used the thermistor4 library.
A quick check showed these formulas inside the lib
//1: Simplified: Temp = 1 / ( A + B(ln(R)) )
//2: Standard: Temp = 1 / ( A + B(ln(R)) + D(ln(R)^3) )
//3: Extended: Temp = 1 / ( A + B(ln(R)) + C(ln(R)^2) + D(ln(R)^3) )

The formula on the JPG you attached is different in the C and D parameter.
So either adapt that in the library or write your own calculation.

Thank you for your reply.
Well spotted, I had missed the different powers in the equation.

I have now corrected that, saved the file and restarted the Arduino IDE and am now getting negative temps (~ -23°F) when the current temp is approx 65°F.

I checked further in the library (altering libraries is very new to me) and I noticed in the Thermistor4.cpp library file that the CalculateTemperature method (lines 89 - 102) that the lines -

if(Selection>=2) { this->Temperature = this->Temperature + (this->SteinhartA4 * LnResist * LnResist * LnResist); }
//If 3, add in level 3.
if(Selection>=3) { this->Temperature = this->Temperature + (this->SteinhartA3 * LnResist * LnResist ); }

needed to be updated as well, so I have changed them to -

if(Selection>=2) { this->Temperature = this->Temperature + (this->SteinhartA4 * LnResist * LnResist * LnResist * LnResist * LnResist); }
//If 3, add in level 3.
if(Selection>=3) { this->Temperature = this->Temperature + (this->SteinhartA3 * LnResist * LnResist * LnResist); }

However I am still getting negative temperatures at room temperature.

I noticed that the methods CalculateTemperature (line 89) and ReadCalculate (line 108) both have no return value (ie void).

Should they be returning a double or float value so that methods GetCentigrade and GetFarenheit are passed a value?

Thanks in advance :slight_smile:

Nevermind, I found the problem.
Its amazing what another cup of coffee can do!

I had entered the wrong value for the Fixed Resistor (facepalm!)

Now it seems to be working correctly!