Pages: [1]   Go Down
Author Topic: LM35 Temp Code Inquiry....  (Read 637 times)
0 Members and 1 Guest are viewing this topic.
New York, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
You may be disappointed if you fail, but you are doomed if you don't try!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, it seems that there are tons of postings and links to LM34/35 questions, but despite all the looking I can't seem to get any reasonable results out of my LM35 sensor. I am posting a fragment of the code (sorry if I missed a bracket or two) as well as the serial output. The code compiles and uploads fine, just that the results are inconsistent with the temperature (obviously, given the output). There is some type of math error with relation to conversion to temp, but I have never worked with this sensor before so I am a bit stuck. Any guidance would be appreciated.    At the input pin of the LM35 the voltage measures around .24 vdc, and the digital pin is pin 6 in case that helps.

Thanks a ton!
-Dave



Code:
tempc=0;
tempf=0;
for(i = 0 ; i <= 7 ; i++){            // gets 8 samples of LM-34/35 temperature data
 
  samples[i] = ( 5.0 * analogRead(tempPin) * 100.0) / 1024.0;
  tempc = tempc + samples[i];
  delay(4);

}

tempc = tempc/8.0;                    // improves precision
tempf = (tempc * 9)/ 5 + 32;          // converts to fahrenheit

//if(tempc > maxi) {maxi = tempc;}     // set max temperature
//if(tempc < mini) {mini = tempc;}     // set min temperature

// ASCII serial output
//TEMP PRINTOUT SECTION
//Serial.print (analogRead(tempPin));
//Serial.print("     ");

Serial.print(tempc,DEC);
Serial.print(" Celsius, ");

Serial.print(tempf,DEC);
Serial.println(" fahrenheit -> ");

Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");

 // Serial.println(readings[4], DEC);

tempc = 0;
lcd.print(tempf);

delay(4);                          // delay before loop (optional - remove if delay is too great)




Output:
Code:
161 Celsius, 321 fahrenheit ->
163 Celsius, 325 fahrenheit ->
170 Celsius, 338 fahrenheit ->
166 Celsius, 330 fahrenheit ->
159 Celsius, 318 fahrenheit ->
161 Celsius, 321 fahrenheit ->
163 Celsius, 325 fahrenheit ->
170 Celsius, 338 fahrenheit ->
166 Celsius, 330 fahrenheit ->
158 Celsius, 316 fahrenheit ->
160 Celsius, 320 fahrenheit ->
161 Celsius, 321 fahrenheit ->
162 Celsius, 323 fahrenheit ->
156 Celsius, 312 fahrenheit ->
147 Celsius, 296 fahrenheit ->
151 Celsius, 303 fahrenheit ->
155 Celsius, 311 fahrenheit ->
164 Celsius, 327 fahrenheit ->
161 Celsius, 321 fahrenheit ->
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post the whole code as I cannot see what type tempC and tempF and samples[] are. If they are integer you get rounding problems in the math of the sketch resulting in strange output.

The fact that you see some fluctuation (last 2 bits or so) is "normal".

Have you read the reference page about AREF ==> analog Reference?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: