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
}
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
}