Experiment-9
Interfacing LM35 Temperature Sensor with ESP32 Web Server for Real-Time
Monitoring
Aim
To interface the LM35 temperature sensor with the ESP32 microcontroller and develop a web server
to display real-time temperature readings in a user-friendly web interface.
Apparatus Required
1. ESP32 Development Board
2. LM35 Temperature Sensor
3. Breadboard
4. Jumper Wires
5. USB Cable for Programming
6. Computer with Arduino IDE
Theory
The LM35 is a high-accuracy temperature sensor that generates an output voltage directly
proportional to the surrounding temperature in Celsius. Its linear calibration outputs 10 mV per °C,
providing precise readings without additional adjustments.
The ESP32 is a versatile microcontroller with built-in Wi-Fi capabilities, ideal for IoT applications. By
interfacing the LM35 with the ESP32, the analog temperature readings can be digitized and
processed for real-time visualization on a web server hosted by the ESP32. This combination
demonstrates the seamless integration of hardware and software for IoT-based temperature
monitoring.
Key Concepts
1. LM35 Sensor Output: Analog signal proportional to temperature (e.g., 0°C → 0V, 100°C →
1V).
2. ESP32 ADC: Converts the LM35's analog output to digital signals for processing.
3. Web Server: A lightweight HTML-based web interface hosted on ESP32 to display sensor
readings dynamically.
4. #include <WiFi.h>
5. #include <WebServer.h>
6. #include "esp_adc_cal.h"
7. #include "html.h"
8.
9. WebServer server(80);
10.
11. #define LM35_SENSOR_PIN 33
12.
13. // Function prototype for reading and calibrating the ADC value
14. uint32_t readAndCalibrateADC(int rawADCValue);
15.
16. // Variables to store raw ADC value, voltage in millivolts, and temperatures in
Celsius and Fahrenheit
17. int rawADCValue = 0;
18. float voltageInMilliVolts = 0.0;
19. float temperatureInCelsius = 0.0;
20. float temperatureInFahrenheit = 0.0;
21.
22. // Replace these with your WiFi network settings
23. const char* ssid = "ssid of your lab";
24. const char* password = "password of wifi";
25.
26. void MainPage()
27. {
28. String _html_page = html_page; /*Read The HTML Page*/
29. server.send(200, "text/html", _html_page); /*Send the code to the web server*/
30. }
31.
32. void Temp()
33. {
34. // Prepare a JSON string that includes both Celsius and Fahrenheit temperatures
35. String json = "{";
36. json += "\"celsius\": " + String(temperatureInCelsius, 2) + ",";
37. json += "\"fahrenheit\": " + String(temperatureInFahrenheit, 2);
38. json += "}";
39. server.send(200, "application/json", json); //Send updated temperature values to
the web server
40. }
41.
42.
43. void setup(void)
44. {
45. Serial.begin(115200); /*Set the baudrate to 115200*/
46. WiFi.mode(WIFI_STA); /*Set the WiFi in STA Mode*/
47. WiFi.begin(ssid, password);
48. Serial.print("Connecting to ");
49. Serial.println(ssid);
50. delay(1000); /*Wait for 1000mS*/
51. while (WiFi.waitForConnectResult() != WL_CONNECTED)
52. {
53. Serial.print(".");
54. }
55. Serial.print("Connected to ");
56. Serial.println(ssid);
57. Serial.print("Your Local IP address is: ");
58. Serial.println(WiFi.localIP()); /*Print the Local IP*/
59.
60. server.on("/", MainPage); /*Display the Web/HTML Page*/
61. server.on("/readTemp", Temp); /*Display the updated Temperature and
Humidity value*/
62. server.begin(); /*Start Server*/
63. delay(1000); /*Wait for 1000mS*/
64. }
65.
66. void loop(void)
67. {
68. // Read the raw ADC value from the LM35 sensor
69. rawADCValue = analogRead(LM35_SENSOR_PIN);
70.
71. // Calibrate the raw ADC value and convert it to voltage in millivolts
72. voltageInMilliVolts = readAndCalibrateADC(rawADCValue);
73.
74. // Calculate the temperature in Celsius. The LM35 outputs 10mV per degree Celsius.
75. temperatureInCelsius = voltageInMilliVolts / 10;
76.
77. // Convert the temperature to Fahrenheit
78. temperatureInFahrenheit = (temperatureInCelsius * 1.8) + 32;
79.
80. server.handleClient();
81.
82. // Print the temperature readings in both Celsius and Fahrenheit
83. Serial.print("Temperature = ");
84. Serial.print(temperatureInCelsius);
85. Serial.print(" °C, ");
86. Serial.print("Temperature = ");
87. Serial.print(temperatureInFahrenheit);
88. Serial.println(" °F");
89.
90. // Delay for a short period before reading the temperature again
91. delay(1000); // Delay of 1 second
92. }
93.
94. // Function to read and calibrate the raw ADC value to voltage in millivolts
95. uint32_t readAndCalibrateADC(int rawADCValue)
96. {
97. // ADC calibration characteristics
98. esp_adc_cal_characteristics_t adcCharacteristics;
99.
100. // Characterize the ADC at attenuation of 11dB and width of 12 bits
101. esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12,
1100, &adcCharacteristics);
102.
103. // Convert the raw ADC value to calibrated voltage in millivolts
104. return esp_adc_cal_raw_to_voltage(rawADCValue, &adcCharacteristics);
105. }
Unique Features of My Project
1. Implemented a sleek, responsive HTML interface for temperature display, including a color-
coded temperature status (e.g., green for normal, red for high).
2. Introduced data logging functionality to store temperature readings at regular intervals.
3. Enabled dynamic updates using non-blocking methods for seamless real-time monitoring.
Connection Diagram
Procedure
1. Hardware Setup:
o Connect the LM35 sensor's VCC pin to the ESP32's 3.3V, GND to GND, and output pin
to an analog input pin (e.g., A0).
o Ensure stable connections using a breadboard and jumper wires.
2. Code Development:
o Write code to:
a. Read analog values from the LM35 using the ESP32 ADC.
b. Convert the analog value to a temperature in Celsius using the LM35's formula.
c. Serve a dynamic HTML page displaying the readings.
3. Dual Approach:
o Implement two versions of the code:
a. Delay-Based: Uses delays for basic operation.
b. Non-Blocking: Utilizes millis() for real-time, smooth operation without blocking.
4. Upload and Test:
o Use the Arduino IDE to upload the code to ESP32.
o Open the Serial Monitor to verify the temperature data and obtain the IP address for
accessing the web server.
5. Connect and Monitor:
o Connect to the ESP32's Wi-Fi network.
o Open a browser and enter the displayed IP address to access the web interface and
view temperature readings.
Observations
1. Accurate temperature readings were displayed with negligible delay.
2. The non-blocking code provided a seamless, real-time user experience compared to the
delay-based approach.
3. The web page interface enhanced usability with visual indicators and smooth updates.
Conclusion
The LM35 temperature sensor was effectively interfaced with the ESP32, and temperature readings
were successfully displayed on a real-time web server. Enhancements like non-blocking code and a
visually appealing interface improved the project's functionality, showcasing the ESP32's potential for
IoT applications. This experiment highlights the ease of integrating hardware and software to build
practical IoT solutions.
Name: Prashant Kumar
Roll No.: BT22EEE099