# DHT11 temperature and humidity sensor with Arduino

> Learn how to read temperature and humidity from the DHT11 sensor with Arduino, using Adafruit's DHT library and its readTemperature and readHumidity methods.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2021-03-17 | Topics: [Arduino](https://flaviocopes.com/tags/electronics/) | Canonical: https://flaviocopes.com/electronics-components-dht11-temperature-sensor/

This sensor is one of the first sensors you learn to use because everyone has a good application for it: building an indoor or outdoor thermometer.

Here you can see it mounted on a breakout board:

![DHT11 temperature sensor mounted on black breakout board with blue plastic sensor housing](https://flaviocopes.com/images/electronics-components-dht11-temperature-sensor/IMG_3661.jpg)

![Side view of DHT11 sensor breakout board showing blue temperature sensor and three pins](https://flaviocopes.com/images/electronics-components-dht11-temperature-sensor/IMG_3659.jpg)

![Back view of DHT11 breakout board showing pin labels and circuit board components](https://flaviocopes.com/images/electronics-components-dht11-temperature-sensor/IMG_3660.jpg)

Note that the sensor has 4 output pins, although the breakout board I got only has 3 (the reason being the pin 3 of DHT11 is not connected to anything - don't ask me why).

From the left, keeping our pins at the bottom, we have:

- `Vdd`, the positive input (`+`)
- `Vss`, the negative input (`-`)
- the output signal

The output is a 40-bit serialized signal that lasts 4ms.

This means that every 4ms the sensor sends the temperature information and to read it we must get the value and unserialize it.

In the Arduino program we use the **DHT Sensor Library** maintained by Adafruit ([here's the link to the repository](https://github.com/adafruit/DHT-sensor-library)) that makes it very simple for us to read the temperature:

![Arduino IDE Library Manager showing search results for DHT sensor libraries including Adafruit DHT Sensor Library](https://flaviocopes.com/images/electronics-components-dht11-temperature-sensor/Screen_Shot_2020-12-18_at_20.01.53.png)

You include it with `#include <DHT.h>`, then you initialize an object of class `DHT` by passing the sensor signal pin and the type, in this case using the constant `DHT11`.

> The library can also work with other sensors like the more precise `DHT22` and `DHT21`

Then you can call the `readHumidity()` and `readTemperature()` methods to get values as a `float` variable.

`readTemperature()` gets the value as Celsius, but the library also provides a `convertCtoF()` method to get the Fahrenheit value.

The library also provide other methods, like `computeHeatIndex()`. I recommend you to checkout the [`DHT.h` header file source code on GitHub](https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.h).

This simple Arduino program reads the data from a DHT11 connected with the signal pin on digital pin #2 and prints it to the serial monitor:

```c
#include <DHT.h>

DHT dht(2, DHT11);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Cannot read values");
    return;
  }

  Serial.println((String)"Humidity: " + h + "%, temperature: " + t + "C");
}
```

![Circuit diagram showing Arduino Uno connected to DHT11 temperature sensor with wiring to digital pin 2](https://flaviocopes.com/images/electronics-components-dht11-temperature-sensor/Swanky_Curcan.png)

![Arduino Uno board connected to DHT11 sensor with jumper wires showing the actual hardware setup](https://flaviocopes.com/images/electronics-components-dht11-temperature-sensor/IMG_3662.jpg)

```
Humidity: 56.00%, temperature: 20.20C
Humidity: 56.00%, temperature: 20.20C
Humidity: 56.00%, temperature: 20.10C
Humidity: 56.00%, temperature: 20.20C
```
