DHT11: all about the sensor for measuring temperature and humidity

DHT11

Measuring temperature and humidity is very common in many electronic maker projects. In DIY it is common to have to measure these parameters to control certain systems. For example, to be able to create a refrigeration, plant care, or air conditioning system that starts up if the temperature or humidity reaches a certain value. But for that to be possible you need a sensor like the DHT11.

Release there are many sensors very different temperature ranges, with supported temperature ranges or different precisions. An example of this is the LM35, one of the most popular and used in electronics. There are also other humidity sensors that act by variation of conductivity such as the AD22103KTZ from Analog Devices. But if you want to measure both parameters, perhaps the device that we discuss today in this article is of much more interest ...

What is DHT11?

El DHT11 is a simple sensor that measures temperature and humidity, all in one. A) Yes you won't have to buy two sensors separately. Its price is about € 2, so it is quite cheap, although you can also find it mounted on a module (mounted on a PCB for ease of use) as is usual in this type of electronic components for Arduino. In the case of the board, it includes a 5 kilo ohm pull-up resistor and an LED that warns us of the operation.

DHT11 has high reliability and stability due to its calibrated digital signal. Also, if you look at its datasheet, you will see that it has interesting features as you will see in future sections.

Similar products

DHT22

There is a product similar to DHT11 that you may be interested in. It is the DHT22. It is also an integrated temperature and humidity sensor, but in this case its price is a little higher, about € 4. Accuracy to measure temperature is 5% variation also like DHT11, but unlike it, it measures beyond the range of humidity between 20 and 80%. Therefore, you may be interested in the DHT22 for projects where you need to measure humidity from 0 to 100%.

La data collection frequency it is also twice that of DHT11, in DHT22 2 samples are taken per second instead of 1 sample per second of DHT11. As for the temperature, it can measure from -40ºC to + 125ºC with more precision, since it can measure fractions of degrees, specifically it can appreciate variations of plus / minus 0,5ºC.

Pinout, features and datasheet

DHT11 pinout

You can find quite a bit of technical information about the DHT11 in your datasheets. Each manufacturer of this device can provide some values ​​that may vary, so I always recommend reading the PDF of the specific manufacturer of the device you have purchased. Although most values ​​may look the same to you, there could be some slight variation from one to the other. Its most important technical characteristics are:

  • 3,5v to 5v power supply
  • 2,5mA current consumption
  • Digital output signal
  • Temperature range from 0ºC to 50ºC
  • Accuracy to measure temperature at 25ºC of about 2ºC variation
  • The resolution to measure temperature is 8-bit, 1ºC
  • Humidity can measure from 20% RH to 90% RH
  • Accurately humidity 5% RH for temperatures between 0-50ºC
  • The resolution is 1% RH, it cannot pick up variations below that
  • Mouser Datasheet

Regarding the data, broadcast in digital. Therefore, it is not necessary to go from analog to digital as in other sensors. That complicated the code to write in the Arduino IDE, but in this case it is not needed and it is much easier. Although the sensor itself is analog, but it includes a system to perform the conversion and can be connected directly to a digital input of the Arduino.

The analog signal, which is a variation of the voltage, from the sensor is passed into digital format to be sent to the Arduino microcontroller. It is transmitted in a 40-bit frame that correspond to the humidity and temperature information captured by the DHT11. The first two 8-bit groups are for humidity, that is, the most significant 16 bits of this frame. Then the other 2 remaining 8-bit groups for temperature. That is, it has two bytes for humidity and two bytes for temperature. For example:

0011 0101 0000 0010 0001 1000 0000 0000 0011 1001

In this case, 0011 0101 0000 0010 is the humidity value, and 0001 1000 0000 0000 is the temperature. A first part is for the integer part and the second part is for decimals. As for 0011 1001, that is, the last 8-bit are parity to avoid mistakes. That way you can check that everything is correct during transmissions. It corresponds to the sum of the previous bits, therefore, if the sum is equal to parity, it will be correct. In the example that I have put, it would not be, because as you can see it does not correspond ... That would indicate a failure.

Once this is known, the next thing on a technical level of the DHT11 that should be noted is the pins. The contacts or pinout of this device is simple, since it only has 4 of them. One of the pins is for power or Vcc, the other for I / O to transmit data, an NC pin that does not connect, and GND for the ground connection.

Integration with Arduino

Connecting the DHT11 with Arduino

Once you know the pinout of the DHT11 and also the Arduino board, the connection is very simple. Remember that if you have chosen a DHT11 module integrated into a PCB, the pins will be three, since the NC is removed to make things easier. All you have to do is connect the ground pin to one of the Arduino's GND connections as it appears in the diagram in the previous image.

On the other hand, the power pin should be connected to the 5v connection from Arduino, Thus the sensor will be fully powered with GND and Vcc, but now the data is missing. To transmit the data from the DHT11 sensor to the Arduino board, you can use any of the digital inputs, such as the 7 that appears in the image ... Now you have everything ready to use it once you have created the necessary Arduino IDE code ...

If the sensor is far away in your project and you are going to use a cable longer than 20 meters, then use a 5k pull-up resistor, for larger cables it should be proportionally greater. Note that if you use 3,5v power instead of 5v, then the cable should not be longer than 20cm due to voltage drops.

Remember that what they recommend is take measurements every 5 seconds, although the sampling frequency at which the DHT11 can operate is higher, but if it is done more frequently it may not be as accurate.

Code in Arduino IDE

Going straight to the code, say that in Arduino IDE you can use a number of existing libraries with features that will make your life easier with the DHT11. For example, one of them is the one that provides Adafruit. Remember that we have a beginner's guide that starts with Arduino in PDF that you can download free from here and it can help you.

Once you have the corresponding library installed, you can comment to enter the code to control the DHT11 temperature and humidity sensor for your project with Arduino. For example:

#include "DHT.h"

const int DHTPin = 7;     
 
DHT dht(DHTPin, DHTTYPE);
 
void setup() {
   Serial.begin(9600);
   Serial.println("Midiendo...");
 
   dht.begin();
}
 
void loop() {
   delay(2000);
 
   float h = dht.readHumidity();
   float t = dht.readTemperature();
 
   if (isnan(h) || isnan(t)) {
      Serial.println("Fallo en la lectura del sensor DHT11");
      return;
   }
 
 
   Serial.print("Humedad relativa: ");
   Serial.print(h);
   Serial.print(" %\t");
   Serial.print("Temperatura: ");
   Serial.print(t);
   Serial.print(" ºC ");
}


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.