ESP32 - LM35 Temperature Sensor

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×LM35 Temperature Sensor
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About LM35 Temperature Sensor

Pinout

LM35 temperature sensor has three pins:

  • GND pin needs to be connected to GND (0V)
  • VCC pin needs to be connected to VCC (5V)
  • OUT pin signal pin gives the output voltage that is linearly proportional to the temperature, should be connected to a analog pin on ESP32.
  • LM35 temperature sensor Pinout

How It Works

The LM35 outputs the voltage linearly proportional to the Centigrade temperature. The output scale factor of the LM35 is 10 mV/°C. It means that the temperature is calculated by dividing the voltage (mV) in output pin by 10.

Wiring Diagram

  • Wiring diagram with power supply from USB cable
  • ESP32 LM35 temperature sensor Wiring Diagram

    Image is developed using Fritzing. Click to enlarge image

  • Wiring diagram with power supply from 5v adapter
  • ESP32 LM35 temperature sensor Wiring Diagram

    Image is developed using Fritzing. Click to enlarge image

How To Program For LM35 Temperature Sensor

  • Get the ADC value from the temperature sensor by using analogRead() function.
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 Celsius
float tempC = milliVolt / 10;
  • (Optional) Convert the Celsius to Fahrenheit
float tempF = tempC * 9 / 5 + 32;

ESP32 Code

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-lm35-temperature-sensor */ #define ADC_VREF_mV 3300.0 // in millivolt #define ADC_RESOLUTION 4096.0 #define PIN_LM35 A0 void setup() { Serial.begin(9600); } void loop() { // get 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 Celsius float tempC = milliVolt / 10; // convert the Celsius to Fahrenheit float tempF = tempC * 9 / 5 + 32; // print the temperature in the Serial Monitor: Serial.print("Temperature: "); Serial.print(tempC); // print the temperature in Celsius Serial.print("°C"); Serial.print(" ~ "); // separator between Celsius and Fahrenheit Serial.print(tempF); // print the temperature in Fahrenheit Serial.println("°F"); delay(1000); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Grasp the sensor by your hand
  • See the result on Serial Monitor. It looks like the below:.
  • 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

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere