Guide for LM35, LM335 and LM34 Temperature Sensors with Arduino

This guide shows how to use the LM35 (LM35DZ), LM335 and LM34 temperature sensors with the Arduino board. We’ll show you how to wire the sensors and write the code to get temperature readings. Lastly, we’ll build a project example to display the sensor readings on an OLED display.

LM35 LM35DZ LM34 LM335 Arduino board OLED temperature Celsius Fahrenheit readings

Introducing LM35, LM335 and LM34 Temperature Sensors

The LM35, LM335 and LM34 are linear temperature sensors that output a voltage proportional to the temperature value.

Temperature SensorOutput Voltage Linearity
LM35 (LM35DZ)proportional to temperature in Celsius (ºC)10mV/ºC
LM335proportional to temperature in Kelvin (ºK)10mV/ºK
LM34proportional to temperature in Fahrenheit (ºF)10mV/ºF

These sensors work in a similar way, but are calibrated differently to output a voltage proportional to the different temperature units.

The LM35 outputs 10 mV per degrees Celsius rise in temperature. In a similar way, the LM335 outputs 10 mV per degrees Kelvin rise in temperature and the LM34 outputs 10 mV per degrees Fahrenheit rise in temperature.

For example, if the LM35 outputs a voltage of 345 mV, that means we have a temperature value of 34.5ºC.

For more information about these sensors, you can consult their datasheet:

Where to Buy?

You can go to Maker Advisor to find the Temperature sensors’ best price at different stores:

LM35 (LM35DZ) Pinout

The LM35 has only three pins, VCC, Vout and GND.

LM35 LM35DZ Pinout. Pins: VCC, Vout and GND

Here’s the connections you need to make between the LM35 and the Arduino:

LM35 / LM34Arduino
VCC5V
VoutAny Analog Pin
GNDGND

Note: if you’re using an LM34 temperature sensor, the pinout is the same of the LM35.

LM335 Pinout

The pinout of the LM335 temperature sensor is slightly different.

LM335 LM335Z Pinout. Pins: VCC, Vout and GND
LM335Arduino
AdjDon’t connect
VoutAny Analog Pin (pull up with 2k Ohm resistor)
GNDGND

The adj pin can be used to calibrate the sensor and obtain more accurate temperature readings. We won’t use that pin in this tutorial, so it should be left unconnected.

Schematic Diagram

You should follow the same schematic diagram whether you’re using an LM35 or LM34 temperature sensor. You should follow a slightly different diagram if you’re using the LM335.

LM35 and LM34 with Arduino

LM35 / LM35DZ and LM34 Wiring to Arduino Schematic Diagram

LM335 with Arduino

LM335 and LM34 Wiring to Arduino Schematic Diagram

Code – Read Temperature

LM35 LM35DZ LM34 LM335 Arduino board OLED wiring schematic

The following code reads the temperature from the LM35 sensor and displays the readings in the Serial Monitor. This code is also compatible with LM335 and LM34 – you just need to uncomment some lines in the code to use the right sensor.

/*
 * Rui Santos
 * Complete Project Details https://RandomNerdTutorials.com
 */
 
const int sensorPin = A0; 
float sensorValue;
float voltageOut;

float temperatureC;
float temperatureF;

// uncomment if using LM335
//float temperatureK;

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  voltageOut = (sensorValue * 5000) / 1024;
  
  // calculate temperature for LM35 (LM35DZ)
  temperatureC = voltageOut / 10;
  temperatureF = (temperatureC * 1.8) + 32;

  // calculate temperature for LM335
  //temperatureK = voltageOut / 10;
  //temperatureC = temperatureK - 273;
  //temperatureF = (temperatureC * 1.8) + 32;

  // calculate temperature for LM34
  //temperatureF = voltageOut / 10;
  //temperatureC = (temperatureF - 32.0)*(5.0/9.0);

  Serial.print("Temperature(ºC): ");
  Serial.print(temperatureC);
  Serial.print("  Temperature(ºF): ");
  Serial.print(temperatureF);
  Serial.print("  Voltage(mV): ");
  Serial.println(voltageOut);
  delay(1000);
}

View raw code

How the Code Works

You start by defining the pin that is connected to the sensor output. It must be an analog pin. We’re using pin A0, but you can use any other analog pin.

const int sensorPin = A0;

Define a variable that will hold the analog value read from the sensor:

float sensorValue;

The voltageOut variable will store the actual voltage output value coming from the sensor.

float voltageOut;

Then, create variables that will store the temperature value. Here, we create a temperatureC and a temperatureF variables to hold the temperature in Celsius and Fahrenheit, respectively.

float temperatureC;
float temperatureF;

If you’re using the LM335 sensor, you also need a variable to hold the temperature in Kelvin. So, if you’re using that sensor you need to uncomment the following line:

//float temperatureK;

In the setup(), declare the sensorPin as an input:

pinMode(sensorPin, INPUT);

Initialize a serial communication at a baud rate of 9600. You need to initialize the serial communication so that you can display the readings on the Serial Monitor:

Serial.begin(9600);

In the loop(), read the value coming from you sensor and save it in voltageOut variable. To read an analog value with Arduino you just need to use analogRead() function and pass the pin you want to read as an argument.

voltageOut = analogRead(sensorPin);

As mentioned previously, these sensors output a voltage value that is proportional to the temperature.

The analog values read from the Arduino may have a value between 0 and 1024, in which 0 corresponds to 0V and 1024 to 5V. So, we can easily get the output voltage of the sensor in mV.

voltageOut = (sensorValue * 5000) / 1024

In case of the LM35 sensor, we’ve seen that 10mV corresponds to a Celsius degree rise in temperature. So, the temperature in Celsius corresponds to the voltage read from the sensor in mV divided by 10mV.

temperatureC = voltageOut / 10;

To get the temperature in Fahrenheit, we just need to use the Celsius -> Fahrenheit conversion:

temperatureF = (temperatureC * 1.8) + 32;

If you’re using an LM335 or an LM34, you use the same calculations to get the temperature. You just need to keep in mind that the LM335 returns the temperature in Kelvin degrees and the LM34 in Fahrenheit degrees. Then, you can convert the values to other units if needed.

Finally, print the sensors readings to the Serial Monitor both in Celsius and Fahrenheit degrees.

Serial.print("Temperature(ºC): ");
Serial.print(temperatureC);
Serial.print("  Temperature(ºF): ");
Serial.print(temperatureF);

For debugging purposes, we also print the voltage.

Serial.print("  Voltage(mV): ");
Serial.println(voltageOut);

The loop() is repeated every second.

delay(1000);

Demonstration

Upload the code to your Arduino IDE. Don’t forget to select the right board and COM port in the Tools menu.

After that, open the Serial Monitor at a baud rate of 9600. You should get new temperature readings every second. You can cover the sensor with your finger to see the temperature values increasing.

LM35/LM35DZ, LM335 and LM34 Temperature Sensors with Arduino board. Print serial readings in Arduino IDE Serial Monitor

Temperature Readings in OLED Display

In this section we’ll show you how to display your sensor readings in an OLED display.

For an in-depth tutorial on how to use the OLED display with the Arduino, follow the next guide:

Parts Required

For this project, you’ll need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic Diagram

Wire all the components as shown in the next schematic diagram:

Arduino LM35/LM35DZ OLED Display Schematic Diagram

Installing OLED Libraries

To control the OLED display you need the adafruit_SSD1306.h and the adafruit_GFX.h libraries. Follow the next instructions to install those libraries.

1. Open your Arduino IDE and go to Sketch Include Library > Manage Libraries. The Library Manager should open.

2. Type “SSD1306” in the search box and install the SSD1306 library from Adafruit.

Installing SSD1306 OLED Library  Arduino UNO board

3. After installing the SSD1306 library from Adafruit, type “GFX” in the search box and install the library.

Installing GFX Library Arduino UNO board

4. After installing the libraries, restart your Arduino IDE.

Code – Display Readings on OLED

After wiring the circuit and installing the required libraries, upload the following code to your Arduino board.

/*
 * Rui Santos
 * Complete Project Details https://RandomNerdTutorials.com
 */

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const int sensorPin = A0; 
float sensorValue;
float voltageOut;

float temperatureC;
float temperatureF;

// uncomment if using LM335
//float temperatureK;

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  voltageOut = (sensorValue * 5000) / 1024;
  
  // calculate temperature for LM35 (LM35DZ)
  temperatureC = voltageOut / 10;
  temperatureF = (temperatureC * 1.8) + 32;

  // calculate temperature for LM335
  //temperatureK = voltageOut / 10;
  //temperatureC = temperatureK - 273;
  //temperatureF = (temperatureC * 1.8) + 32;

  // calculate temperature for LM34
  //temperatureF = voltageOut / 10;
  //temperatureC = (temperatureF - 32.0)*(5.0/9.0);

  Serial.print("Temperature(ºC): ");
  Serial.print(temperatureC);
  Serial.print("  Temperature(ºF): ");
  Serial.print(temperatureF);

  // clear display
  display.clearDisplay();

  // display temperature Celsius
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print(temperatureC);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");

  // display temperature Fahrenheit
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(temperatureF);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("F");

  display.display();    
  delay(1000);
 }

View raw code

Demonstration

Congratulations! You’ve completed the project. Now, you can check the sensor readings in the OLED display. New temperature readings are displayed every second.

LM35 LM35DZ LM34 LM335 Arduino board OLED display temperature readings

Wrapping Up

The LM35, LM335 and LM34 are linear temperature sensors that output a voltage proportional to the temperature value. They can be powered by the Arduino 5V pin, and to read the voltage, you use an analog pin.

Reading the voltage from the sensor is as easy as using the analogRead() function on the sensor pin. Then, you just need to make a simple calculation to get the temperature in your desired unit.

We hope you’ve found this guide useful. We have other guides about Arduino sensors and modules that you may like:

If you like Arduino, you’ll also like our Arduino Course: Arduino Step-by-step Projects

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

11 thoughts on “Guide for LM35, LM335 and LM34 Temperature Sensors with Arduino”

  1. Much appreciated !
    I am still waiting on the LM35DZ amongst other items
    This can be a real show stopper as some shipments get lost and or take as long as 90 days to arrive.

    Reply
    • Hi Chris.
      Yes, that’s right.
      Sometimes parts from China take a long time to get to its destination. But, they are also very cheap. So, we have to wait.
      Regards,
      Sara

      Reply
  2. Great Tutorial! Thanks a lot.
    For best results, I recommend measuring the AREF pin on Arduino with voltmeter. And change the value on 5000.
    voltageOut = (sensorValue * 5000) / 1024
    For example AREF = 4.87V
    voltageOut = (sensorValue * 4870) / 1024

    Reply
  3. Hi,
    First thanks for this good tutorial 👏
    I am trying the code but I get a very high temperature, not sure why.
    I am using ESP32 board and my connections look like this:cloud.elalemanyo.de/tmp/Bildschirmfoto%202021-03-24%20um%2017.50.00-w2xMiwdjlx.png

    The code is the same as the one used here. But I am getting this output:
    Temperature(ºC): 45.66 Voltage(mV): 456.56
    Temperature(ºC): 45.66 Voltage(mV): 456.56
    Temperature(ºC): 45.66 Voltage(mV): 456.56
    Temperature(ºC): 45.66 Voltage(mV): 456.56
    Temperature(ºC): 45.66 Voltage(mV): 456.56

    I am living in Germany so no way is this correct 😊

    I am doing something wrong? I use LM35 DZ

    Thanks

    Reply
  4. Hi there!

    Great tutorial! it really helped me with my project. However I still need a little push to have it fully completed.

    I’m using a LM335Z sensor to activate a DC motor when it reaches a certain temperature. The sensor gives the correct temperature readings out. And these values increase or decrease correctly following the temperature changes.

    When the temperature readings go above the temperature limit I´ve previously set the DC motor turns on. But just at the same exact moment the temperature readings (voltage) increases drastically and stay constant so I could never cool down the sensor so the DC motor turns off when the temperature goes under the limit temperature I´ve mentioned before.

    I’m struggling with this issue and can’t really figure out what’s going on.
    I am a beginner using ARDUINO and I’d really appreciate some help.

    Thanks a lot!
    Julen

    Reply
  5. I’m a little confused concerning your calculations. For example, you have Voltage calculating by taking the PWM value from the ADC times 5000, then divide by 1024.

    So if the ADC says that the value is 1024, then (1024 * 5000) / 1024 will be 5000 volts. Which if you then divide that by 10, the temperature comes out as 500 degrees C … is it possible that the code you published is not the exact same code that you used to get the values you published?

    Reply
  6. Hi how can I use esp32 with lm35 sensor to read both negative and positive temperature values.
    for example reading temperature values below zero, instead of giving me 0 degrees, when temperature is below that.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.