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
void setup() 
  Serial.println("KY-015 test - temperature and humidity test:");
  // Measurement is started
// 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
  // 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");
  // Output to serial console
  Serial.print("Humidity: ");
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(char(186)); //Output <°> symbol
  Serial.println("C ");
  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

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, the required system modules must be updated and installed. For this, the next three commands are entered and confirmed with the "Enter" key in each case (these may also have to be confirmed with the "Y" key):

sudo apt-get update
sudo apt-get install build-essential python-dev


sudo apt install python3-libgpiod

With the command...

sudo pip3 install adafruit-circuitpython-dht

... the current version of the Adafruit_CircuitPython_DHT Library can be downloaded and installed.

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 and temperature (in °C and F).

When restarting the code, you should wait a while before doing so to avoid unnecessary error messages.

import time
import board
import adafruit_dht

# Initialize the dht device with the data pin connected to pin 16 (GPIO 23) of the Raspberry Pi:
dhtDevice = adafruit_dht.DHT11(board.D23)

# You can pass DHT22 use_pulseio=False if you do not want to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)

while True:
        # Print the values via the serial interface
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        print("Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(temperature_f, temperature_c, humidity))

    except RuntimeError as error:
        # Errors happen quite often, DHT's are hard to read, just move on
    except Exception as error:
        raise error


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

To start with the command:

sudo python3

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, search for the library to add it to.

Sample program download