This sensor is a mixture of temperature sensor and humidity sensor in a compact design - however, the disadvantage is the low sampling rate of the measurement, so that only every 2 seconds a new measurement result is available - this sensor is therefore very well suited for long-term measurements.

Technical data

Chipset DHT11
Communication Protocol 1-Wire
Measuring range 0°C to 50°C
Measurement accuracy ±2°C
Measurement Accuracy ±5%RH
Measurable humidity 20-90%RH

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Sensor
Pin D2 Signal
5V +V
Ground GND

For the following code example an additional library is needed:

DHT-sensor-library by Adafruit | published under the MIT License.

This sensor does not output its measurement result as an analog signal to an output pin, but communicates it digitally encoded.

The example below uses this said library - for this we recommend to download it from Github, unzip it and copy it in the Arduino library folder which is located by default in (C:\User[username]\Documents\Arduino\libraries) so that it is available for this code example and following projects. Alternatively, this is also included in the download package below as well.

// Adafruit_DHT library is inserted
#include "DHT.h"
 
// Here the respective input pin can be declared
#define DHTPIN 2     
 
// The sensor is initialized
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
 
void setup() 
{
  Serial.begin(9600);
  Serial.println("KY-015 test - temperature and humidity test:");
 
  // Measurement is started
  dht.begin();
}
 
// Main program loop
// The program starts the measurement and reads out the measured values
// There is a pause of 2 seconds between measurements,
// so that a new measurement can be acquired on the next run.
void loop() {
 
  // Two seconds pause between measurements
  delay(2000);
 
  // Humidity is measured
  float h = dht.readHumidity();
  // temperature is measured
  float t = dht.readTemperature();
   
  // Checking if the measurements have passed without errors
  // if an error is detected, a error message is displayed here
  if (isnan(h) || isnan(t)) {
    Serial.println("Error reading the sensor");
    return;
  }
 
  // Output to serial console
  Serial.println("-----------------------------------------------------------");
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(char(186)); //Output <°> symbol
  Serial.println("C ");
  Serial.println("-----------------------------------------------------------");
  Serial.println(" ");
}

Please note that the sensor provides a new measurement result only about every 2 seconds; thus, it is rather designed for long-term recordings.

Example program download

KY015-Arduino.zip

Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
GPIO 23 [Pin 16] Signal
+3.3V [Pin 1] +V
Ground [Pin 6] GND

The program uses the corresponding Adafruit_CircuitPython_DHT library from Adafruit to control the DHT11 sensor, which is installed on this sensor module. This was released with the MIT OpenSource license.

This must be installed first. To do this, the following procedure must be followed:

First, if this has not been done on the Raspberry Pi, the GitHub software must be installed:

sudo apt-get install git

For this, the Raspberry Pi must be connected to the Internet.

After that, the required system modules have to be updated and installed. To do this, enter the next two commands and confirm each with the "Enter" key (you may have to confirm them additionally with the "Y" key):

sudo apt-get update

and

sudo apt-get install build-essential python-dev

With the command...

git clone https://github.com/adafruit/Adafruit_CircuitPython_DHT.git

... the current version of the Adafruit_CircuitPython_DHT library can be downloaded and unpacked.

After that we change with...

cd Adafruit_Python_DHT/

... into the downloaded folder and install with...

sudo python setup.py install

... the library.

After this the following Python code example can be used. The program starts the measurement at the sensor and outputs the measured values for air pressure, temperature and altitude above sea level.

#!/usr/bin/python
# coding=utf-8
 
# import and set up needed modules
import RPi.GPIO as GPIO
import Adafruit_DHT
import time
 
# The pause of two seconds between measurements is set here
sleeptime = 2
 
# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
DHTSensor = Adafruit_DHT.DHT11
 
# Here you can declare the pin to which the sensor module is connected
GPIO_Pin = 23
 
print('KY-015 sensor test - temperature and humidity')
 
try:
    while(1):
        # measurement is started and the result is written to the corresponding variables
        humidity, temperature = Adafruit_DHT.read_retry(DHTSensor, GPIO_Pin)
 
        print("-----------------------------------------------------------------")
        if humidity is not None and temperature is not None:
 
            # The measured result is printed in the console
            print('Temperature = {0:0.1f}°C | rel. humidity = {1:0.1f}%'.format(Temperature, Humidity))
 
        # Since the Raspberry Pi is disadvantaged for real-time applications due to the Linux operating system,
        # it is possible that due to timing problems the communication can fail.
        # In this case an error message will be issued - a result should be available on the next try
        else:
            print('Error reading - please wait for next attempt!')
        print("-----------------------------------------------------------------")
        print("")
        time.sleep(sleeptime)
 
# Clean up after the program is finished
except KeyboardInterrupt:
    GPIO.cleanup()

Please note that the sensor provides a new measurement result only about every 2 seconds; thus, it is rather designed for long-term recordings

Example program download

KY015-RPi.zip

To start with the command:

sudo python3 KY015.py

Code example Micro:Bit

Pinout Micro:Bit:

Micro:Bit Sensor
Pin 1 Signal
3V +V
Ground GND

This is a MakeCode example for Micro:Bit which essentially does the same as the examples of the other two variants. However, this example is closer to the Raspberry Pi example than the Arduino example.

An additional librarie is needed for the following code example:

pxt-DHT11_DHT22 from alankrantas | published under the MIT License.

You must add this library to your IDE before using the code.

You do this by going to Extensions in your IDE and, using the following URL https://github.com/alankrantas/pxt-DHT11_DHT22.git, search for the library to add it to.

Sample program download

microbit-KY-015.zip