Thermistors aren’t just sensors—they’re tiny temperature detectives your ESP32 needs.
When I first started tinkering with temperature sensing on the ESP32, I assumed all sensors were created equal. Spoiler: they’re not. My early builds gave me erratic, unreliable temperature readings that felt more like a wild guess than solid data.
Then I met thermistors—the tiny, unsung heroes of temperature sensing. These simple components turned my ESP32 from a confused guesser into a sharp, precise temperature detective. No expensive gadgets, no proprietary magic—just clever hardware and open-source smarts.
For fellow FOSS advocates and makers, thermistors offer an elegant, affordable way to bring accuracy and reliability to your projects without sacrificing openness or flexibility.
If you’re ready to level up your ESP32 temperature sensing game and embrace the power of thermistors, keep reading—because precision is just a few wires away.
Datasheets:
- KY-028 Digital Temperature Sensor Module
- Analog Temperature Arduino Sensor Module NTC Thermistor 3 Pin Black Color DC 5V
- How Thermistors Work: NTC vs PTC and Why It Matters
- Using an Analog Thermisor
- Using a Digital Thermistor
- Calibrating Your Thermistor: Making Sense of Real-World Readings
- Practical Applications of Thermistors with ESP32
- Wrapping Up: Why Thermistors Are a FOSS-Friendly Game-Changer for ESP32 Temperature Sensing
How Thermistors Work: NTC vs PTC and Why It Matters
Thermistors are resistors that change their resistance based on temperature. There are two main types:
- NTC (Negative Temperature Coefficient) thermistors decrease resistance as temperature rises—making them ideal for temperature sensing because this predictable, inverse relationship is easy to measure.
- PTC (Positive Temperature Coefficient) thermistors do the opposite: resistance increases as temperature rises, and they’re often used as resettable fuses rather than sensors.
In most ESP32 temperature projects, NTC thermistors are your go-to. Their smooth resistance change across typical temperature ranges makes them well-suited for accurate measurement and simple calculation. Understanding this behavior helps you interpret readings correctly and choose the right thermistor for your project.
· · ─ ·𖥸· ─ · ·
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:
- ESP32 Development Board (e.g., ESP32 DevKitC)
- NTC Thermistor (10kΩ is commonly used for general applications)
- Resistor (10kΩ – used for the voltage divider)
- Breadboard (for prototyping)
- Jumper Wires (for connections)
- Power Supply (3.3V or 5V, depending on your setup)
- Optional: External Pull-up Resistor (for stable ADC readings)
Like what you’re reading?
Help keep DevDigest
free and caffeine-powered
—buy me a coffee on Ko-fi.
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:
- 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.
- 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.
- 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:
- 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). - Reading the Digital Output:
The functionread_digital_temperature()
reads the digital value of the pin and prints a message indicating whether the temperature is above or below the threshold. - Main Loop:
Themain()
function continuously checks the temperature by callingread_digital_temperature()
and prints the corresponding status every second.
Calibrating Your Thermistor: Making Sense of Real-World Readings
Thermistors don’t output temperature directly—they give you resistance values that vary with temperature in a non-linear way. To translate ADC readings into accurate temperatures, calibration is key.
One popular method is using the Steinhart-Hart equation or the simpler Beta parameter equation, which relate resistance to temperature mathematically. You’ll need the thermistor’s datasheet values for these formulas, including the Beta coefficient and nominal resistance at 25°C.
Calibration also involves:
- Taking readings at known temperatures (e.g., ice water at 0°C, boiling water at 100°C)
- Adjusting your calculations or code constants based on these references
- Considering your ESP32’s ADC characteristics and any voltage fluctuations
A well-calibrated thermistor setup transforms your ESP32 project from a rough temperature guesser into a precision sensor.
· · ─ ·𖥸· ─ · ·
Practical Applications of Thermistors with ESP32
- 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.
- 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.
- 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.
- 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.
· · ─ ·𖥸· ─ · ·
Wrapping Up: Why Thermistors Are a FOSS-Friendly Game-Changer for ESP32 Temperature Sensing
Don’t let their size fool you—thermistors pack a big punch. When paired with the ESP32 and open-source code, they offer precise, affordable, and adaptable temperature sensing that anyone can build, modify, and improve.
Whether you’re a student, hobbyist, or open-source advocate creating practical tools or teaching others, thermistors embody the best of DIY: simple hardware, powerful results, and community-driven innovation.
If this guide helped you get a clearer picture of temperature sensing with ESP32 and thermistors, you’ll want to stay connected for more hands-on, free knowledge.
👉 Subscribe now to my newsletter at samgalope.dev/newsletter and join a growing community of makers dedicated to empowering each other through open-source technology.
Like what you’re reading?
Help keep DevDigest
free and caffeine-powered
—buy me a coffee on Ko-fi.
Leave a Reply