ESP8266 - LM35 Temperature Sensor

This tutorial instructs you how to use ESP8266 to read the temperature from LM35 sensor. In detail, we will learn:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×LM35 Temperature Sensor
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of LM35 Temperature Sensor

The LM35 Temperature Sensor Pinout

The LM35 temperature sensor has three pins:

  • GND pin: This should be connected to the ground (0V)
  • VCC pin: This should be connected to the VCC (5V)
  • OUT pin: This signal pin gives an output voltage that is linearly proportional to the temperature, and should be connected to an analog pin on ESP8266.
LM35 temperature sensor pinout

How It Works

The LM35 produces a voltage that is linearly proportional to the Centigrade temperature. The output of the LM35 has a scale factor of 10 mV/°C. This implies that the temperature can be determined by dividing the voltage (in mV) at the output pin by 10.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and LM35 temperature sensor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For LM35 Temperature Sensor

  • Retrieve the ADC value from the temperature sensor by utilizing the analogRead() function.
int adcVal = analogRead(PIN_LM35);
  • Transform the ADC value into a voltage in millivolts.
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
  • Transform the voltage into Celsius temperature.
float temperature_C = milliVolt / 10;
  • Optionally, convert Celsius to Fahrenheit.
float temperature_F = temperature_C * 9 / 5 + 32;

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-lm35-temperature-sensor */ #define ADC_VREF_mV 3300.0 // in millivolt #define ADC_RESOLUTION 1024.0 #define PIN_LM35 A0 // The ESP8266 pin ADC0 connected to LM35 void setup() { Serial.begin(9600); } void loop() { // read the ADC value from the temperature sensor int adcVal = analogRead(PIN_LM35); // convert the ADC value to voltage in millivolt float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION); // convert the voltage to the temperature in °C float temperature_C = milliVolt / 10; // convert the °C to °F float temperature_F = temperature_C * 9 / 5 + 32; // print the temperature in the Serial Monitor: Serial.print("Temperature: "); Serial.print(temperature_C); // print the temperature in °C Serial.print("°C"); Serial.print(" ~ "); // separator between °C and °F Serial.print(temperature_F); // print the temperature in °F Serial.println("°F"); delay(500); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to upload the code to the ESP8266.
  • Hold the sensor in your hand.
  • Check the results on the Serial Monitor.
COM6
Send
Temperature: 26.31°C ~ 79.36°F Temperature: 26.44°C ~ 79.59°F Temperature: 26.50°C ~ 79.70°F Temperature: 26.56°C ~ 79.81°F Temperature: 27.06°C ~ 80.71°F Temperature: 27.75°C ~ 81.95°F Temperature: 28.37°C ~ 83.07°F Temperature: 29.00°C ~ 84.20°F Temperature: 29.56°C ~ 85.21°F Temperature: 30.00°C ~ 86.00°F Temperature: 30.31°C ~ 86.56°F Temperature: 30.62°C ~ 87.12°F Temperature: 30.87°C ~ 87.57°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!