The LM35 is a popular temperature sensor known for its accuracy, and low cost. It provides a linear voltage output directly proportional to temperature and doesn't require any external calibration or signal conditioning, making it ideal for Arduino projects.
In this tutorial, you'll learn how to interface the LM35 sensor with an Arduino UNO. We'll build a simple temperature monitoring system that measures the temperature and displays it in both Celsius and Fahrenheit. The temperature readings will be shown in real-time and shown on a 16×2 LCD display.
LM35 Temperature Sensor Overview
The LM35 is a precision analog temperature sensor that provides a voltage output linearly proportional to temperature in degrees Celsius (°C). One of the biggest advantages of the LM35 is its 10 mV/°C linear output, making it easy to interface with ADCs or microcontrollers like the Arduino.
LM35 Sensor Specifications
- Operating Voltage: 4V to 30V
- Temperature Range: -55°C to +150°C
- Current Consumption: 60 μA (typical)
- Output Type: Analog
- Accuracy: ±0.5°C (typical)
- Output Impedance: 0.1Ω for 1 mA load
- Linearity: ±0.25°C
- Sensitivity: 10 mV/°C
LM35 Temperature Sensor Pinout
VCC: Power supply pin of the LM35.
GND: Ground pin.
Out: Analog output pin of the sensor.
Interfacing LM35 Temperature Sensor with an Arduino
Component Required
- Arduino UNO R3
- LM35 Temperature Sensor
- RGB LED (Common Cathode)
- Resistor (220Ω)
- Breadboard
- Jumper Wires
- USB Type A to B Cable
- 12V Adapter
- PCF8574 I2C LCD Module
Wiring Connections
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 more checkout: Interfacing LM35 Temperature Sensor with Arduino
Top comments (0)