Skip to main content
ESPBoards

ESP32 KY-013 Analog Temperature Sensor Module

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.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

🔗 Quick Links

KY-013 Analog Temperature Sensor Module Datasheet ButtonKY-013 Analog Temperature Sensor Module Specs ButtonKY-013 Analog Temperature Sensor Module Specs ButtonKY-013 Analog Temperature Sensor Module Specs Button

🛒 KY-013 Price

KY-013 Analog Temperature Sensor Module
Normally, the KY-013 Analog Temperature Sensor Module costs around 1$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

ℹ️ 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.

⚙️ KY-013 Sensor Technical Specifications

Below you can see the KY-013 Analog Temperature Sensor Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.

  • Type: module
  • Protocol: Analog
  • Operating Voltage: 3.3V - 5V
  • Temperature Range: -55°C to +125°C
  • Accuracy: ±0.5°C
  • Thermistor Resistance: 10 kΩ at 25°C
  • B-Value: 3950K
  • Dimensions: 19 x 15.5 x 7 mm

🔌 KY-013 Sensor Pinout

Below you can see the pinout for the KY-013 Analog Temperature Sensor Module. 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.

🛠️ KY-013 Analog Temperature Sensor Module Troubleshooting

This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug 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.

💻 Code Examples

Below you can find code examples of KY-013 Analog Temperature Sensor Module with ESP32 in several frameworks:

If you encounter issues while using the KY-013 Analog Temperature Sensor Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-013 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-013 Analog Temperature Sensor Module:

#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.

Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP-IDF Image

ESP32 KY-013 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-013 Analog Temperature Sensor Module, here's how you can set it up and read data from the sensor. Fill in this code in the main ESP-IDF file:

#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.

Update the I2C pins (I2C_MASTER_SDA_IO and I2C_MASTER_SCL_IO) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESPHome Image

ESP32 KY-013 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-013 Analog Temperature Sensor Module

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.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 KY-013 PlatformIO Code Example

Example in PlatformIO Framework

For PlatformIO, make sure to configure the platformio.ini file with the appropriate environment and libraries, and then proceed with the code.

Configure platformio.ini

First, your platformio.ini should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:

[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino

ESP32 KY-013 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-013 Analog Temperature Sensor Module:

#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.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 KY-013 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-013 Analog Temperature Sensor Module with your ESP32.

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.

Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy.

Conclusion

We went through technical specifications of KY-013 Analog Temperature Sensor Module, its pinout, connection with ESP32 and KY-013 Analog Temperature Sensor Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.