How to Use Thermistors with ESP32 for Accurate Temperature Sensing

Learn how to integrate thermistors with ESP32 to create accurate temperature measurement systems. This guide explains how thermistors work, their types, and how to use them in your projects for precise temperature sensing.


Temperature sensing is a critical aspect of many electronics projects, ranging from smart homes to weather stations. Among the most reliable and inexpensive components for temperature measurement are thermistors, which change their resistance based on temperature. Thermistors, when paired with the ESP32 microcontroller, can help you create highly accurate temperature sensing systems. The ESP32 is an excellent choice for this task, thanks to its powerful Analog-to-Digital Converter (ADC) and ease of integration with various sensors. In this article, we will explore how thermistors work, how to wire them to the ESP32, and how to use them to measure temperature in your projects.

Whether you are building a temperature monitoring system for your home, an environmental monitoring tool, or a battery management system, thermistors offer a simple yet effective solution. By combining these components with the ESP32, you can gather precise temperature data and even transmit it over Wi-Fi or Bluetooth for real-time monitoring. Let’s dive deeper into how thermistors function, how they are wired with the ESP32, and the practical applications you can implement using these components.

Datasheets:


Table of Contents


How Thermistors Work with ESP32

Thermistors are resistors whose resistance varies with temperature. The two main types of thermistors are Negative Temperature Coefficient (NTC) and Positive Temperature Coefficient (PTC). NTC thermistors are the most commonly used in temperature sensing applications because their resistance decreases as the temperature increases, which provides a predictable relationship between temperature and resistance. In ESP32 projects, thermistors are typically used in voltage divider circuits, where the thermistor’s resistance alters the voltage at a specific point in the circuit as the temperature changes. The ESP32’s ADC reads this voltage change and translates it into a temperature reading.

To use a thermistor with the ESP32, you will need to connect it to one of the analog input pins. The circuit typically includes the thermistor, a fixed resistor, and a power source. As the temperature changes, the resistance of the thermistor changes, which directly impacts the voltage at the junction of the thermistor and resistor. The ESP32 reads this voltage using its ADC and calculates the corresponding temperature. For accurate temperature readings, you may need to use a lookup table or a mathematical formula like the Steinhart-Hart equation to convert the resistance value to temperature.


Using an Analog Thermisor

Wiring Diagram

Here’s a basic wiring diagram for connecting a thermistor to an ESP32:

+3.3V (or 5V) ----> Thermistor ----> Analog Pin (e.g., GPIO 34)
| |
Resistor GND
|
GND

Parts List

To build a temperature sensing system using the ESP32 and a thermistor, you will need the following components:

  1. ESP32 Development Board (e.g., ESP32 DevKitC)
  2. NTC Thermistor (10kΩ is commonly used for general applications)
  3. Resistor (10kΩ – used for the voltage divider)
  4. Breadboard (for prototyping)
  5. Jumper Wires (for connections)
  6. Power Supply (3.3V or 5V, depending on your setup)
  7. Optional: External Pull-up Resistor (for stable ADC readings)

MicroPython Script

The following MicroPython script reads the voltage from the thermistor circuit and converts it to temperature using a simple calculation for an NTC thermistor. You can modify the formula or use a lookup table for more precise readings.

import machine
import time
import math

# Constants for the thermistor calculation
R1 = 10000  # Resistor value in ohms (10kΩ)
T0 = 298.15  # Reference temperature in Kelvin (25°C)
B = 3950  # Beta constant for the thermistor (typical value)

# Create an ADC object on GPIO34
adc = machine.ADC(machine.Pin(34))

# Set the ADC width and range (12-bit resolution)
adc.width(machine.ADC.WIDTH_12BIT)
adc.atten(machine.ADC.ATTN_0DB)

# Function to calculate temperature from thermistor resistance
def calculate_temperature(adc_value):
    # Calculate the thermistor resistance
    R = R1 * (4095 / adc_value - 1)
    # Calculate temperature using the Beta equation
    temperature = 1 / (1 / T0 + 1 / B * math.log(R / R1))
    # Convert temperature from Kelvin to Celsius
    temperature_celsius = temperature - 273.15
    return temperature_celsius

# Main loop to continuously read temperature
while True:
    # Read the ADC value (0 to 4095)
    adc_value = adc.read()
    
    # Calculate temperature
    temperature = calculate_temperature(adc_value)
    
    # Print the temperature
    print('Temperature: {:.2f} °C'.format(temperature))
    
    # Wait for a second before the next reading
    time.sleep(1)

How the Script Works:

  1. ADC Configuration: The script configures the ESP32’s ADC on GPIO34 to read the voltage from the thermistor. It sets the ADC width to 12 bits (providing values from 0 to 4095) and uses the 0dB attenuation for the best resolution in the voltage range.
  2. Thermistor Calculation: The script uses the Beta constant (B) and a reference resistor value (R1) to calculate the thermistor’s resistance based on the ADC value. The resistance is then used in the Beta equation to estimate the temperature in Kelvin, which is converted to Celsius.
  3. Temperature Output: The script continuously reads the temperature and prints it to the console every second.

Using a Digital Thermistor

The KY-028 is a digital thermistor module that uses the LM35 analog temperature sensor combined with a digital output for temperature threshold detection. The module features both an analog output and a digital output (HIGH or LOW) depending on the temperature threshold.

If you’re using the digital output to read the temperature as a binary signal, the module essentially works as a simple on/off indicator, which is more of a temperature threshold-based sensor (e.g., it can be used to signal when a certain temperature is exceeded). Here’s how to handle it in MicroPython.

Wiring the KY-028 to ESP32:

  • VCC -> 3.3V (or 5V)
  • GND -> GND
  • DO -> GPIO pin (e.g., GPIO 23) for digital output
  • AO -> GPIO pin (optional) for analog output (if needed)

MicroPython Script for KY-028 Digital Thermistor

This script will read the digital output from the KY-028, which will be HIGH when the temperature exceeds a certain threshold and LOW when it doesn’t.

import machine
import time

# Define the GPIO pin where the digital output (DO) of KY-028 is connected
do_pin = machine.Pin(23, machine.Pin.IN)

# Set up an optional analog input (AO) pin for further analysis
# ao_pin = machine.Pin(34, machine.Pin.IN)  # Uncomment if you wish to use the analog output

def read_digital_temperature():
    # Read the digital state (HIGH or LOW)
    temperature_status = do_pin.value()
    
    if temperature_status == 1:
        print("Temperature is above the threshold!")
    else:
        print("Temperature is below the threshold.")
    
def main():
    while True:
        read_digital_temperature()
        time.sleep(1)  # Wait for 1 second before the next reading

# Run the main loop
main()

Explanation of the Script:

  1. Pin Initialization:
    The script sets up GPIO 23 as an input pin to read the digital output (DO) of the KY-028. When the temperature exceeds the preset threshold, the DO pin goes HIGH (1), and when the temperature is below the threshold, it goes LOW (0).
  2. Reading the Digital Output:
    The function read_digital_temperature() reads the digital value of the pin and prints a message indicating whether the temperature is above or below the threshold.
  3. Main Loop:
    The main() function continuously checks the temperature by calling read_digital_temperature() and prints the corresponding status every second.

Practical Applications of Thermistors with ESP32

  1. Temperature Monitoring Systems:
    • By integrating thermistors with the ESP32, you can create a remote temperature monitoring system. This setup can be used in smart homes to track room temperature or in agricultural environments to monitor soil temperature.
  2. Weather Stations:
    • ESP32-based weather stations often include thermistors to measure air temperature. The ESP32 can then transmit this data to a cloud server for real-time monitoring.
  3. Battery Management Systems:
    • Thermistors are useful in battery-powered systems where temperature monitoring is essential to ensure safe charging and operation. The ESP32 can be programmed to activate a cooling system or shut down the device if temperatures exceed safe limits.
  4. Medical Devices:
    • Thermistors are also used in medical applications, such as digital thermometers, where accurate body temperature readings are essential. The ESP32 can read these measurements and display them on a connected screen or send them to a mobile app.

Conclusion

Integrating thermistors with the ESP32 provides a powerful and cost-effective solution for temperature measurement in a wide range of applications. By understanding how thermistors work, how to wire them to the ESP32, and how to interpret the data from the ADC, you can build accurate and reliable temperature sensing systems. Whether for home automation, weather monitoring, or battery management, thermistors paired with the ESP32 enable you to develop sophisticated systems that collect and process temperature data efficiently.

For further reading, explore additional resources like integrating multiple sensors with the ESP32 or tips for improving the accuracy of your temperature readings. With just a few simple components, you can enhance your ESP32 projects and add valuable functionality to your creations.

For more detailed guides, check out these related articles:

4 responses to “How to Use Thermistors with ESP32 for Accurate Temperature Sensing”

  1. I appreciate the detailed explanation, very helpful!

    1. Glad you found it helpful! You might also like this post on ESP32 Potentiometer Reading for more hands-on projects. Let me know if you have any questions!

  2. Thanks for sharing such valuable information!

    1. Glad you found it helpful! You might also enjoy this post on How to Integrate a Tilt Sensor with ESP32 for Motion-Sensing Projects. Let me know if you have any questions!

Leave a Reply

Your email address will not be published. Required fields are marked *