ESP32 Temperature Sensor LM35 Interfacing (in Arduino IDE)

Previous Tutorial Previous Tutorial Tutorial 10 Next Tutorial Next Tutorial
ESP32 Temperature Sensor LM35 Interfacing (Arduino)
ESP32 Course Home Page ????

 

In this tutorial, you’ll learn how to use ESP32 (or ESP8266) with the LM35 Temperature Sensor in Arduino IDE using ADC analog input pins. We’ll discuss how the LM35 temperature sensor works, how to connect it with ESP32, and how to get readings using ADC in Arduino IDE.

We’ll be doing 3 different LABs in this tutorial. To show the ESP32 LM35 Temperature readings on the serial port, I2C LCD, and also to get multiple temperature sensors readings at the same time. Without further ado, let’s get right into it!

In this tutorial: 3 LABs

LAB22 ESP32 with LM35 Temperature Sensor – Serial Monitor & Processing
LAB23 ESP32 with LM35 Temperature Sensor – I2C LCD
LAB24 ESP32 with Multiple LM35 Temperature Sensors

ESP32 Temperature Sensor Arduino LM35 With I2C LCD - ADC Calibration

[toc]


Requirements For This Tutorial

Prior Knowledge

Software Tools

Hardware Components

You can either get the complete course kit for this series of tutorials using the link down below. Or just refer to the table for the exact components to be used in practical LABs for only this specific tutorial.


LM35 Temperature Sensor

LM35-Temperature-Sensor

The LM35 is a temperature sensor widely used in electronic projects and midrange devices. It has limited usage in industrial applications due to maximum temperature range limitations. It’s rated to a full range of −55°C to 150°C.

You can just power it up and instantly read the voltage level on the output terminal. The VOUT of the sensor directly maps to the sensor’s temperature as we’ll see hereafter.

Technical Features of LM35 Temperature Sensor

  • Linear + 10-mV/°C Scale Factor
  • 0.5°C Ensured Accuracy (at 25°C)
  • Rated for Full −55°C to 150°C Range
  • Operates From 4 V to 30 V
  • Less Than 60-μA Current Drain
  • Non-Linearity Only ±¼°C Typical

LM35 Pinout

LM35 Temperature Sensor Pinout - ESP32 Arduino Tutorial

LM35 V-T Characteristics & Configurations

As stated in the LM35 temperature sensor datasheet, the accuracy specifications of the LM35 are given with respect to a simple linear transfer function:

VOUT = (10 mv / °C) × T

where VOUT is the LM35 output voltage & T is the temperature in °C. And this is what we’ll be using in code in order to convert the ADC voltage readings to temperature values (in °C or °F).

There are two possible configurations for the LM35 temperature sensor as described in the datasheet. They’re shown in the diagram down below. The first of which is the Basic Configuration (with a range of 2 °C to 150 °C).

The other configuration for LM35 is called the “Full-Range” which gives you a range for measurements between (-55 °C up to 150 °C). It also requires an additional external resistor and a negative supply voltage as indicated in the diagram below.

LM35 Temperature Sensor Operating Ranges

 


ESP32 LM35 Temperature Sensor Interfacing (Arduino)

 

In this section, we’ll see how to make the ESP32 to LM35 connections, and how to use the ESP32 ADC to take measurements of the temperature sensor in Arduino IDE.

ESP32 LM35 Temperature Sensor Connection

The connection between ESP32 & LM35 temperature sensor should be as follows. Note that you can use any other GPIO pin that has an analog input channel capability.

LM35 Temperature Sensor ESP32 DevKit v1 Board
VOUT  GPIO35 
VCC   Vin  
GND   GND  

Using ESP32 ADC With LM35 Sensor (in Arduino)

Note that: I’ll be using the ESP32 ADC Calibration routine (By Espressif implemented in Arduino Core) to get temperature readings as accurate as possible. This topic was discussed in more detail in the ESP32 ADC Tutorial if you want to check it out.

Here are the exact steps you need to follow in order to read an LM35 temperature sensor with ESP32 boards (using ADC calibrated) + a line-by-line code explanation.

Step1– Include the Arduino Core ADC Calibration Library Header File

#include “esp_adc_cal.h”

Step2– Define the LM35 sensor GPIO pin to be used as an ADC analog input channel

#define LM35_Sensor1 35

Step3– Now, create 4 variables: an int to hold the raw ADC reading, 3 x float to hold the final temperature measurement (in °C  and/or °F), and a temporary voltage variable as we’ll need it in the calculations.

int LM35_Raw_Sensor1 = 0;

float LM35_TempC_Sensor1 = 0.0;

float LM35_TempF_Sensor1 = 0.0;

float Voltage = 0.0;

Step4– Add this ADC Read & Calibration Function to your code

Step5– Now, in the main loop function, you can read the ADC, get the calibrated voltage vlaue, and convert it to temperature (in °C and/or °F).

Note that: ADC Noise and readings fluctuation should be minimal after applying the calibration technique shown in the code above. However, you can still improve this after eliminating ADC noise by using a simple digital filter (like the moving average)  to get temperature readings as accurate as possible. This topic was also discussed in detail in the ESP32 ADC Tutorial if you want to check it out.

Components For This Tutorial’s LABs

 

QTY. Component Name Buy Links
1 ESP32 Devkit v1 DOIT Board

or Any Other ESP32 Dev Board

Amazon.com  –  eBay.com  –  Banggood.com
2 BreadBoard Amazon.com –  eBay.com –  Banggood.com
1 Resistors Kit Amazon.com / Amazon.com  –  eBay.com  –  Banggood.com
1 Jumper Wires Pack Amazon.comAmazon.com –  eBay.comeBay.com –  Banggood.com
1 LEDs Kit Amazon.comAmazon.com –  eBay.com –  Banggood.com
1 Micro USB Cable Amazon.com  –  eBay.com –  Banggood.com
2 I2C LCD Module Amazon.com  Amazon.com –  eBay.com –  Banggood.com
2 LM35 Temperature Sensors Amazon.com –  eBay.com –  Banggood.com

*Affiliate Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network (EPN) and Amazon.com, Banggood.com. This may be one of the ways to support this free platform while getting your regular electronic parts orders as usual at no extra cost to you.


ESP32 Temperature Sensor LM35 – Serial Print

LAB Number 22
LAB Name ESP32 Temperature Sensor LM35 – Serial Print + Processing Gauge Example
  • Read the temperature using ADC calibrated routine
  • Print the readings over the UART serial port
  • Keep repeating…

ESP32 Temperature Sensor LM35 – Arduino Code Example

The Full code Listing

Choose the board, COM port, hold down the BOOT button, click upload and keep your finger on the BOOT button pressed. When the Arduino IDE starts sending the code, you can release the button and wait for the flashing process to be completed. Now, the ESP32 is flashed with the new firmware.

Hardware Connections

ESP32 Temperature Sensor LM35 Arduino ADC Code Calibration

Here is The Result

ESP32 Temperature Sensor LM35 Arduino IDE - ADC Calibration Serial Print

ESP32 LM35 Temperature Sensor PC Station (With Processing)

This is the same example, but we’ll do a little bit of modification in the ESP32 Arduino code. And also you’ll need to have installed Processing on your machine. Then, copy the processing code provided down below in a new sketch and run it directly to see the GUI temperature meters window.

ESP32 Code

replace the loop() function in the previous (ESP32 Arduino Code example) by this one shown below.

Processing Code

Replace the COM port name in the code down below with your COM port name for your ESP32 board. That could be easily found in the device manager if you’re using windows.

Here is The Result

ESP32 Temperature Sensor LM35 Processing Gauge Example PC Station

 


ESP32 Temperature Sensor LM35 – I2C LCD

LAB Number 23
LAB Name ESP32 Temperature Sensor LM35 + I2C LCD Display
  • Include the libraries
  • Define an object of LiquidCrystal_I2C, set its parameters, and initialize the LCD
  • Read the ADC calibrated value & convert it to temperature
  • Print the temperature to the I2C LCD display
  • Keep repeating…

ESP32 Temperature Sensor LM35 + I2C LCD – Arduino Code

The Full code Listing

Choose the board, COM port, hold down the BOOT button, click upload and keep your finger on the BOOT button pressed. When the Arduino IDE starts sending the code, you can release the button and wait for the flashing process to be completed. Now, the ESP32 is flashed with the new firmware.

The connection between ESP32 & I2C LCD module should be as follows.

PCF8574 I2C LCD Module ESP32 DevKit v1 Board
SCL  GPIO22 
SDA  GPIO21 
Vcc   Vin  
GND   GND  

Here is The Result

ESP32 Temperature Sensor LM35 I2C LCD Arduino Example Code

 


ESP32 Multiple Temperature Sensors LM35

LAB Number 24
LAB Name ESP32 Multiple Temperature Sensors LM35 + I2C LCD
  • Include the libraries
  • Define an object of LiquidCrystal_I2C, set its parameters, and initialize the LCD
  • Read the 2ch ADC calibrated value & convert it to temperature (for LM35 sensor 1 & 2)
  • Print the temperatures to the I2C LCD display
  • Keep repeating…

ESP32 Multiple Temperature Sensors LM35 + I2C LCD

This LAB example is very similar to the previous one. Except for the fact that here we’ll be using 2 LM35 sensors connected to 2 different analog input channels. So, we’ll take the measurement for the sensors one by one, and then we’ll print the readings to the I2C LCD as usual.

The Full code Listing

Choose the board, COM port, hold down the BOOT button, click upload and keep your finger on the BOOT button pressed. When the Arduino IDE starts sending the code, you can release the button and wait for the flashing process to be completed. Now, the ESP32 is flashed with the new firmware.

Here is The Result

ESP32 Multiple Temperature Sensors Arduino LM35 With I2C LCD

 


ESP32 Temperature Sensor LM35 Applications

 

Temperature sensors like LM35 can be used in so many applications as we’ll see in future tutorials. I’ll keep updating this series of tutorials by adding more applications and techniques that may help you in your projects. Drop me a comment if you’ve got any questions or suggestions, I’ll be glad to help!

Related Tutorials Based On ESP32 Temperature Sensors

Learn More About LM35 Sensor

 

You can also check the ESP32 Course Home Page ????  for more ESP32 tutorials divided into sections based on categories. This may be helpful for you in case of searching for a specific tutorial or application.


Did you find this helpful? If yes, please consider supporting this work and sharing these tutorials!

 

Stay tuned for the upcoming tutorials and don’t forget to SHARE these tutorials. And consider SUPPORTING this work to keep publishing free content just like this!

 

 

ESP32 Course Home Page ???? 
Previous Tutorial Previous Tutorial Tutorial 10 Next Tutorial Next Tutorial
Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment