One of the easiest and inexpensive ways to add temperature sensing in your Arduino project is to use LM35 Temperature Sensor. These sensors are fairly precise and needs no external components to work. So, with just a few connections and some Arduino code you’ll be sensing temperature in no time!

LM35 Temperature Sensor

The LM35 is a low voltage, precision centigrade temperature sensor manufactured by Texas Instruments. It is a chip that provides a voltage output that is linearly proportional to the temperature in °C and is, therefore, very easy to use with an Arduino.

lm35 temperature sensor

The LM35 temperature sensor is fairly precise, never wears out, works under many environmental conditions and requires no external components to work. In addition, the LM35 sensor does not require calibration and provides a typical accuracy of ±0.5°C at room temperature and ±1°C over a full −55°C to +155°C temperature range.

The sensor can be powered with a 4V to 30V power supply and consumes less than 60µA during active temperature conversions, providing very low self-heating (less than 0.08°C in still air).

Here are the complete specifications:

Power supply4V to 30V
Current draw60µA
Temperature range−55°C to +155°C
Accuracy±0.5°C
Output scale factor10mV/°C
Output at 25°C250mV

For more information, please refer below datasheet.

The only disadvantage of the LM35 sensor is that it requires a negative bias voltage to measure negative temperature. So if you are planning to use the sensor to measure negative temperature, it is recommended that you use TMP36 temperature sensor. The TMP36 by Analog Devices is fairly accurate (-40°C to 125°C) and has the advantage of being able to measure negative temperatures without the need for negative bias voltage. You can find a dedicated tutorial for the TMP36 below.

A better alternative to the LM35 is to use a digital temperature sensor like the DS18B20 which comes in the same package. Digital temperature sensors have better noise immunity which is useful when the sensor is placed at a distance or in an electrically noisy environment.

Working Principle

The LM35 uses a solid-state technique to measure the temperature. It makes use of the fact that the voltage drop between the base and emitter (forward voltage – Vbe) of the Diode-connected transistor decreases at a known rate as the temperature increases. By precisely amplifying this voltage change, it is easy to generate an analog signal that is directly proportional to temperature.

relationship between forward voltage and temperature

This linear relationship between forward voltage and temperature is the reason why diode-connected transistors are used as temperature measurement devices. Essentially this is how temperature is measured, although there have been some improvements in this technique over the years. More information about this technique can be found here.

The good news is that all these complex calculations are done inside the LM35. It just outputs a voltage that is linearly proportional to temperature.

How to Measure Temperature

The LM35 is easy to use; just connect the left pin to power (4V to 30V) and the right pin to ground (assuming the flat side of the sensor is facing you). Then the middle pin will have an analog voltage that is directly proportional (linear) to the temperature in °C. This can be easily seen in the output voltage vs temperature characteristic. Note that the analog output voltage is independent of the power supply.

lm35 temperature sensor output relationship curve

To convert the voltage to temperature, simply use the basic formula:

Temperature (°C) = Vout * 100

For example, if the voltage out is 0.5V that means that the temperature is 0.5 * 100 = 50 °C

Testing the LM35 Sensor

Testing the LM35 is pretty easy, just connect the left pin to 4V to 30V power supply (Four AA batteries work great) and the right pin to ground (assuming the flat side of the sensor is facing you). Now connect your multimeter in DC voltage mode to ground and the middle pin. At the room temperature (25°C), the voltage should be about 0.25V.

Try squeezing the plastic case of the sensor gently to see a rise in temperature.

try squeezing lm35 to see rise in temperature

Or try touching the sensor with an ice cube (in a plastic bag so your circuit doesn’t come into contact with water) and watch the temperature drop.

try touching lm35 with ice to watch temperature drop

LM35 Sensor Pinout

The LM35 comes in three different form factors, but the most common type is the 3-pin TO-92 package, which looks just like a transistor. Let’s take a look at its pinout.

lm35 temperature sensor pinout

+Vs is the power supply for the sensor which can be anywhere between 4V to 30V.

Vout pin produces an analog voltage that is directly proportional (linear) to the temperature. It should be connected to an Analog (ADC) input.

GND is a ground pin.

Connecting the LM35 Temperature Sensor to an Arduino

Hooking up the LM35 to an Arduino is super simple. You only need to connect three pins: two for power and one for reading the sensor value.

The sensor can be powered from 5V. The positive voltage connects to ‘+Vs’ and ground connects to ‘GND‘. The middle pin ‘Vout’ is the analog signal output from the sensor and connects to the A0 analog input of an Arduino.

Below is the hookup for the experiments with the LM35:

wiring lm35 temperature sensor to arduino

To measure air temperature leave the sensor in the open air or attach it to an object you want to measure the temperature of, such as a heat sink.

Reading the Analog Temperature Data

As you can see in the wiring diagram above, the output of the LM35 is connected to one of the analog inputs of the Arduino. The value of this analog input can be read with the analogRead() function.

However, the analogRead() function does not actually return the output voltage of the sensor. Instead it maps the input voltage between 0 and the ADC reference voltage (technically it is the operating voltage i.e. 5V or 3.3V unless you change it) to 10-bit integer values ​​ranging from 0 to 1023. To convert this value back to the sensor’s output voltage, use this formula:

Vout = (reading from ADC) * (5 / 1024)

This formula converts the number 0-1023 from the ADC into 0-5V

Then, to convert volts into temperature, use this formula:

Temperature (°C) = Vout * 100

Arduino Code – Simple Thermometer

The following sketch shows a quick way to read LM35 temperature sensor and can serve as the basis for more practical experiments and projects. It simply reads the value from the LM35 using analog port A0 and prints the current temperature (in both °C and °F) on the serial monitor. Go ahead and upload it to your Arduino.

// Define the analog pin, the LM35's Vout pin is connected to
#define sensorPin A0

void setup() {
  // Begin serial communication at 9600 baud rate
  Serial.begin(9600);
}

void loop() {
  // Get the voltage reading from the LM35
  int reading = analogRead(sensorPin);

  // Convert that reading into voltage
  float voltage = reading * (5.0 / 1024.0);

  // Convert the voltage into the temperature in Celsius
  float temperatureC = voltage * 100;

  // Print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.print("\xC2\xB0"); // shows degree symbol
  Serial.print("C  |  ");
  
  // Print the temperature in Fahrenheit
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  Serial.print(temperatureF);
  Serial.print("\xC2\xB0"); // shows degree symbol
  Serial.println("F");

  delay(1000); // wait a second between readings
}

You should see the following output in the serial monitor.

lm35 arduino output

Code Explanation:

The sketch starts by defining the Arduino pin to which the sensor’s Vout pin is connected.

#define sensorPin A0

In the setup, we initialize the serial connection with the computer.

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

In the loop, we first read in the analog signal from the LM35 using the analogRead() function.

int reading = analogRead(sensorPin);

Next, we will use the formulas we discussed earlier in the article to convert the analog reading into voltage and then into temperature.

float voltage = reading * (5.0 / 1024.0);

float temperatureC = voltage * 100;

Next, the results are printed on the Serial Monitor.

Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.print("C  |  ");

The temperature value we get is in Celsius (°C). It is converted in to Fahrenheit (°F) using a simple formula and printed on the Serial Monitor.

T(°F) = T(°C) × 9/5 + 32

float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.println("F");

Arduino Project – Standalone Thermometer with LM35 and an I2C LCD

Sometimes you come up with an idea where you want to display the temperature readings in real time and show an alert when the temperature is outside the specified range. Then you’ll probably need a 16×2 character LCD instead of a serial monitor.

In this example, we’ll hook the I2C LCD up to the Arduino along with the LM35.

Connecting the I2C LCD is quite easy as you can see in the wiring diagram below. If you’re not familiar with an I2C LCDs, consider reading (at least skimming) below tutorial.

The following diagram shows you how to wire everything.

wiring lm35 temperature sensor to arduino and i2c lcd

The following sketch will print the temperature values on the I2C LCD. The code is similar to the previous example, except that the values are printed on the I2C LCD.

// Include the LiquidCrystal_I2C library
#include <LiquidCrystal_I2C.h>

// Create a new instance of the LiquidCrystal_I2C class
LiquidCrystal_I2C lcd(0x3F, 16, 2);

// Define a custom degree character
byte Degree[] = {
  B00111,
  B00101,
  B00111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};

// Define the analog pin, the LM35's Vout pin is connected to
#define sensorPin A0

void setup() {
  // Start the LCD and turn on the backlight
  lcd.init();
  lcd.backlight();

  // Create a custom character
  lcd.createChar(0, Degree);
}

void loop() {
  // Get the voltage reading from the LM35
  int reading = analogRead(sensorPin);

  // Convert that reading into voltage
  // Replace 5.0 with 3.3, if you are using a 3.3V Arduino
  float voltage = reading * (5.0 / 1024.0);

  // Convert the voltage into the temperature in Celsius
  float temperatureC = voltage * 100;

  // Print the temperature on the LCD;
  lcd.setCursor(0, 0);
  lcd.print("Temperature:");
  lcd.setCursor(0, 1);
  lcd.print(temperatureC, 1);
  lcd.write(0); // print the custom degree character
  lcd.print("C ");
  
  // Print the temperature in Fahrenheit
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  lcd.print(temperatureF, 1);
  lcd.write(0); // print the custom degree character
  lcd.print("F ");

  delay(1000); // wait a second between readings
}

You should see the following output on the LCD:

lm35 sensor output on i2c lcd   copy

Login
ADS CODE