LM35 temp sensor reading 0 degrees

Hi, I am creating a temp sensor with an LM35 and LCD but the LCD is only reading 0 Celcius and 32 Farenheit. I havent found any problems in the code or connections but there probably is a problem. Can anyone find anything?

code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int value;
float celsius;
const int inPin = 0; // The analog pin of the LM35

void setup() {
lcd.begin(16, 2);
}

void loop() {
value = analogRead(inPin);
celsius = (value / 1023) * 500; // Getting the temperature from the sensor

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(celsius);
lcd.print(“C”);
lcd.setCursor(0, 1);
lcd.print((celsius * 9) / 5 + 32);
lcd.print(“F”);
delay(1000);
}

You have an integer division truncation issue. Try:

celsius = (value / 1023.0) * 500; // Getting the temperature from the sensor
const int inPin = 0; // The analog pin of the LM35

At least on the picture it seems that the LM35 is connected to A5 and not A0.

Yes! that was it inpin = 0 i changed it to 5 and now it is reading temperature however it says that its 185 degrees Fahrenheit. Does anyone know why this would be? i have made no changes to the circuit or the code besides inpin = 5 now.

Measure the voltage on the pin with a multimeter to see if its the code or the sensor that are at fault. You haven't given a schematic, and that photo isn't super clear. I think your sensor is top right on the breadboad and its ground wire goes off out of shot somewhere?

yes the sensor is in the top right. I am pretty sure the power and ground for it are set up correctly. Do you know about how much voltage it should be reading?

Do you know about how much voltage it should be reading?

To correspond with your sketch output it should be about 0.8V.

If you have a standard LM35 your calculation should be:

celsius = -55.0 + (value / 1023.0) * 500.0; // Getting the temperature from the sensor

That would show you about 30°C or about 86°F. Is that more or less correct?

yes thank you, that worked it is now reading about 82 Fahrenheit which is a lot closer to what the actual temperature is. its about 77 degrees in this room around the arduino and everything. Is there an explanation for this small change?

its about 77 degrees in this room around the arduino and everything. Is there an explanation for this small change?

That's almost 3°C difference. But what told you the value of 77°F? It depends on the planned use area of the setup if a read error like this is acceptable. I use my temperature sensors to control my heating, an error of 3°C is way outside of acceptable. I have about +/- 0.2°C.

On the other side: The Arduino seems to be powered by USB, so an error of about 10% is to be expected as PCs almost never provide exactly 5V on USB.

pylon: On the other side: The Arduino seems to be powered by USB, so an error of about 10% is to be expected as PCs almost never provide exactly 5V on USB.

Indeed, that's another problem. These sensors are best read using the internal reference (1.1V but this again has a 10% tolerance so has to be calibrated).

pylon:
On the other side: The Arduino seems to be powered by USB, so an error of about 10% is to be expected as PCs almost never provide exactly 5V on USB.

According to data sheets, the LM35 sensor could be operated at any stable voltage in the range: 4V to 30V. The sensor output is directly calibrated to degree Celsius, and it generates 10 mV output for 10C change in the surrounding temperature.

pylon:

celsius = -55.0 + (value / 1023.0) * 500.0; // Getting the temperature from the sensor

Assuming VREF = 5V, the response of the LM35 sensor is:

T0C = 100*(5/1023)*ananlogRead(A5); //

This is the first time, I have seen -55 in the response equation of LM35 sensor.

According to data sheets, the LM35 sensor could be operated at any stable voltage in the range: 4V to 30V. The sensor output is directly calibrated to degree Celsius, and it generates 10 mV output for 10C change in the surrounding temperature.

You didn't get the point, my remark was targeting the measurement in the Arduino which has a 10% tolerance depending on the USB voltage (which almost never 5V).

Assuming VREF = 5V, the response of the LM35 sensor is:

T0C = 100*(5/1023)*ananlogRead(A5); //

This is the first time, I have seen -55 in the response equation of LM35 sensor.

The standard LM35 has a measurement range of -55 to 150°C. The equation above is for a LM35D which is specified to be used here.

So should I change the equation in the code?

deep12154: So should I change the equation in the code?

Of course.

hmm after changing the code to 100*(5/1023)*analogRead(A5) it is back to reading 0 degrees Celsius

deep12154:
hmm after changing the code to 100*(5/1023)*analogRead(A5) it is back to reading 0 degrees Celsius

1. Remove the LM35 from the breadboard. Place Pin-1 of LM35 directly on 5V socket of UNO; Pin-3 directly on GND-pin of UNO. Place one-pin of jumper wire on A5-pin of UNO. Cut other end of the jumper; remove about 1/2" of insulation and then wrap the bare wire strands on Pin-2 of LM35. This is to eliminate doubtful breadboard and jumper wires.

2. Upload the following sketch (tested on NANO+LM35).

void setup() 
{
  Serial.begin(9600);
  analogReference(INTERNAL);
}

void loop() 
{
  int tempSensor = analogRead(A5);
  float airTemp = (float)100*(1.1/1024)*tempSensor;
  Serial.print(airTemp, 2);
  Serial.println(" C");
  delay(1000);
}

sm15.png

deep12154: hmm after changing the code to 100*(5/1023)*analogRead(A5) it is back to reading 0 degrees Celsius

Not surprizing as 5/1023 = 0

Perhaps you mean 5.0/1023?

change this to:

100*(5.0/1023)*analogRead(A5)

This forces floating point instead of integer (in integers, 5/1024 = 0).

hmm when i changed the code to that it began reading 180 degrees F again.

Try this test sketch:

/*
 LM35 thermometer, no floats, no delays
   http://www.ti.com/lit/gpn/lm35
*/
const byte sampleBin = 8, // number of samples for smoothing
           aInPin = A0;
const int calValue = 0; // adjust for calibration 
const int kAref = 1100, // analog ref voltage * 1000
                        // measured with accurate DMM
          kSampleBin = sampleBin * 1000,
          tEnd = 5000; // update time in mS
int tempC,
    tempF;
uint32_t total,  // sum of samples
         tStart; // timer start

void setup()
{
  Serial.begin(9600); // set serial monitor baud rate to match
  analogReference(INTERNAL); // use 1.1V internal ref
  analogRead(aInPin);
  for(int i = 0;i < sampleBin;i++) // for smoothing, fill total
    total += analogRead(aInPin);   // with sampleBin * current
                                   // reading
}
void loop()
{
  if(millis() - tStart > tEnd)
  {
    tStart = millis(); // reset timer 
    total -= (total / sampleBin); // make room for new reading
    total += analogRead(aInPin); // add new reading
    tempC = total * kAref / kSampleBin + calValue;
    tempF = (tempC * 18 + 3200) / 10;
    Serial.print(analogRead(aInPin));
    Serial.print("\t");
    Serial.print(total); // sum of samples
    Serial.print("\t");
    prntTemp(tempC);
    prntTemp(tempF);
    Serial.println();
  }
}
void prntTemp(int temp){
  Serial.print(temp / 10); // whole degrees
  Serial.print(".");
  Serial.print(temp % 10); // tenths
  Serial.print("\t");
}