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 readingshttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/LM35-LM35DZ-LM34-LM335-Arduino-board-OLED-Temperature.jpg?resize=300%2C169&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/LM35-LM35DZ-LM34-LM335-Arduino-board-OLED-Temperature.jpg?resize=768%2C432&ssl=1 768w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/LM35-LM35DZ-LM34-LM335-Arduino-board-OLED-Temperature.jpg?resize=1024%2C576&ssl=1 1024w" sizes="(max-width: 813px) 100vw, 813px" data-recalc-dims="1" class="extendsBeyondTextColumn" style="max-width: none; margin: 0.5em auto; display: block; height: auto; width: 786.09375px; margin-inline-start: -70px;">

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.

     
     
     
     

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 GNDhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/LM35-Temperature-Sensor-Pinout.png?resize=234%2C300&ssl=1 234w" sizes="(max-width: 363px) 100vw, 363px" width="363" height="465" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

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

   
   
   
   

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 GNDhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/LM335Z-Temperature-Sensor-Pinout.png?resize=236%2C300&ssl=1 236w" sizes="(max-width: 364px) 100vw, 364px" width="364" height="463" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
   
   
   
   

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 Diagramhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/Arduino-LM35-Schematic-Diagram.png?resize=289%2C300&ssl=1 289w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/Arduino-LM35-Schematic-Diagram.png?resize=768%2C798&ssl=1 768w" sizes="(max-width: 684px) 100vw, 684px" class="extendsBeyondTextColumn" style="max-width: none; margin: 0.5em auto; display: block; height: auto; width: 684px; margin-inline-start: -18.953125px;">

LM335 with Arduino

LM335 and LM34 Wiring to Arduino Schematic Diagramhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/Arduino-LM335-Schematic-Diagram.png?resize=254%2C300&ssl=1 254w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/Arduino-LM335-Schematic-Diagram.png?resize=768%2C909&ssl=1 768w" sizes="(max-width: 813px) 100vw, 813px" data-recalc-dims="1" class="extendsBeyondTextColumn" style="max-width: none; margin: 0.5em auto; display: block; height: auto; width: 786.09375px; margin-inline-start: -70px;">

Code – Read Temperature

LM35 LM35DZ LM34 LM335 Arduino board OLED wiring schematichttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/LM35-LM35DZ-LM34-LM335-Arduino-board-OLED-wiring-schematic.jpg?resize=300%2C168&ssl=1 300w" sizes="(max-width: 750px) 100vw, 750px" data-recalc-dims="1" class="extendsBeyondTextColumn" style="max-width: none; margin: 0.5em auto; display: block; height: auto; width: 750px; margin-inline-start: -51.953125px;">

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.

 

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.

 

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

 

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

 

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.

 

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:

 

In the setup(), declare the sensorPin as an 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:

 

In the loop(), read the value coming from you sensor and save it in voltageOutvariable. 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.

 

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.

 

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.

 

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

 

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.

 

For debugging purposes, we also print the voltage.

 

The loop() is repeated every second.

 

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 Monitorhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/LM35-Temperature-and-Voltage-Readings-on-Serial-Monitor.png?resize=300%2C188&ssl=1 300w" sizes="(max-width: 711px) 100vw, 711px" width="699" height="437" class="extendsBeyondTextColumn" style="max-width: none; margin: 0.5em auto; display: block; height: auto; width: 699px; margin-inline-start: -26.453125px;">

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:

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 boardhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/05/install-ssd1306-adafruit-library.png?resize=300%2C169&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/05/install-ssd1306-adafruit-library.png?resize=768%2C432&ssl=1 768w" sizes="(max-width: 788px) 100vw, 788px" width="699" height="393" class="extendsBeyondTextColumn" style="max-width: none; margin: 0.5em auto; display: block; height: auto; width: 699px; margin-inline-start: -26.453125px;">

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

Installing GFX Library Arduino UNO boardhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/05/install-gfx-library-adafruit.png?resize=300%2C169&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/05/install-gfx-library-adafruit.png?resize=768%2C433&ssl=1 768w" sizes="(max-width: 786px) 100vw, 786px" data-recalc-dims="1" class="extendsBeyondTextColumn" style="max-width: none; margin: 0.5em auto; display: block; height: auto; width: 786px; margin-inline-start: -69.953125px;">

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.

 

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 readingshttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/07/LM35-LM35DZ-LM34-LM335-Arduino-board-OLED-display-temperature-readings.jpg?resize=300%2C168&ssl=1 300w" sizes="(max-width: 750px) 100vw, 750px" data-recalc-dims="1" class="extendsBeyondTextColumn" style="max-width: none; margin: 0.5em auto; display: block; height: auto; width: 750px; margin-inline-start: -51.953125px;">

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 program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »