connecting an arduino with a LM35 temperature sensor – special issues

So this is a very easy one – most of the time. There are already many blog posts and forum posts about connecting a LM 35 temperature sensor to an arduino like here, here or here. So why writing another one? When beginning with my LM35 I thought the same and this is why I decided to take this special sensor. But things don’t always work as easily as it looks from a distance. There are some details I found and this is why I write this post.

The basics

LM35 is a linear temperature sensor with the output voltage calibrated in centigrade celsius. 1 degree celsius makes an output voltage of 10mV. So when you have room temperature at 22°C the LM35 gives you a voltage of 220mV = 0.22V. The sensor has only 3 pins: Vcc which can be between +4V and+20V, GND and Vout. So you connect Vcc to +5V on your arduino board and GND to GND and you have the sensor powered up. Then connect Vout to one of the analog inputs of the arduino and you are done (in many cases – see below). No additional hardware needed!

All the schematics in this post are taken from the national semiconductor data sheet of the LM35 that you can find here. When you connect the LM35 as shown on the left the lowest temperature you can measure is +2°C. The LM35 can also measure temperatures down to -55°C but you will need some additional components for that as shown in the data sheet.

The software side is easy: read the analog input scale the value and put it out the way you want!

Improving accuracy

You can improve accuracy of the analog to digital conversion by changing the reference voltage. The arduino reads 10 bits which means 1024 steps. The reference voltage gives the maximum of the input range. By default the reference voltage is set to +5V. You can change to an internal reference that gives 1.1V. Then 1.1V is your maximum input voltage. This fits very well to the output voltage of the sensor and so you can measure from +2°C to +110°C. You don’t need this in terms of resolution but the 1.1V reference is more precise. And it is always better to use the full input range of an AD converter in terms of noise reduction. The code for reading the temperature every second and putting it out to the serial monitor is quite short and simple:

// the pin where the analog data is read
const int analogPin = 0;
// the variable for the data
float sensorValue = 0;

void setup()
{
  // setting the reference voltage
  // to internal 1.1V
  analogReference(INTERNAL);
  // starting serial
  Serial.begin(9600);
}

void loop()
  {
  // read the data and
  // scale it from 10 bit to 0.01 / degree C
  sensorValue = float(analogRead(analogPin)) * 110 / 1024;
  // output data to serial monitor
  Serial.print("temp: ");
  // print only one decimal place
  Serial.print(sensorValue, 1);
  Serial.println("degrees C");
  // wait one second then start again
  delay(1000);
}

Preventing swing on the output of the sensor

When you don’t want the sensor to be directly beneath the arduino board you take a cable to connect from the sensors location to your arduino. The cable should have a shield to keep away all the high frequency electric noise you get from computers mobile phones and other electronic stuff. Connect the shield to GND on the arduino side of the cable. Not (!) on the sensors side. This is the normal way to do it and you should do this the same way. Now here a new problem can occur that is not even documented in the data sheet (although you find the solution there). Whats happening is that the LM35 can begin to swing because of the capacitive load that comes from the cable (especially from the shield). This happened to me although the cable was only around 1m long. When you connect the input pin of your arduino to a scope you can see something like in the screenshot you see on the screenshot (scope at 10μs/cm and 50mV/cm). I measured a swing frequency around 50kHz with an amplitude of 170mV. So this will make serious problems. The temperature value that your arduino reads goes up and down around 10 degrees all the time. This also indicates you a swing. The solution is a resistor in series with a capacitor between Vout and GND directly at the sensors pins. The data sheet proposes a resistor of 75Ω and a capacitor of 1μF. You can also take a 68Ω resistor which is more common. The value is not critical. Check the polarity when you take an electrolytic capacitor. If you still have problems you can try the additional bypass capacitor. In my case there was no need of. The swing problem was the reason I wrote this post because it took me a little while to find out what it is all about.

So this is what I think you need to get good results out of your LM35. Happy measuring!

heliosoph

About these ads
This entry was posted in arduino and tagged , , . Bookmark the permalink.

5 Responses to connecting an arduino with a LM35 temperature sensor – special issues

  1. kamaar says:

    Hi,
    I have run this code exactly but it just showed on screen 109.9 C all the times. I do not know what’s has gone wrong with my Duino board. I am very grateful to get your comments. Thanks.

    • heliosoph says:

      Hi,

      first of all I would check if the hardware is wired correctly and running. Take a voltmeter and measure the output voltage of the LM35 (betwwen Vout and GND). So you see if the sensor is working correctly. Then measure the input voltage at that input pin of the arduino that is read by the script. This has to be the same voltage :-) When you have a scope check if the input voltage has a swing as described in the post. When everything is ok then your hardware is wired up correctly. Please report what you found out. Then we can look further.

      Best regards

      heliosoph

  2. Dan Tabarez says:

    amazing, it works perfect…. im using PIC16f877a without delay… thanks

  3. Philip Løventoft says:

    Hi
    I did not have the exact parts so I ended up running it with a 100 uF capacitor and a 4k ohm resistor and still seems to work fine. I only have the problem when I am measuring often on one of the other analog ports strangely enough – if I just run a program measuring the output of the port with the sensor itself it works fine. Anyway, thank you so much for helping out with this frustrating problem :)

    • heliosoph says:

      Hi Philip,

      thank you for your friendly reply. :-)

      When you read the analog inputs without having anything connected to then you get some random numbers depending on the noise in your system. A simple solution to this is pull-down resistors from the inputs to GND. A good guess would be 10k resistors. This way you get low-impedance inputs. This is how you can avoid noise problems. When you connect a sensor to one of these inputs and you don’t remove the resistor the sensor must be able to drive the resistor as additional load.

      Very best regards

      heliosoph

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s