I am having trouble with my code below. It is switching between two inaccurate numbers when trying to read the temperature through my thermistor sensor. Any help would be very appreciated!
byte tempPin = A0; // connect a Patton Robotics thermistor/VDB to pin A0
void setup() {
Serial.begin (9600);
pinMode (tempPin, INPUT); // this is not necessary for analog input pins
delay (100); // allow the serial window time to open
}
void loop() {
printTempData(); // print all relevant thermistor/temperature data
}
void printTempData () {
int rawTempData = analogRead(tempPin); // digital reading from the thermistor/VDB
Serial.println ("Raw thermistor data = " + String(rawTempData));
float tempVout = DAC(rawTempData); // convert the raw digital reading to volts
Serial.println ("Thermistor/VDB output voltage = " + String(tempVout) + " V");
float tempRv = calcRv(tempVout); // convert VDB voltage to resistance (in ohms)
// Serial.println ("Temp Rv = " + String(tempRv));
Serial.println ("Thermistor variable resistance, Rv = " + String(tempRv) + " ohms");
float Tk = calcTempK(tempRv); // convert thermistor resistance to temperature in K
float Tc = calcTempC(Tk); // convert temperature from Kelvin to Celsius
float Tf = calcTempF(Tc); // convert temperature from Celsius to Fahrenheit
Serial.println ("Temperature = " + String(Tk) + " K");
Serial.println (" = " + String(Tc) + " C");
Serial.println (" = " + String(Tf) + " F");
Serial.println (""); delay (250); // delay to read the data
}
// DAC() is a function that converts a 10-bit digital sensor value into a voltage
float DAC(int digitalValue) {
float analogValue = digitalValue * 3.3 / 1023.0; // 1023 because 10-bit resolution
return analogValue;
}
// calcRv() takes the output voltage from the VDB and returns the value (in ohms) of the
// resistor that is screwed into the VDB’s terminal block.
float calcRv(float Vout) {
const float Rf = 9900; // the VDB’s fixed resistor in ohms – do not assume 10k!
const float Vin = 5.00; // the input voltage (V). Ensure PRT_28 power pin is 5V!
float Rv = ( Vout / (Vin - Vout) ) * Rf; // see Equation 21.10
return Rv;
}
// calcTempK() takes the resistance of the Patton Robotics thermistor
// and returns the temperature in Kelvin units (K)
float calcTempK(float R) {
const float R0 = 90; // the thermistor’s reference resistance in ohms (1000)
const float T0 = 289.15; // the thermistor’s reference temperature in Kelvin = 298.15
const float B = 4038; // the thermistor’s Beta constant in Kelvin
float inverseTk = (1.0 / T0) + (1.0 / B) * log(R / R0); // 1/Tk (see Equation 21.12)
float Tk = 1.0 / inverseTk; // the thermistor’s temperature in Kelvin
return Tk;
} // calcTempC() converts a temperature from Kelvin (K) to Celsius (C)
float calcTempC(float Tk) {
float Tc = Tk - 273.15; // temperature in Celsius (see Equation 21.13)
return Tc;
} // calcTempF() converts a temperature from Celsius (C) to Fahrenheit (F)
float calcTempF(float Tc) {
float Tf = (9.0 / 5.0) * Tc + 32.0; // temperature in Fahrenheit (see Equation 21.13)
return Tf;
}