I have a resistive temperature sensor which runs at 2k ohms around room temp and 200 ohms at 80C.
I have this fed from 5v to a 2k resistor and the mid point of the two resistor bridge is being fed to an analog input.
So at room temp the analog input sees about 2.25v and as the temp rises, it rises to about 4.5v.
I am taking a pwm output and RC filtering it into a bar graph.
My problem is from 80c (4.5v analog in) to just above 20C (2.3ish analog in) the PWN seems to fall nicely but then right around 20C when the analog is 2.25v the pwm jumps up again and my bar graph goes full scale.
I think it is the way I am using MAP() which I clearly dont fully understand.
Also I have tested the sensor over the temp range and it is fairly linear but when it reports on the serial monitor, the maths I have in is only accurate around 60-80C.
If I understand analog input range correctly 0-5v is 0- 1023. I am running from 4.5v to 2.25v so a range of 2.25v or 460 (i used 512 in the program just as a start). My final temp gauge scale will be 0 - 120C so my calculation to convert to temp should be;
temp = val1*(460/120)
int val = analogRead(0);
int val1 = analogRead(0);
int temp;
val = map(val, 0, 512, 0, 255);
analogWrite(9, val);
Serial.println (val1);
delay (2000);
temp = val1 * 3.83;
Serial.println (temp);
delay (2000);
What analog value do You read at 4.5 volt? Id that value within the mapping space of 0 to 512?
Have You been reading the Arduino Reference abot the map function?
Add a Serial.print so you can see the actual values out of the sensor, i.e. val AND val1. You'll probably find them going from something like 1000 to 450 NOT 0 to 512.
And just for interest, why are you reading the same pin twice, once for the motor and then again for the temperature? Surely once would be plenty.
Hammy - that is all my simple code to get a PWM signal out.
As in the post I did a do a linearity test (graph temp against resistance) at it looked better that my serial print of val1 (corrected to temp) was saying.
Railroader....good point I will check but my assumption is at 4.5 v it will be close to 1023 so my MAP() is wrong and should read ?
val = map(val, 512, 1023, 0, 255);
I did read the Arduino reference but am obviously confused, so will do again.
Slipstick. I was only reading the same pin twice because I though the MAP() function may be modifying val and I wanted to see 'real' values hence serial print val1 (temp).
In commersial products, heavy machines, we used a quite simple and low price NTC resistor. Using a certain resistor to Vcc, The interpretation of the analog reading was split into 3 intervals, each using a unic convertion factor. The max error from -30 C to 120 C was only 3 degrees. Find that approach!
If you input value goes below 275, the output value will go below 0.
There are no constraints on the output values if you input goes outside the two parameters set.
The resulting A/D value of a thermistor (NTC) is normally converted to temp with a Steinhart-Hart formula.
The pull-up resistor value should be about the same value as the thermistor in the middle of the temp of interest. Only then can you be certain of getting a fair resolution across the whole temp span.
A thermistor with pull up is far from linear, so map() is a poor choice. MultiMap() could do better.
Finding the Steinhart-Hart coefficients is the way to max linearity.
Leo..
So I set up a test with a bucket of hot water and a calibrated (i hope) external temperature sensor.
The results are attached.
My val1 to temp maths from my previous post is;
So with a range of around 1.9v to 5v and 0 - 120C my maths temp convert should be;
1023-275 = 748.
748/120 = 6.23 so temp is;
temp - val1 / 6.23
.
The excel attached shows three values for the multiplier and the closest match I can get is 11.6 for the range and the calculated 6.23 is no where close and I cant see why even given the non linearity it should be close somewhere in the range?