I am trying to output the temperature from 2 thermistors with smoothing. I am using an arduino uno with a 10K thermistor (SEMITEC 103AT) with a 10k resistor. I have 2 problems with my code right now
- When connected to 5V as reference I am getting 23.5C (room is probably 21C) connected to 3.3V I get 6C I don't know how this is possible.
- If I have a thermistor only connected to A0 the serial output associated with the pin A1 will change as well although it will be a lower number. I have tried adding serial outputs to try to understand where my problem is but I'm stumped.
#include <math.h>
const int numReadings = 5; // number of cacluations used for smoothing
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0;
int average = 0; // the running total
int average1 = 0; // the average
int average2 = 0;
#define ThermistorPIN0 0 // Analog Pin 0
#define ThermistorPIN1 1 // Analog Pin 1
float vcc = 4.91; // only used for display purposes, if used
// set to the measured Vcc.
float pad = 9950; // balance/pad resistor value, set this to
// the measured resistance of your pad resistor
float thermr = 10000; // thermistor nominal resistance
float A = 0.8880739089e-3 ; // values from thermistor SEMITEC 103AT SRS Thermistor Calculator
float B = 2.514251712e-4 ;
float C = 1.922794488e-7 ;
float Thermistor(int analog) {
long Resistance;
float Temp; // Dual-Purpose variable to save space.
Resistance=pad*((1024.0 / analog) - 1);
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later
Temp = 1 / (A + (B * Temp) + (C * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celsius
return Temp; // Return the Temperature
}
void setup() {
Serial.begin(115200);
for (int thisReading = 0; thisReading < numReadings; thisReading++) // initialize all the readings to 0:
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
do
{ // total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(ThermistorPIN0);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
Serial.print("Total: ");
Serial.print(total,1);
Serial.println("");
Serial.print("index: ");
Serial.print(index,1);
// if we're at the end of the array...
} while (index < numReadings) ;
// calculate the average:
average = total / numReadings;
float temp1=Thermistor(average); // read ADC and convert it to Celsius
index = 0;
total = 0;
readings[0]=0;
// ------------------------------------------------
// second sensor
do{
// { total= total - readings[index];
readings[index] = analogRead(ThermistorPIN1);
total= total + readings[index];
index = index + 1;
Serial.print("Total2 : ");
Serial.print(total,1);
Serial.println("");
Serial.print("index2 : ");
Serial.print(index,1);
} while (index < numReadings) ;
average2 = total / numReadings;
float temp2=Thermistor(average2); // read ADC and convert it to Celsius
index = 0;
total = 0;
readings[0]=0;
Serial.print("Temp #1 deg C: ");
Serial.print(temp1,1); // display Celsius
Serial.print("Temp #2 deg C: ");
Serial.print(temp2,1); // display Celsius
Serial.println("");
delay(1000); // Delay a bit...
}