ESP32 KY-015 Temperature and Humidity Sensor Module
The KY-015 is a temperature and humidity sensor module based on the DHT11 sensor. It provides digital output for temperature and humidity measurements, making it ideal for environmental monitoring applications.
🔗 Quick Links
🛒 KY-015 Price
ℹ️ About KY-015 Temperature and Humidity Sensor Module
The KY-015 Temperature and Humidity Sensor Module integrates the DHT11 sensor, capable of measuring temperatures from 0°C to 50°C with an accuracy of ±2°C, and relative humidity levels from 20% to 90% with an accuracy of ±5%. It communicates using a single-wire digital protocol, making it straightforward to interface with microcontrollers like Arduino and ESP32. Due to its low sampling rate, the sensor provides new readings approximately every two seconds, making it suitable for applications such as climate monitoring, greenhouse management, and environmental data logging.⚙️ KY-015 Sensor Technical Specifications
Below you can see the KY-015 Temperature and Humidity 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: 1-Wire
- Operating Voltage: 3.3V - 5V
- Temperature Range: 0°C to 50°C
- Temperature Accuracy: ±2°C
- Humidity Range: 20% to 90% RH
- Humidity Accuracy: ±5% RH
- Sampling Rate: Once every 2 seconds
🔌 KY-015 Sensor Pinout
Below you can see the pinout for the KY-015 Temperature and Humidity 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):
Digital signal output.
🧵 KY-015 Wiring with ESP32
Below you can see the wiring for the KY-015 Temperature and Humidity Sensor Module with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
KY-015 Pin (-):
Connect to ESP32GND
.KY-015 Pin (middle):
Connect to ESP323.3V
or5V
.KY-015 Pin (S):
Connect to a GPIO pin on ESP32 (e.g.,GPIO4
).
🛠️ KY-015 Temperature and Humidity 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.
❌ No Data Output
Issue: The sensor is not providing any data.
Solutions:
- Ensure all connections are secure and correctly placed.
- Verify that the microcontroller pin connected to the sensor's data pin is correctly configured as an input.
- Confirm that the sensor is receiving adequate power (3.3V or 5V).
⚠️ Incorrect or Fluctuating Readings
Issue: The sensor outputs incorrect or unstable temperature and humidity values.
Solutions:
- Check for electromagnetic interference from nearby devices; try to isolate the sensor from potential sources of interference.
- Ensure that the sensor is not exposed to condensation or water droplets, which can affect readings.
- Allow the sensor to stabilize in the environment for a few minutes before taking readings.
💻 Code Examples
Below you can find code examples of KY-015 Temperature and Humidity Sensor Module with ESP32 in several frameworks:
If you encounter issues while using the KY-015 Temperature and Humidity Sensor Module, check the Common Issues Troubleshooting Guide.

ESP32 KY-015 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the KY-015 Temperature and Humidity Sensor Module:
#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("KY-015 Test - Temperature and Humidity:");
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C");
}
This Arduino code utilizes the DHT library to read temperature and humidity data from the KY-015 sensor. The sensor is connected to digital pin 2, and the readings are displayed on the serial monitor every two seconds.
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.

ESP32 KY-015 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the KY-015 Temperature and Humidity 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 "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "dht.h"
#define DHT_PIN GPIO_NUM_4
void app_main(void) {
printf("KY-015 Sensor Test\n");
while (1) {
int16_t temperature = 0;
int16_t humidity = 0;
if (dht_read_data(DHT_TYPE_DHT11, DHT_PIN, &humidity, &temperature) == ESP_OK) {
printf("Humidity: %d.%d%% Temperature: %d.%d°C\n", humidity / 10, humidity % 10, temperature / 10, temperature % 10);
} else {
printf("Could not read data from sensor\n");
}
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
This ESP-IDF code reads temperature and humidity data from the KY-015 sensor using the DHT11 library. The sensor is connected to GPIO4, and data is printed to the console every 2 seconds. If the reading fails, an error message is displayed.
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.

ESP32 KY-015 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the KY-015 Temperature and Humidity Sensor Module
sensor:
- platform: dht
pin: GPIO4
model: DHT11
temperature:
name: "KY-015 Temperature"
humidity:
name: "KY-015 Humidity"
update_interval: 2s
This ESPHome configuration sets up the KY-015 temperature and humidity sensor using the DHT11 protocol. The sensor is connected to GPIO4, and readings are updated every 2 seconds.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.

ESP32 KY-015 PlatformIO Code Example
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
lib_deps =
adafruit/DHT sensor library
ESP32 KY-015 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the KY-015 Temperature and Humidity Sensor Module:
#include <Arduino.h>
#include <DHT.h>
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("KY-015 Test - Temperature and Humidity:");
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C");
}
This PlatformIO code reads the KY-015 sensor data using the DHT library. It prints temperature and humidity values every 2 seconds, with error handling for failed sensor readings.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.

ESP32 KY-015 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the KY-015 Temperature and Humidity Sensor Module with your ESP32.
import dht
import machine
import time
sensor = dht.DHT11(machine.Pin(4))
while True:
try:
sensor.measure()
temp = sensor.temperature()
humidity = sensor.humidity()
print("Temperature:", temp, "°C", " Humidity:", humidity, "%")
except OSError as e:
print("Failed to read sensor.")
time.sleep(2)
This MicroPython script reads temperature and humidity data from the KY-015 sensor connected to GPIO4. It updates readings every 2 seconds and handles errors in case of sensor failure.
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-015 Temperature and Humidity Sensor Module, its pinout, connection with ESP32 and KY-015 Temperature and Humidity Sensor Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.