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.
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.
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.
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
LM335 with Arduino
Code – Read Temperature
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.
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.
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:
- Arduino board (read Best Arduino Started Kits)
- LM35 Temperature Sensor (or LM335, LM34)
- 2k Ohm resistor (if using an LM335)
- I2C OLED Display
- Jumper Wires
- Breadboard
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.
3. After installing the SSD1306 library from Adafruit, type “GFX” in the search box and install the library.
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.
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.
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:
- BME280 Sensor with Arduino (Pressure, Temperature, Humidity)
- DS18B20 Temperature Sensor with Arduino
- DHT11/DHT22 Humidity and Temperature Sensor With Arduino
- 0.96 inch I2C OLED Display with Arduino
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 »