ESP32 KY-013 Analog Temperature Sensor Module
Overview
The KY-013 is an analog temperature sensor module utilizing an NTC thermistor. It provides temperature measurements in the range of -55°C to +125°C with an accuracy of ±0.5°C. Suitable for various temperature monitoring applications.
About KY-013 Analog Temperature Sensor Module
The KY-013 Analog Temperature Sensor Module features an NTC thermistor capable of measuring temperatures ranging from -55°C to +125°C. As a negative temperature coefficient (NTC) thermistor, its resistance decreases with increasing temperature. This characteristic allows for temperature determination by measuring the voltage across the thermistor when used in a voltage divider configuration. The module operates at voltages between 3.3V and 5V, making it compatible with various microcontrollers, including Arduino and ESP32. It’s commonly used in applications such as climate control systems, environmental monitoring, and other temperature-dependent processes.
Where to Buy
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
Technical Specifications
Pinout Configuration
The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32.
Pin (-):
Ground (GND).Pin (middle):
VCC (3.3V to 5V).Pin (S):
Analog signal output.
Wiring with ESP32
KY-013 Pin (-):
Connect to ESP32GND
.KY-013 Pin (middle):
Connect to ESP323.3V
or5V
.KY-013 Pin (S):
Connect to an analog-capable GPIO pin on ESP32 (e.g.,GPIO36
).
Troubleshooting Guide
Common Issues
🌡️ Incorrect Temperature Readings
Issue: The sensor outputs inaccurate or fluctuating temperature values.
Solutions:
- Ensure stable power supply to the sensor.
- Verify correct wiring connections, especially the analog signal pin.
- Implement proper analog signal filtering in the code to reduce noise.
❌ No Output from Sensor
Issue: The sensor provides no data or constant values.
Solutions:
- Check for loose or incorrect connections.
- Confirm that the analog input pin on the microcontroller is functioning correctly.
- Test the sensor with a known temperature source to validate its operation.
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
#include <math.h>
int sensorPin = A0; // Analog input pin connected to the sensor
void setup() {
Serial.begin(9600);
}
void loop() {
int rawValue = analogRead(sensorPin);
double voltage = rawValue (5.0 / 1023.0);
double resistance = (5.0 - voltage) 10000 / voltage;
double temperature = 1 / (log(resistance / 10000) / 3950 + 1 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000);
}
This Arduino code reads the analog value from the KY-013 sensor, calculates the corresponding temperature using the Steinhart-Hart equation, and outputs the temperature in Celsius to the serial monitor. The sensor is connected to analog pin A0.
ESP-IDF Example
#include <stdio.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
#define ADC_CHANNEL ADC1_CHANNEL_0 // GPIO36
void app_main(void) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC_CHANNEL, ADC_ATTEN_DB_11);
while (1) {
int raw = adc1_get_raw(ADC_CHANNEL);
double voltage = raw (3.3 / 4095.0);
double resistance = (3.3 - voltage) 10000 / voltage;
double temperature = 1 / (log(resistance / 10000) / 3950 + 1 / 298.15) - 273.15;
printf("Temperature: %.2f °C\n", temperature);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
This ESP-IDF code configures ADC1 on GPIO36 to read the analog signal from the KY-013 sensor. It calculates the temperature using the Steinhart-Hart equation and prints the result in Celsius every second.
ESPHome Example
sensor:
- platform: ntc
sensor: resistance_sensor
name: "KY-013 Temperature Sensor"
calibration:
b_constant: 3950
reference_temperature: 25°C
reference_resistance: 10kΩ
- platform: adc
pin: GPIO36
id: resistance_sensor
update_interval: 1s
This ESPHome configuration sets up the KY-013 temperature sensor using an NTC thermistor on GPIO36. The resistance is measured via ADC, and the temperature is calculated using the Steinhart-Hart equation with a B-constant of 3950.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#include <Arduino.h>
#include <math.h>
#define SENSOR_PIN 36 // Analog input pin
void setup() {
Serial.begin(115200);
}
void loop() {
int rawValue = analogRead(SENSOR_PIN);
double voltage = rawValue (3.3 / 4095.0);
double resistance = (3.3 - voltage) 10000 / voltage;
double temperature = 1 / (log(resistance / 10000) / 3950 + 1 / 298.15) - 273.15;
Serial.printf("Temperature: %.2f °C\n", temperature);
delay(1000);
}
This PlatformIO code configures the KY-013 temperature sensor on GPIO36. It reads the analog value, converts it to voltage, calculates the resistance, and then determines the temperature using the Steinhart-Hart equation.
MicroPython Example
import machine
import time
import math
SENSOR_PIN = 36 # ADC input pin
adc = machine.ADC(machine.Pin(SENSOR_PIN))
adc.atten(machine.ADC.ATTN_11DB) # Full range 0-3.3V
def get_temperature():
raw = adc.read()
voltage = raw (3.3 / 4095.0)
resistance = (3.3 - voltage) 10000 / voltage
temperature = 1 / (math.log(resistance / 10000) / 3950 + 1 / 298.15) - 273.15
return temperature
while True:
print("Temperature:", round(get_temperature(), 2), "°C")
time.sleep(1)
This MicroPython script reads the KY-013 sensor using ADC on GPIO36. It converts the voltage to resistance and then calculates the temperature using the Steinhart-Hart equation. The temperature is printed to the console every second.
Conclusion
The ESP32 KY-013 Analog Temperature Sensor Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.