Using LM35 Temperature Sensor with Arduino

The LM35 is a simple and precise analog temperature sensor that outputs a voltage directly proportional to temperature in degrees Celsius. It’s ideal for measuring temperature accurately with a simple setup.

In this tutorial, you will learn how to connect a LM35 temperature sensor to an Arduino and read the temperature data precisely. You will also learn how to display the temperature data on an I2C OLED display.

LM35 Temperature Sensor

The LM35 is a linear temperature sensor that provides a voltage output directly proportional to the temperature in degrees Celsius. It operates on 4V to 30V, and draws less than 60 μA.

The LM35 is highly accurate and calibrated, offering ±0.5°C precision at room temperature (25°C) and ±0.75°C across its full -55°C to 150°C range. Also, it has low self-heating, only about 0.08°C in still air, which ensures that internal heat has no effect on the accuracy of temperature readings.

Ads
LM35 Temperature Sensor

Although the LM35 is rated for -55°C to 150°C, measuring negative temperatures requires a negative voltage bias. For example, we can supply +5V to Vcc and -5V (instead of 0V) to GND, and the sensor will output voltages from -0.55V to +1.5V.

To read negative temperatures with an Arduino, you would need a level-shifting circuit using an op-amp. As the circuit is slightly complex, we will only focus on the 0°C to 150°C range in this example.

If you want to measure negative temperature without any complex circuit, use TMP36, LM335, or DS18B20 instead.

LM35 Pinout

It comes with a TO-92 package and an 8-pin dip package. The TO-92 package is most popular because it can be easily attached to any surface and used to measure the temperature of that surface.

LM35 Pinout
PinDescriptions
+VsPower supply pin (4V–30V). Connect to Arduino 5V.
VoutOutputs analog voltage proportional to temperature. Connect to Arduino A0.
GNDGround pin. Connect to Arduino GND.

Arduino LM35 Circuit Diagram

Connecting the LM35 to an Arduino is very simple. Connect the VCC pin to the Arduino’s 5V pin and the GND pin to ground. Then, connect the Vout pin to any Arduino analog input pin; in this example, we’ll use A0.

Here’s how the circuit looks:

Arduino LM35 Circuit

How to Calculate Temperature

The LM35 outputs 10 mV (or 0.01 V) for every degree Celsius. We can read the voltage at the Vout pin using the Arduino analog input pin. And from there, we can calculate the actual temperature.

Ads

Arduino Uno has 10-bit ADCs, which means analogRead() converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023.

The general formula to convert the digital reading to voltage is:

\[V_{\text{out}} = \text{analogRead value} \times \left( \frac{\text{ADC reference voltage}}{2^{\text{[ADC resolution]}}} \right)\]

The Arduino Uno R3 board operates on +5V, and the resolution of the ADC pins is 10 bits.

\[V_{\text{out}} = \text{analogRead value} \times \left( \frac{5}{1024} \right)\]

The Arduino Uno R4 boards support 14-bit ADC.

\[V_{\text{out}} = \text{analogRead value} \times \left( \frac{5}{16,384} \right)\]

And for a 3.3V Arduino board with 10-bit ADC, we would do.

\[V_{\text{out}} = \text{analogRead value} \times \left( \frac{3.3}{1024} \right)\]

You can now easily read the Vout pins’ voltage on different Arduino boards and divide the voltage by 0.01 to obtain the temperature data in degrees Celsius.

Arduino Code for LM35 Temperature Sensor

You have learned how to read the voltage output from the Vout pin and get the temperature in degrees Celsius. Now, we will implement it using the Arduino code.

Arduino Code – View the Temperature in the Serial Monitor

// The LM35's Vout pin is connected to
const int LM35_PIN A0;

void setup() {
  // begin serial communication at 9600 baud rate
  pinMode(LM35_PIN, INPUT);
  Serial.begin(9600);
}

void loop() {
  // reading voltage from the LM35 Vout
  int val = analogRead(LM35_PIN);

  // use 3.3 for 3.3v arduino
  float voltage = val * (5.0 / 1024.0);

  float tempC = voltage * 100;

  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print("°C  |  ");

  float tempF = (tempC * 9.0 / 5.0) + 32.0;
  Serial.print(tempF);
  Serial.println("°F");

  delay(1000);
}

Explaining the code

Here, you can see that we started by defining the Arduino pin connected to the sensor’s output pin. This pin must be an analog input pin, and we will use pin A0 for that.

const int LM35_PIN A0;

In the setup section, we declare this pin as an input pin.

pinMode(LM35_PIN, INPUT);

We initialize the serial communication at a 9600 baud rate, as we use the serial monitor to view the data.

Serial.begin(9600);

Now, coming to the loop section, we first read the analog pin value and store it in an integer variable.

int val = analogRead(LM35_PIN);

Then we calculate the voltage.

float voltage = val * (5.0 / 1024.0);

and multiply it by 100 to get the temperature in degrees Celsius.

float tempC = voltage * 100;

Arduino Code with analogReference()

The default ADC step is 5/1024 = 4.88 mV. With analogReference(1.1V), the step size becomes 1.07 mV, meaning each step can detect smaller voltage changes.

LM35 gives 10 mV/°C (0–1.5 V for 0–150 °C). So 1.1 V ref covers 0–110 °C with ~0.1 °C resolution, much more accurate than the default 5 V ref.

Ads

Thus, using Arduino’s internal voltage reference improves the accuracy of LM35 temperature readings.

// Define the analog pin, the LM35's Vout pin is connected to
const int LM35_PIN A0;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Use 1.1 V reference for ADC measurements
  analogReference(INTERNAL);
}

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

  // Convert that voltage into voltage
  float voltage = val * 1.1 / 1024.0;

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

  // Print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print("°C  |  ");

  // Print the temperature in Fahrenheit
  float tempF = (tempC * 9.0 / 5.0) + 32.0;
  Serial.print(tempF);
  Serial.println("°F");

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

LM35 with OLED Display

You have learned how to display the temperature on a serial monitor. Now, in this section, we will learn how to display the temperature on an OLED display.

If you are new to OLEDs, read the article below.

Circuit diagram

To add an I2C OLED to your existing circuit, connect its SCL pin to Arduino pin A5 (SCL) and its SDA pin to Arduino pin A4 (SDA). Then connect the OLED’s VCC and GND pins to the Arduino’s 5V and GND, respectively.

Arduino with LM35 OLED Circuit

Arduino Code

The following code uses Adafruit’s SSD1306 and GFX libraries, so make sure to install both libraries before compiling the code.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED Display Settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int LM35_PIN A0;  // LM35 VOUT connected to A0

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

  analogReference(INTERNAL);
  analogReadResolution(14);

  // Initialize OLED
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);  // Stop here if OLED fails
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print("Initializing...");
  display.display();
  delay(2000);
}

void loop() {
  int val = analogRead(LM35_PIN);
  float voltage = val * 1.1 / 16384.0;
  float tempC = voltage * 100.0;
  float tempF = (tempC * 9.0 / 5.0) + 32.0;

  Serial.print("ADC Value: ");
  Serial.print(val);
  Serial.print("\tVoltage: ");
  Serial.print(voltage, 4);
  Serial.print(" V\tTemp C: ");
  Serial.print(tempC, 2);
  Serial.print(" °C\tTemp F: ");
  Serial.println(tempF, 2);

  // Display on OLED
  display.clearDisplay();
  display.setCursor(0, 0);
  display.setTextSize(1);
  display.print("Temp (C): ");
  display.println(tempC, 2);
  display.print("Temp (F): ");
  display.println(tempF, 2);
  display.display();

  delay(1000);
}

Summary

In this tutorial, you learned how to interface the LM35 temperature sensor with Arduino and display the readings on an OLED screen. We covered everything from understanding the sensor’s working principle to wiring, coding, and visualizing real-time temperature data.

Now that you can monitor temperature digitally, you can extend this project by adding data logging, wireless monitoring, or integrating it with a weather station setup.

If you enjoyed this tutorial, subscribe to our newsletter to get the latest Arduino and electronics tutorials delivered straight to your inbox!

Help me to Build more Projects for You!

At CircuitGeeks, we're passionate about creating exciting electronics projects and sharing our knowledge with the world. Our projects are free and open to everyone, but we would need your support to keep the creativity flowing!

If you enjoy our work and find our projects valuable, please consider supporting us on Buymeacoffee. By buying us a coffee, you help us buy more components and keep our projects going strong.

We truly appreciate your contribution!

Leave a Comment