LM35 Temperature sensor values

I'm using the following code to read the temperature values.

//initializes/defines the output pin of the LM35 temperature sensor
int outputpin= A0;
//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
}
 
//main loop
void loop()
{
int rawvoltage= analogRead(outputpin);
float millivolts= (rawvoltage/1024.0) * 5000;
float celsius= millivolts/10;
Serial.print(celsius);
Serial.print(" degrees Celsius, ");
 
Serial.print((celsius * 9)/5 + 32);
Serial.println(" degrees Fahrenheit");
delay(1000);
 
}

This is the serial monitor

12:34:17.498 -> 12.21 degrees Celsius, 53.97 degrees Fahrenheit
12:34:18.492 -> 38.09 degrees Celsius, 100.55 degrees Fahrenheit
12:34:19.480 -> 39.55 degrees Celsius, 103.19 degrees Fahrenheit
12:34:20.500 -> 39.06 degrees Celsius, 102.31 degrees Fahrenheit
12:34:21.504 -> 36.62 degrees Celsius, 97.92 degrees Fahrenheit
12:34:22.497 -> 35.16 degrees Celsius, 95.28 degrees Fahrenheit
12:34:23.492 -> 34.18 degrees Celsius, 93.52 degrees Fahrenheit
12:34:24.478 -> 20.51 degrees Celsius, 68.91 degrees Fahrenheit
12:34:25.507 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:26.506 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:27.490 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:28.510 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:29.500 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:30.489 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:31.510 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:32.497 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:33.488 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:34.512 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:35.494 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:36.512 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:37.497 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:38.506 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:39.522 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:40.512 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:41.496 -> 3.91 degrees Celsius, 39.03 degrees Fahrenheit
12:34:42.523 -> 23.93 degrees Celsius, 75.07 degrees Fahrenheit
12:34:43.506 -> 40.04 degrees Celsius, 104.07 degrees Fahrenheit
12:34:44.498 -> 40.04 degrees Celsius, 104.07 degrees Fahrenheit
12:34:45.525 -> 39.06 degrees Celsius, 102.31 degrees Fahrenheit
12:34:46.520 -> 37.11 degrees Celsius, 98.80 degrees Fahrenheit
12:34:47.518 -> 35.64 degrees Celsius, 96.16 degrees Fahrenheit
12:34:48.507 -> 34.18 degrees Celsius, 93.52 degrees Fahrenheit
12:34:49.530 -> 23.44 degrees Celsius, 74.19 degrees Fahrenheit
12:34:50.537 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:51.530 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:52.517 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:53.523 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:54.522 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:55.527 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:56.518 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:57.540 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:58.533 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:34:59.514 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:35:00.520 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:35:01.518 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:35:02.534 -> 0.00 degrees Celsius, 32.00 degrees Fahrenheit
12:35:03.537 -> 0.98 degrees Celsius, 33.76 degrees Fahrenheit
12:35:04.523 -> 13.67 degrees Celsius, 56.61 degrees Fahrenheit
12:35:05.541 -> 40.53 degrees Celsius, 104.95 degrees Fahrenheit

What could be the problem?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Here is my circuit, made in Tinkercad. Could change the colours

Hi,
Ops image.


Thanks, its not actually a circuit diagram but it will do.

Where did you purchase the LM35?

How are you powering your project?

Thanks.. Tom... :slight_smile:

The LM35 is bought in a starterkit, maybe a cheap Chinese, don’t remember.

Power is via a adapter, output 9V/1A

I’d guess that you have a wiring issue or the sensor is faulty - I don’t see an issue with the code.

Easiest solution to figure it out is probably to get a few more of them or switch to using DS18B20.

What is the ambient temperature?

The picture shows the LM35 power supply pins connected backward.
LM35pinout.png
This has worked for me:
Connect a 0.1µF capacitor between input pin and GND, connect a 2.2k resistor between input pin and GND.
Try this program, works for me on UNO, Nano:

/*
 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);
  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");
}

LM35pinout.png

JCA34F:
The picture shows the LM35 power supply pins connected backward.

This has worked for me:
Connect a 0.1µF capacitor between input pin and GND, connect a 2.2k resistor between input pin and GND.

Good catch, to late a night and too much blood in the caffeine stream. :slight_smile: :slight_smile: :slight_smile:

Join the club Tommy. :wink: