• Introduce the DHT11sensor.
    • Show how to use it using the control library.
    • Write a basic sketch to use it.

Bill of materials

Arduino UNO   Arduino UNO or equivalent.
BreadboardJumper wires A solderless Breadboard plus some jumper wires.
330 Ohm resistor  A 330Ω resistor
DHT11 Temperature sensor  A temperature sensor DHT11.




In the chapter 15, Temperature sensors, we saw how to measure the temperature with a TMP36 sensor. It is quite simple and the only special problem that can come up is handling operations with float numbers.

But often, and especially in the food industry, is not enough to measure temperature but relative humidity, which is also an important factor to consider.

Therefore the family of DHT sensors were developed because they provide digital temperature and humidity, with varying accuracy depending on the model.

Basically there are two models DHT11 and DHT22

The features of the DHT11 are:

  • It is very cheap, over 2€
  • It can be powered with 3,3 or 5V.
  • Temperature range: from 0º to 50º with 5% of accuracy (but only measures degrees, not fractions of degrees)
  • Humidity range: from 20% to 80% with 5% of accuracy.
  • 1 sample per second (It is not the fastest in the West)
  • Low power.
  • Measures in ºC.

The features of the DHT22 are:

  • It is very cheap, between 4 and 5 €.
  • It can be powered with 3,3 or 5V.
  • Temperature range: de -40º a 125º ±0.5°C.
  • Humidity range: from 0% to 1000% with 5% of accuracy.
  • 2 samples per second.
  • Low power.
  • Measures in ºC.

The way to connect and program them is the same for both models and we will see that there are Arduino libraries for supporting both in a simple way.

Note that the chip incorporates electronics to internally convert temperature and humidity and it will give us a digital measurement value, that is, we do not need an analog pin as in the case of TMP36, but we’ll read with a digital pin instead.




The connection is trivial, but it is noteworthy that they are sold encapsulated in two ways. One of them has three pins that are GND, Data and Vcc, and the other has 4-pin and one of them is simply left over and not connected. Usually the name of each pin is labeled in the sensor, but if not you can search the manual in Google.

DHT11 pinout
DHT11 3 pin model
Let’s go with the diagram, courtesy of Fritzing:

Chapter 24, schematic



And here we have the wiring diagram:

Chapter 24, wiring diagram




First, we need to download the library, DX11.zip, and we should import it. Here is the library:



Then we have to include it in the sketch:

#include <DHT11.h>

And we define an instance of the sensor object in which we declare to which pin it is connected.

int pin=2;
DHT11 dht11(pin);

Read the measure is very simple now:

int error ;
float temp, humi;
error  =  dht11.read(humi, temp)

We only need to write dht11.read() by passing the variables where you want the result, and check that there are no errors (is always a good idea to check whether there is no error before making a call). The entire program would be more or less like this:

Sketch 24.1
#include <DHT11.h>
int pin=2;
DHT11 dht11(pin);

void setup()

void loop()
       int err;
       float temp, hum;
       if((err = dht11.read(hum, temp)) == 0)    // If returns 0 the measure is good
             Serial.print("Temperature: ");
             Serial.print(" Humidity: ");
             Serial.print("Error Num :");
       delay(1000);                              //Remember that it can only make a sample per second

The result is sent to the Serial Console and here you can see a sample.

Temperature and humidity data screen


To vary the values of temperature and humidity and check that everything works correctly, you can simply breathe to the sensor, and unless you were in the tropics both temperature and relative humidity will raise up.

As you see, they are very easy to use and a regular component of your arsenal of sensors.





  • We have seen the DHT11 sensors and assemble a prototype using it
  • The DHT22 sensors use the same circuit schematic diagram and pins, but their resolution and speed is quite better, although for a lot of applications the DHT11 is useful enough.





No Comments

Give a Reply