ok So i'm braindead i know this should be easy but not sure how to express the non linear relationship of Resistance to temperature with the BWD WT3000 water temp sensor i have purchased.
I wrote BWD and got a decent chart showing Ohms resistance at specific Temps (apx). and have the sensors wired in and reading fairly consistant and displaying the rsults on a proccessing graph.
but of course the temp to voltage or temp to Resistance is not a constant so my readings are way off
i have attached a copy of the excell spreadsheet and graph showing the relationship based on the data but do not know how to express this in the program because im not sure how to write the equation that would lead to these results) needless to say trig etc. is a little beyond my education level
any help would be great
I'm using 5v input for the 2 wire sensor and reading Vout across a voltage divider to an analog pin
right now i'm using voltage but cannot figure out how to relate the voltage to the temp
float voltage1 = a1*(5.0/1023.0);
which is only accurate in about a 10 degree range I know i should be able to use a slope and offset in order to make this work but not sure how to figure out the calculation. and then put it into code
thanks all for the help i have got it figuring temps now and i have even managed to smooth the values to get rid of the temp. jump caused by god knows what. so onward to the next question my temps ap[pear to be aprox 5 degrees F hi almost all the time any ideas could a slope and offset be used here ?
if so how and how do i figure out what slope and offset to use ?
anyway direction would be great but not asking for you to figure it out for me.
code is below for what i have now attached that is
A few curly brackets were out of place, "index" is a reserved name in IDE 1.58 so changed it to "indx".
Curious why Vin is 4.86 ... shouldn't it be 5.0? Would this fix the 5 degF offset error?
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int raw1 = 10;
int readings[raw1]; // the readings from the analog input
int indx = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
int raw = 0;
float Vin = 4.86;
float Vout = 0;
int R1 = 150;
float R2 = 0;
float buffer = 0;
float Temp = 0;
void setup()
{
// initialize serial communication with computer:
Serial.begin(115200);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < raw1; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
total = total - readings[indx];
// read from the sensor:
readings[indx] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[indx];
// advance to the next position in the array:
indx = indx + 1;
// if we're at the end of the array...
if (indx >= raw1) {
// ...wrap around to the beginning:
indx = 0;
// calculate the average:
average = total / raw1;
// send it to the computer as ASCII digits
raw = average;
if (raw) {
buffer = raw * Vin;
Vout = (buffer) / 1023.0;
buffer = (Vin / Vout) - 1;
R2 = R1 * buffer;
buffer = R2;
float Temp = 1 / ( 0.00136499304133829 + 0.000251344732042827 * log(R2) + (-0.00000000550607815) * log(R2) * log(R2) * log(R2)) - 273.15;
float TempF = (Temp * 9.0 / 5.0) + 32.0;
Serial.print(" Vout:");
Serial.print(Vout);
Serial.print(" R2:");
Serial.print(R2);
Serial.print(" TEMPC:");
Serial.print(Temp);
Serial.print (" TEMPF:");
Serial.println (TempF);
delay(175);
}
}
}
Thanks all im running at 5.0v now the 4.86 was based on my fluke reading of actual volts supplied by the 5v on the arduino , i had often wondered about the 1024 vs the 1023 i have seen both in code on this site and im not sure which is right.
El_supremo i used your numbers first that you supplied and then tried a new set i got from the microsquirt calculator for coefficients that i found on this site somewhere prior to reading your post (had some trouble getting into the forums for a few days)
the ones i'm using now are getting me closer but will just for grins try yours again.
The buffer lines are just that a multipurpose storage spot of sorts found it with the code i bastardized for the averaging function to try and get rid of some nasty temp spikes every couple readings seams to work allthough it is a pain to use when trying to read 2 sensors at once actually had to make up some bs variable names cause you cannot rename (buffer) to anything else and use it twice.
had also considered doing the Log(r) only once as well may help alot
i'm working on the code for 2 sensors at once now and will post that too for anyones interest.
also i find myself wondering on the steihart-hart coefficients if i should be using the full range of the sensor for those resistances ie 150,0,-40 c as opposed to what most seam to do which is 150 or 100 , 60 and 0c
As I mentioned when I posted the coefficients in message #3, "This formula has an error of less than one third of a degree across the whole range" which is less than one degree Fahrenheit across the entire range.
If you don't need that wide a range of temperatures, you can calculate coefficients for a smaller range which should give an even smaller error.