LM35 Temperature Sensor with ESP32 – Web Server

This guide contains discussion on how to interface LM35 temperature sensor with ESP32. LM35 is a very popular temperature sensor in academia and many students want to use it with ESP32.  But due to issue of ESP32 ADC, we can not interface LM35 directly with ESP32. In this guide,  I will show you how interface by avoiding this issue and how to send temperature sensor values on a web page by building a web server using ESP32.

Introduction to LM35 Temperature sensor

  • LM35 is used to measure temperature of any object or surrounding.
  • Sensor provides temperature range  -55°C to 150°C
  • Temperature sensor has  a accuracy of +/-0.4°C  at room temperature.
  • It gives output in voltage form proportional to temperature. Proportional factor is defined by scaling factor or resolution.
  • The scaling factor is .01V/°C. It mean for every 1°C, LM35 temperature sensor provides 100mV at the output.

Pinout of LM35

LM35 pinout

Table provides the details and pinout of this sensor.

LM35Functionality
VDDConnect power supply between 4-30 volt
VoutProvides output voltage proportional to temperature
GNDConnect with ground of power supply

Connection diagram of LM35 with ESP32

  • The scaling factor of LM35 is .01V/°C and the resolution of ESP32 ADC is 3.3V/4095 which is equal to 0.8mV. Therefore, ESP32 ADC should be able to read the 10mV output of the temperature sensor.
  • But, the built-in ADC of ESP32 can not measure temperature fluctuations of LM35 in fraction range or decimal range and so is the output voltage of the sensor.
  • We have to use an external ADC with higher resolution like ADS1115 I2C based external  ADC.
  • ADS1115 can measure 0.1875mV minimum voltage.  So the resolution of this external ADC is 5 times better than the scaling factor of LM35. We can use it to measure 10mV per degree centigrade.

You can check this External ADC ADS1115 interfacing with ESP32 to know more about this chip.

LM35 Web server with ESP32

Layout below shows the connection diagram of LM35 with ESP32 through external analog to digital converter ADS1115.

Connection diagram explained here:

GND pins : Connect ground pins of LM35, ADS1115 and ESP32 to a common reference point that is ground of ESP32. Also connect ADDR pin with ground.

VDD :Connect Vdd pin of LM35 and ADS1115 with 3.3 volts or Vin pin of ESP32.

I2C : Connect GPIO22 with SCL and GPIO21 with SDA pins of ADS1115 ADC respectively.

A0 :A0 is a analog channel zero of external adc. Connect output temperature sensor with analog channel zero.

Code to measure temperature with ESP32

Code used in this guide is similar to the one used in last tutorial on external adc interfacing with esp32 except temperature measurement part. Upload this code to ESP32 and check the output on serial monitor.

#include <Wire.h> // library for I2C communication 
#include <Adafruit_ADS1015.h> //Library of external adc

Adafruit_ADS1115 ads(0x48); // define the types of external adc 
float temp = 0.0; // variable to store temperature value

void setup(void) 
{
Serial.begin(9600); //define the baudrate of 9600
ads.begin(); // start adc to read sensor value over I2C 
}

void loop(void) 
{
int16_t adc0; // 16 bit interger to store output of analog channel zero

adc0 = ads.readADC_SingleEnded(0); // read ANO values
temp = (adc0 * 0.1875)/1000; // convert ADC value into voltage
temp = temp * 100; // converts voltage into temperature 10mv=1C

// this section displays value of adc and temperature on serial monitor
Serial.print("AIN0: "); 
Serial.print(adc0);
Serial.print("\tTempertaure: ");
Serial.println(temp); 
Serial.println();

delay(1000);
}

After uploading code, open serial monitor, you will see this output on serial monitor. Output contains adc0 value and temperature value.

LM35 output on serial monitor with ESP32

LM32 Web Server with ESP32 (Arduino IDE)

Now we will see how to create a Web server to display temperature sensor LM35 value on the web page.  This HTML files displays the value of temperature in Celsius and Fahrenheit on web page. Whenever a web client try to access a web page through an IP address, ESP32 will send this HTML page through HTTTP.

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<body>

<h1>WEB SERVER LM35 SENSOR</h1>
<p style="color:red">Temperature = ----*C</p>
<p style="color:red">Temperature in Farenhite = ----*F</p>

</body>
</html>
HTML page LM35 ESP32

This HTML Page provides this output. Instead of “—” , we will display temperature values.

Now adds this html code inside the above temperature measurement code and send html strings through client.println() function.

#include <WiFi.h> 
#include <Wire.h> // library for I2C communication 
#include <Adafruit_ADS1015.h> //Library of external adc

Adafruit_ADS1115 ads(0x48); // define the types of external adc 
float temp = 0.0; // variable to store temperature value 
const char* ssid = "PTCL-BB"; 
const char* password = "5387c614";

WiFiServer server(80);

String header;

void setup(void) 
{
Serial.begin(9600); //define the baudrate of 9600
ads.begin(); // start adc to read sensor value over I2C 
Serial.print("Connecting to Wifi Network");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Successfully connected to WiFi.");
Serial.println("IP address of ESP32 is : ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("Server started");
}

void loop(void) 
{
int16_t adc0; // 16 bit interger to store output of analog channel zero

adc0 = ads.readADC_SingleEnded(0); // read ANO values
temp = (adc0 * 0.1875)/1000; // convert ADC value into voltage
temp = temp * 100; // converts voltage into temperature 10mv=1C

// this section displays value of adc and temperature on serial monitor
Serial.print("AIN0: "); 
Serial.print(adc0);
Serial.print("\tTempertaure: ");
Serial.println(temp); 
Serial.println();
// This line checks if web client is available or not
WiFiClient client = server.available();
// if client is available 'if' condition becomes true
if (client) 
{ 
Serial.println("Web Client connected ");   //print on serial monitor
String request = client.readStringUntil('\r'); // wait untill http get request ends
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();

// This part send HTML page to web client 
// HTML page contains temperature values 
client.println("<!DOCTYPE html>\n");
client.println("<html>\n");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
client.println("<body>\n");
client.println("<h1>WEB SERVER LM35 SENSOR</h1>\n");
client.println("<p style=\"color:red\">Temperature =\"");
client.println("*C</p>\n");
client.println(temp);
client.println("<p style=\"color:red\">Temperature in Farenhite =\"");
client.println(1.8 * temp + 32);
client.println("*F</p>\n");
client.println("</body></html>"); 
client.println();
Serial.println("Client disconnected.");
Serial.println("");
}

}

This code displays the value of temperature sensor on the web page. This web page is accessible through IP address which is assigned to ESP32 by a WiFi network.  You can check this create ESP32 web server in Arduino IDE project for step by step guide on how to create a web page and send values to web page. Now upload this code to ESP32 and before uploading code change WiFi name and password with your own WiFI name and password in these lines.

const char* ssid = "PTCL-BB"; 
const char* password = "5387c614";
  • Now click on upload button to upload code and after you done with uploading, open serial monitor of Arduino IDE. You will find the IP address of ESP32.
  • Now go to any web browser and open the web page through the IP address. You will see the temperature values on web page as shown below: LM35 temperature sesnor esp32 web server

You can check other ESP32 Web server projects to know more about working of Web server part.

3 thoughts on “LM35 Temperature Sensor with ESP32 – Web Server”

  1. hi , thank you for your amazing page , now , I want run a analog fluxgate sensor with this way , it is flc100 sensor , a magnetic field sensor, please help me to run this sensor , pins are 0v,5v,sync,out-,out+, thank you very very much

    Reply
  2. You indicated that “The scaling factor of LM35 is .01V/°C. Therefore, ESP32 adc should be able to read 10mV minimum value.” If the ESP32 ADC is able to do this, why did you need the external ADC? You did not explain that choice.

    Reply
    • The scaling factor of LM35 is .01V/°C and the resolution of ESP32 ADC is 3.3V/4095 which is equal to 0.8mV. Therefore, ESP32 ADC should be able to read the 10mV output of the temperature sensor. But, the built-in ADC of ESP32 can not measure temperature fluctuations of LM35 in fraction range or decimal range and so is the output voltage of the sensor. We have to use an external ADC with higher resolution like ADS1115 I2C based external ADC.

      Reply

Leave a Comment