LM35 Temperature Sensor With Arduino

by Rachana Jain in Circuits > Arduino

16 Views, 0 Favorites, 0 Comments

LM35 Temperature Sensor With Arduino

Untitled design (5).png

The LM35 temperature sensor is a popular and cost-effective choice for measuring temperature in electronic projects. Known for its simplicity and accuracy, the LM35 provides a linear analog voltage output directly proportional to the temperature in Celsius. It requires no external calibration or signal conditioning, making it an ideal component for Arduino-based applications.

In this tutorial, you’ll learn how to interface the LM35 sensor with an Arduino UNO. We’ll build a real-time temperature monitoring system that measures and displays temperature in both Celsius and Fahrenheit. The readings will be shown on a 16×2 LCD display.

Supplies

Arduino UNO R3

LM35 Temperature Sensor

Resistance

Breadboard

RGB LED

Jumper Wires

USB Cable Type A to B

12V Supply Adapter

LM35 Temperature Sensor

Slide3.PNG

The LM35 is a precision analog temperature sensor that delivers a linear voltage output directly proportional to the ambient temperature in degrees Celsius (°C). It operates over a wide voltage range of 4V to 30V and consumes a very low current of 60 μA, minimizing self-heating and ensuring accurate temperature readings.

One of the key advantages of the LM35 is its 10 mV/°C linear output, which simplifies temperature-to-voltage conversion. This makes it easy to interface with analog-to-digital converters (ADCs) or microcontrollers like the Arduino, without the need for external calibration or signal conditioning. However, when used with 8-bit ADCs such as the ADC0808 or ADC0804, an amplifier may be required for precise 1°C resolution due to the limited ADC resolution.

Despite its benefits, the LM35 has some limitations:

  1. It cannot directly measure negative temperatures without a dual-polarity power supply. For applications requiring negative temperature sensing, the TMP36 is a more suitable alternative.
  2. As an analog sensor, it is susceptible to electrical noise, which can degrade accuracy, especially in long cable runs or electrically noisy environments. In such cases, digital sensors like the DS18B20 offer better reliability.

Specifications of LM35 Temperature Sensor

  1. Operating Voltage: 4V to 30V
  2. Temperature Range: -55°C to 150°C
  3. Current Consumption: 60 μA (typical)
  4. Output Type: Analog
  5. Accuracy: ±0.5°C (typical)
  6. Output Impedance: 0.1 Ω for 1 mA load
  7. Linearity: ±0.25°C
  8. Sensitivity: 10 mV/°C


LM35 Temperature Sensor Pinout

The LM35 has three pins:

VCC (Power Supply): This is the power supply pin of the LM35.

GND: This is the ground pin.

Out: This is the analog output pin of the sensor.

Interfacing LM35 Temperature Sensor With an Arduino

Wiring LM35 Temperature Sensor with Arduino UNO.png

Let’s begin by connecting the LM35 temperature sensor to an Arduino UNO and display the temperature readings in both Celsius and Fahrenheit on an I2C 16×2 LCD display. Additionally, we’ll use a common cathode RGB LED to visually indicate the temperature range:

  1. Red for high temperature
  2. Green for normal temperature
  3. Blue for low temperature

Sensor and RGB LED Connections

In the wiring setup:

  1. The Analog output pin of the LM35 is connected to analog pin A0 of the Arduino.
  2. The VCC pin of the sensor is connected to 5V, and the GND pin is connected to GND of the Arduino.
  3. A 10 nF capacitor is placed between the output and GND pins of the LM35 to help stabilize the sensor's analog signal and minimize noise.

The common cathode RGB LED is wired as follows:

  1. Red anode → Arduino pin 10
  2. Green anode → Arduino pin 8
  3. Blue anode → Arduino pin 9
  4. CathodeGND via a current-limiting resistor

This configuration allows the LED to change colors based on the current temperature.

I2C LCD Connections

We’re using a PCF8574-based I2C module attached to a 16×2 LCD to display the temperature readings.

  1. The SDA (data) pin of the LCD is connected to Arduino A4
  2. The SCL (clock) pin is connected to Arduino A5
  3. The VCC and GND pins of the LCD module are connected to the 5V and GND pins of the Arduino, respectively
Note: Since the I2C communication uses A4 (SDA) and A5 (SCL) pins, these should not be used as regular analog inputs when the I2C LCD is connected.

I2C Address Configuration

Make sure that the A0, A1, and A2 address jumpers on the I2C module are not shorted. This configuration sets the I2C address of the LCD to 0x27, which matches the address used in the Arduino code.

If all three address jumpers (A0, A1, A2) are shorted, the I2C address changes to 0x20, and the code would need to be updated accordingly.

Arduino Code


/*
Interfacing temperature Sensor with Arduino UNO using Analog input pin of Arduino and
display temperature in Fahrenheit and in degree Celsius on I2C LCD. Here RGB LED turns Blue at LOW temperature, Red at HIGH temperature and Green when the temperature is between LOW and HIGH limit.
by www.playwithcircuit.com
*/
#include <LiquidCrystal_I2C.h> // Library to Run I2C LCD
#define RED_PIN 10
#define BLUE_PIN 9
#define GREEN_PIN 8
#define LOW_TEMP 10
#define HIGH_TEMP 50
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the analog pin for the temperature sensor
const int TemperatureSensorPin = A0;
// Variable to store the Analog count from temperature sensor
int temperatureCounts;
// Variable to store Voltage values
float voltageValue;
// Variable to store Temperature in Degree Celsius
float temperatureDegreeCelsius;
// Variable to store Temperature in Fahrenheit
float temperatureFahrenheit;
void setup() {
// initialize the lcd
lcd.init();
// Turn on the Backlight
lcd.backlight();
// Clear the display buffer
lcd.clear();
// Make LED pins and Buzzer pin as output
pinMode(RED_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
// Turn off all the pins
digitalWrite(RED_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
// Print a message to the LCD
lcd.setCursor(0, 0);
lcd.print("Initializing");
// Print a message to the LCD
lcd.setCursor(0, 1);
lcd.print("Please Wait...");
// flush out the first hundred values give time to temperature sensor to be stable
for (int i = 0; i < 100; i++) {
// Read the value from the temperature sensor
temperatureCounts = analogRead(TemperatureSensorPin);
delay(10);
}
// Clear the display buffer
lcd.clear();
// Print a message to the LCD
lcd.setCursor(0, 0);
lcd.print("Temp in C:");
// Print a message to the LCD
lcd.setCursor(0, 1);
lcd.print("Temp in F:");
}
void loop() {
// Static variables to save the last temperature
static float lastTemp = 0xFF;
// Read the value from the temperature sensor
temperatureCounts = analogRead(TemperatureSensorPin);
// convert the counts 0 to 1023 into voltage values 0 to 5V
voltageValue = (5.0/1023) * temperatureCounts;
// Convert voltage to temperature in Celsius
temperatureDegreeCelsius = voltageValue * 100;
// Convert Celsius to Fahrenheit
temperatureFahrenheit = (temperatureDegreeCelsius * 9.0 / 5.0) + 32.0;
// If current temperature is not equal to last temperature value in degree celsius
if (lastTemp !
= temperatureDegreeCelsius) {
// Print a temp to the LCD
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print((int)temperatureDegreeCelsius);
// Print a message to the LCD
lcd.setCursor(10, 1);
lcd.print(" ");
lcd.setCursor(10, 1);
lcd.print((int)temperatureFahrenheit);
}
// Save the current temperature value in the static variable to use it later
lastTemp = temperatureDegreeCelsius;
// Change the color of LED as per temperature level
if (temperatureDegreeCelsius > HIGH_TEMP) {
digitalWrite(RED_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
} else if (temperatureDegreeCelsius >= LOW_TEMP && temperatureDegreeCelsius <= HIGH_TEMP) {
digitalWrite(RED_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
} else if (temperatureDegreeCelsius < LOW_TEMP) {
digitalWrite(RED_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
}
// Wait for 1000 ms before the next loop
delay(1000);
}

To learn code description in detail checkout: Interfacing LM35 Temperature Sensor with Arduino