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.

LM35  Functionality
VDD Connect power supply between 4-30 volt
Vout Provides output voltage proportional to temperature
GND Connect with ground of power supply

Connection diagram of LM35 with ESP32

  • The scaling factor of LM35 is .01V/°C.  Therefore, ESP32 adc should be able to read 10mV minimum value.
  • But, built in adc of ESP32 can not measure this voltage correctly.
  • We have to use a 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 measure 10mV per degree centigrade.

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

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

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

Create Web server to display temperature value

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>

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

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.

Leave a Comment

shares