Go Down

Topic: LM35 Temp Code Inquiry.... (Read 907 times)previous topic - next topic

ddeprospero

Nov 14, 2011, 07:28 pm
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: [Select]
`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 precisiontempf = (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: [Select]
`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 ->`

robtillaart

#1
Nov 14, 2011, 08:35 pm
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".