Thermistor Sensor Data Collection

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

what microcontroller are you using?
what does the serial monitor output look like (upload as text)?
post a schematic of the circuit?

If you are using an AVR-based Arduino like the Uno R3, Mega, etc., get rid of the Strings. They cause memory problems and crashes.

This

Serial.println ("Thermistor/VDB output voltage = " + String(tempVout) + " V");

can be replaced by

Serial.print("Thermistor/VDB output voltage =  ");
Serial.print(tempVout);
Serial.println(" V");
1 Like

Here is a photo of my microcontroller:

Here is what is printing on the Serial Monitor:
“
Raw thermistor data = 11
Thermistor/VDB output voltage = 0.04 V
Thermistor variable resistance, Rv = 70.76 ohms
Temperature = 294.22 K
= 21.07 C
= 69.92 F
Raw thermistor data = 10
Thermistor/VDB output voltage = 0.03 V
Thermistor variable resistance, Rv = 64.29 ohms
Temperature = 296.29 K
= 23.14 C
= 73.65 F

Raw thermistor data = 11
Thermistor/VDB output voltage = 0.04 V
Thermistor variable resistance, Rv = 70.76 ohms
Temperature = 294.22 K
= 21.07 C
= 69.92 F
“
It is just flipping between 10 and 11 for the Raw Thermistor value, and the following temperatures (corresponding to the thermistor values). Thank you!

Hi! Thank you, this worked for a bit but now it has gone back to what it was previously doing. Any other suggestions? Thank you for your help! I also attached what is printing to the serial monitor and a picture of my micro controller.

"Raw thermistor data = 10
Thermistor/VDB output voltage = 0.03 V
Thermistor variable resistance, Rv = 64.29 ohms
Temperature = 296.29 K
= 23.14 C
= 73.65 F
Raw thermistor data = 11
Thermistor/VDB output voltage = 0.04 V
Thermistor variable resistance, Rv = 70.76 ohms
Temperature = 294.22 K
= 21.07 C
= 69.92 F.
"

Please post the revised code, using code tags, and a hand drawn wiring diagram. The picture cannot be meaningfully interpreted.

Is that a servo? If so, what is it doing? Servos cannot be powered by Arduinos.

link please?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.