Thermistor Readings

I have a circuit in which a thermistor is measuring the ambient temperature of the area around it. But the readings I am getting are not sensible, as they make no sense to a person. How would I get my Arduino to send out a reading in either celsius or fahrenheit temperature? Here is the code I am using. Thank you in advance for the help! :grin:
/*
// Potted Plant Protector
// By Luke Iseman
// With thanks to http://arduino.cc/en/Tutorial/LiquidCrystalDisplay
*/

//potted plant protector with rgb indicator LED and LCD

//with thanks to http://arduino.cc/en/Tutorial/LiquidCrystalDisplay

// Include the LCD Library
#include <LiquidCrystal.h>

//initialize variables for sensor pins
int moistPin = 0;
int tempPin = 1;
int lightPin = 2;

//initialize variables to store readings from sensors
int moistVal = 0;
int tempVal = 0;
int lightVal = 0;

//initialize variables for LED pins
int redPin = 11;
int greenPin = 10;
int bluePin = 9;

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

void setup()
{
//initialize the serial port
Serial.begin(9600);

//set LED pins to output mode
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

//set LED pins to off
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}

void loop()
{
moistVal = analogRead(moistPin);

//display moisture reading on lcd
lcd.clear();
lcd.print("moisture ");
lcd.print(moistVal);

//turn on LED to blue, others off if moisture pins are touching together
if (moistVal > 1000)
{
blue();
}
else
{
off();
}

//wait 4 seconds
delay(4000);
//display moisture reading on lcd
//Signal pump to turn on
if (moistVal < 5)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}

tempVal = analogRead(tempPin);
lcd.clear();
lcd.print("temperature ");
lcd.print(tempVal);

if (tempVal > 148)
{
red();
}
else
{
off();
}

delay(4000);

lightVal = analogRead(lightPin);
//display moisture reading on lcd
lcd.clear();
lcd.print("light ");
lcd.print(lightVal);

//turn on green if light sensor reads 600 or brighter
if (lightVal > 600)
{
green();
}
else
{
off();
}

delay(4000);

}

void blue()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
}

void red()
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
}

void green()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
}

void off()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);

}

void on()
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}

You can't, at least not without an actual temp sensor to calibrate it with. With no external reference , no amount of code is going to help you. It's a thermister. It was never designed to measure temperature. It was designed to measure changes in heat. If you want to measure temperature., buy a temperature sensor. That's about as simple as I can make it for you. If you think you're going to measure temperature without a calibration reference then your mistaken. If you can put it in ice water and measure it and then put it in boiling water then you have two references 0 degree Celsius and 100 degrees celsius. It will never be accurate for measuring temperature, period end of story.

Hi

Actually I looked up a tutorial on how to get a thermistor to measure temperature.

http://www.facstaff.bucknell.edu/mastascu/eLessonsHTML/Sensors/TempR.html

Hope that helps

So what exactly do the readings I am getting mean? I was getting readings in the hundreds at room temperature.
EDIT: So I found this Arduino Playground - Thermistor2, but how will it interact with my code? I do not have my arduino board with me at the moment unfortunately. Also in my code I am using 9600 for my serial, but in the code I found, the number 115200 is being used. Can I just alter the code I found to also be 9600 and everything work fine?

Well
Assuming you have the same circuit as described in the tutorial..
you can use the formulate, (which I have copied an pasted below, (from the link above)

How Does A Thermistor's Resistance Depend Upon Temperature?
The Steinhart-Hart equation gives the reciprocal of absolute temperature as a function of the resistance of a thermistor.
Using the Steinhardt-Hart equation, you can calculate the temperature of the thermistor from the measured resistance.
The Steinhardt-Hart equation is:

1/T = A + Bln(R) + C(ln(R))3 R in W, T in oK

The constants, A, B and C can be determined from experimental measurements of resistance, or they can be calculated from tabular data.

Here are some data points for a typical thermistor from "The Temperature Handbook" (Omega Engineering, Inc., 1989). (By the way, when you refer to this thermistor, you would say it has 5kW at room temperature.)
T (Deg-C) R (W)
0 16,330
25 5000
50 1801

So your numbers are a little out as to the tutorial, I expect that you can change your stuff to be similar to the tutorial.
at least you will get numbers you can relate to..

Why are you trying to do this ?
If you want to measure temperatue why don't you get a temp sensor?

Well the total circuit I am using is more complex than that.
http://i1.wp.com/makezineblog.files.wordpress.com/2013/11/schematicb.jpg?resize=620%2C465
This is the circuit I am using. Although looking at the two, it seems as if I am using the same circuit in the example, as I am using within my overall circuit!

@raschemmel I am doing this due to my ignorance. When I first started this project I assumed a thermistor will do what I thought it would do. Sadly I was wrong :blush:, so rather than buying a new sensor to use. I figure I can probably code my way to redemption! :grin:

Well that is ugly. Thinking of just coding it is not eeezee.
I see no ln, (natural log) Math function in the manual.

Ok, so the ugly way to do it is to make an array of pre-calculated answers.
and compare your number read against them.
It should work.. but memory might be a concern if you want to do this accurately.
/shrug.

raschemmel's statement of buying a thermal device that pumps out the right numbers might be more simple.

Would I not just use the simplified example code? I will just have to add that I only want the equation to be applied to whichever analog pin I have my thermistor connected to?

Temperature measurements were done with Thermistors successfully for many years prior to integrated devices that automatically return Temperature...
I remember a page from PH Anderson http://www.phanderson.com/picaxe/lin_thermistor.html that provides both the math, a table of values and much other useful information for efficient use of thermistors on both the PICAXE and the Arduino... With minor changes nearly any thermistor can be used and are generally much lower in price than many "sensors" (Linear types... LM34 centigrade, LM35 Fahrenheit, TMP36 Deg C, LM335, Deg K),
( Digital types DS18B20, DHT11 / 22 Deg C and RH and a bunch of other digital types as well from National/Ti)
While the analog or digital devices are accurate and easy to use they can be a great deal more expensive than a simple thermistor..
For that matter a silicon diode can also be used... I used a 1N4148 or a PN2907 BE junction for several commercial designs I did about 10 years ago... It did require an Op-Amp and a couple of pots but it was cost effective compared to a thermistor and the support electronics provided 3 point calibration which if carefully applied can be easily 1 - 2% accurate.. I used them (diodes with an analog LCD panel meter IC. an ICL 7105 and a bare LCD display as the 7105 was the LCD controller and A/D conversion circuit too.
I've used all of them at one time or another and found them all most useful...
I don't recommend the Analog types as most except the diode or thermistor generally are 10mV/Deg C and that's a half a bit or 4.8 MV.. The issue is complex but breaks down to the A/D converter not having a separate Analog return so it does have some.. Digital noise issues and the LSB frequently is a throwaway because of the induced digital noise both from the digital ground loops and the lack of real filtering on the AVcc supply.. 100 nF is barely adequate... but you should have no real problems past the LSB bobble...
Perhaps the best part is that there isn't a special library required for the thermistor and it can be assembled for less than a dollar with equal accuracy as the integrated devices... Good education too...

Doc

In other words, no simple plug & play formula?

Yes for the thermistor on the Anderson page as I wrote in my last post..
in case you missed it.. http://www.phanderson.com/picaxe/lin_thermistor.html.
copied here...

// LIN_THERM - Arduino
//
// Continually measures temperature using linearized thermistor.
//
// copyright, Peter H Anderson, Oct 1, '10

unsigned int ad_meas(byte channel, byte num);
void display_temperature(int T);

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

void loop()
{
    unsigned long Rtherm;
    unsigned int adval, R_therm, R_therm_10;
    int T_10;
    
    Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
    
    while(1)
    {
        adval = ad_meas(0, 32);
    
        R_therm = ((unsigned long) 2200) * adval / (1024 - adval); 
        
        if (R_therm > 3700)
        {
            T_10 = 9999;   // out of range
        }
        else if (R_therm > 1830)
        {
            T_10 = (R_therm * 10 - 37970) / -39;
        }
        else if (R_therm > 827)
        {
            T_10 = (R_therm * 10 - 33600) / -31;
        }
        else if (R_therm > 570)
        {
            T_10 = (R_therm * 10 - 22780) / -17;
        }
        else
        {
            T_10 = 9999;  // out of range
        }
        display_temperature(T_10);
        
        delay(5000);
    }        
}

unsigned int ad_meas(byte channel, byte num)
{
   unsigned int sum = 0, adval;
   byte n;
  
   for (n=0; n<num; n++)
   {
       adval = analogRead(channel);
       sum = sum + adval;
   }
   return(sum / num);
}

void display_temperature(int T)
{
    int whole, fract;
    
    whole = T / 10;  fract = T % 10;
    
    Serial.print(whole, DEC);
    Serial.print(".");
    Serial.println(fract, DEC);
}

Simple enough?

http://playground.arduino.cc/ComponentLib/Thermistor

does not mention if this is a ntc or ptc.

the bottom line is the resistance is a curve, not a straight line.
either look-up or calculate.

if high accuracy is needed, calibration is required

as raschemmel mentioned, ice water is 0°C and boiling water is 100°C at sea level, at 29.92inhg barometric pressure
that is not tap water, but distilled water. contaminants can alter the properties of the fluid, but the principle is sound.