I’m trying to start my weather station learning project. At the moment, I have the thin little Thermistor - 100K. I don’t know the exact model as it came in a ‘Starter Pack’.
I am read the device, and showing it on a OLED screen.
The issue I face is the temperature bounces around a lot. Each read is different… Sometimes by a degree… some times less.
It’s using a 100K ohm resistor.
So what I thought was, add a ‘Wait’ time… and in that time, keep sampling values… then average them out, and return the average for that wait period.
THis has helped, but I’d hope the temp would stay within a xx.x range… but it still changes between 24.5 and 25.5 within checks.
Is there a way to try flatten this out and make it more accurate? Maybe make the pause time longer? At the moment, I sample for 3 seconds… but maybe I need more?
Here’s my code. I created class to handle the device:
/*
- A class to handle the reading of temperature data.
*/
class Thermostat {
int ThermistorPin = 0; // The pin this device will listen on.
int Vo; // The voltage we read from the pin.
int Period; // The time to wait to calculate the average.
float R1 = 100000; // The resistance of the resistor used.
float logR2, R2, T; // Fields for temperature calculation
float c1 = 1.009249522e-03,
c2 = 2.378405444e-04,
c3 = 2.019202697e-07; // No idea… Just values.
public:
Thermostat(int pin, float resistor, int period) {
ThermistorPin = pin;
R1 = resistor;
Period = period;
pinMode(ThermistorPin, OUTPUT);
}
int readCount = 0;
float totalReadValue = 0;
unsigned long startMillis = 0;
/*
- Read the value from the device.
*/
float Read() {
startMillis = millis();
readCount = 0;
totalReadValue = 0;
while(millis() - startMillis < Period) {
Serial.println(“Checking voltage…”);
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
T = T - 273.15;
readCount ++;
totalReadValue = totalReadValue + T;
}
Serial.println(“Returning…”);
Serial.println(totalReadValue);
Serial.println(readCount);
return totalReadValue / readCount;
}
};
Then I create the object in my main code:
// Create the devices.
Thermostat thermostat(THERMOSTAT_PIN, 100000, 4000);
And then I poll it every 20ms. (So the time between screen updates is 20ms + pause time.