0
votes

We are using an LM35 temperature sensor with the Arduino board pin A0 to A7, any one pin of them really. The problem is that I can not get the steady and accurate value in the serial window in the Arduino software. Below is the code I am using:

int pin = 0; // analog pin
int tempc = 0, tempf = 0; // Temperature variables
int samples[8]; // Variables to make a better precision
int maxi = -100, mini = 100; // To start max/min temperature
int i;

void setup()
{
    Serial.begin(9600); // Start serial communication
}

void loop()
{
    for(i = 0; i <= 7; i++) { // Gets 8 samples of temperature
        samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
        tempc = tempc + samples[i];
        delay(1000);
    }

    tempc = tempc/8.0; // Better 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

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

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

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

    tempc = 0;

    delay(1000); // Delay before loop
}
2
So - what do you get? (What is the problem)spring
actually sir i didn't get the exact temp. value on the serial monitor....it was like that 450C and smthing more and more...........and we are using arduino Gboard. actually sir i did not get u properlly, can u give me correction in my code....jalpan
I think this youtube video has all types of basic temparature detection and convertion using LM35 Sensor and Arduino. Code of this video is good enough to detect realtime data. This video tested with google weather youtube.com/watch?v=Qbe9yAIK-Aouser6123079

2 Answers

1
votes

It would be nice to know what the issues are, but here are some things to consider:

  • You are averaging several samples. This is a good step.
  • You can make averaging a little better by doing the voltage-to-temperature conversion afterwards and not in the loop itself. In the loop, just add the analogRead reading to some value, then convert it to temperature before printing. This way you avoid some potential floating-point rounding errors.
  • If you don't do that, at least store samples as floats
  • Make sure your voltages are steady, especially if you are running on a battery. Have a nice big bypass capacitor between the power and ground pins of the Arduino.
  • You don't show how the LM35 is hooked up. That could be part of the problem. Also, if I remember correctly, it has a very limited ability to drive capacitive loads. If your leads to LM35 are long, then the wiring itself will inherently have some capacitance. Take a look at LM35 datasheet to see how the connection between LM35 and the microcontroller could be made more robust.
  • If you want to get more technical, take a look at this appnote on how to increase AVR ADC precision. Though depending on which Arduino you use, this may not be applicable.

However, the bigger problem could be the inherent precision of your circuit. At 25 degrees, the LM35 outputs .25 V, which shows up as reading 51 on your ADC, and for every +1 degree change in temperature you get +2 reading from the ADC, so the ADC is accurate to 1/2 a degree. LM35 is accurate to 1/2 a degree at room temperature, so now you are at +1/-1 deg C accuracy, and this could be the cause of your jitter. If you are just measuring temperatures under 100 degrees C, you could use a 3.3 V reference for your ADC (again depending on which Arduino you use) which will give you a better precision.

However, you will always have some jitter.

0
votes
  void setup() {
   // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
  }

  int samples =0;
  float temprature = 0.0;
  // the loop routine runs over and over again forever:
  void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a temprature
  temprature += sensorValue * (500.0 / 1023.0);
  // print out the value you read:
  samples++;
  if(samples >= 10){
      temprature = temprature/10;
      Serial.println(temprature);
      temprature = 0.0;
      samples = 0;
  }
  delay(50);
}