Get accurate temperature from LM35 on Arduino - Correctly!

Submitted by Tim on 01 Oct 2013 - 11:12

After using some online examples I noticed that the temperature was always a little off with my weather station at home. Reading to some examples I came across two fixes: - divide the analogRead by 1023 not 1024. There are 1024 values but that includes 0. - Use analogReference(INTERNAL). This gives a higher accuracy. I've combined these two in the following example: Download example

float tempC;
float tempF;

int reading;
float referenceVoltage;

int tempPin = 0; //Analog pin connected to LM35

void setup()

 // Set Analog reference to 1.1V this gives more accuracy since the sensor will output 0-1 V
 // This only available on ATmega168 or ATmega328)
 // For more information see:
 referenceVoltage = 1.1; //Set to 5, 3.3, 2.56 or 1.1 depending on analogReference Setting

void loop()
  reading = 0;
  for(int i = 0; i < 10; i++) { // Average 10 readings for accurate reading
     reading += analogRead(tempPin); 

  // A lot of examples divide the sensor reading by 1024. This is incorrect and should be 1023. There are 1024 values including 0 so this should be 1023.
  tempC =  (referenceVoltage * reading * 10) / 1023; 
  // Convert to Fahrenheit
  tempF = (tempC * 9 / 5) + 32;
  Serial.print(tempC, 1); //Print one decimal, it's not accurate enough for two
  Serial.println(" C");
  Serial.print(tempF, 1); //Print one decimal, it's not accurate enough for two
  Serial.println(" F");
  Serial.println(" ");


user picture>
- 08 Feb 2014 - 12:40

Thanks Tim, This is the most accurate example I have seen so far. well done and thanks again.
user picture>
- 30 Jun 2014 - 17:15

Theres a type on line 5 :) Just an extra semi-colon also, I used this code on my arduino UNO. Im getting readings of 110C constantly. the temp isnt changing. Any idea?
user picture>

In reply to by Onca

- 14 Jul 2014 - 21:01

Did you connect the sensor correctly? I messed that one up once. The sensor will get extremely hot and you will see wrong values
user picture>
- 02 Dec 2014 - 03:27

Worked much better for me, thanks!!
user picture>
- 03 Jun 2015 - 10:59

Hi. I tried your code like the man said it doesn't work mate. I get a near close reading on the first pass and than 0.6 deg C than nothing. Help mate you've got something wrong.
user picture>
- 16 Jun 2015 - 21:13

What makes you think you need to divide by 1023? I'm not sure you understand how ADCs work. You divide by 1024 for the 10-bit ADC. "0" is still counted. Even the documentation for analogReference shows this.
user picture>

In reply to by MSBob

- 15 Jul 2015 - 16:26

I got this from different sources: It assumes 0 = 0% and 1023 = 100% so then I would divide it by 1023. Please let me know if I am wrong!
user picture>
- 22 Nov 2015 - 14:03

Hi there, I am new to Arduino. I connected the LM35 sensor and managed but the temperature readings taken every second are varying by a lot. Eg. 1st reading 25.90, 2nd reading (after 1 second) 24.32, ecc... The environment conditions are the same. Range of readings is in the range of 26-15 degrees C. Not sure if my sensor is a bust. I connected it properly and even varied the code a number of times to make sure I wasn't entering the wrong formula or values. ANy help is appreciated. Adrian
user picture>

In reply to by Adrian Busuttil

- 29 Dec 2015 - 18:50

LM35 is not that accurate. A deviation of +/- 1 Degree will be present.
user picture>
- 05 May 2016 - 22:44

Thank you very much Sir Tim Dejong , stay blessed this really helped me and it is super accurate

Add new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.