DHT11: all about the sensor for measuring temperature and humidity

DHT11

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

There are many different temperature sensors on the market, with supported temperature ranges or different accuracies. An example of this is the LM35, one of the most popular and used sensors 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 you will be more interested in the device we are discussing today…

What is DHT11?

The DHT11 is a simple sensor that measures temperature and humidity all in one. So separately. Its price is about 2 euros, so it’s quite cheap, although you can also find it mounted on a module (mounted on a PCB for easy use) as it’s usual in this kind of electronic components for Arduino. In the case of the board, it includes a 5 kilo ohm pull-up resistor and a 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 the next sections.

Similar products

DHT22

There is a product similar to DHT11 that might interest you. It’s DHT22. It is also an integrated temperature and humidity sensor, but in this case its price is a little higher, about 4 The accuracy for measuring temperature is 5% variation also like the DHT11, but unlike it, it measures beyond the humidity range of 20-80%. So you might be interested in the DHT22 for projects where you need to measure humidity from 0 to 100%.

The data collection frequency is also twice as high as DHT11, in DHT22 2 samples are taken per second instead of 1 sample per second from DHT11. As for the temperature, it can measure from -40ºC to +125ºC with more precision, as it can measure fractions of degrees, specifically it can appreciate variations of plus/minus 0.5ºC.
<Pinout, characteristics and datasheet

DHT11 pinout

Pinout, features and datasheet

You can find a lot of technical information about DHT11 in its datasheets. Each manufacturer of this device can provide some values that might vary, so I always recommend reading the PDF of the specific manufacturer of the device you have purchased. Although most of the values may seem the same to you, there might be some small variation from one to another. The most important technical characteristics are:

  • 3.5v to 5v power supply
  • Current consumption of 2.5mA
  • 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
  • Moisture can be measured from 20% RH to 90% RH
  • Accurate to 5% RH humidity for temperatures between 0-50ºC
  • The resolution is 1% RH, it cannot capture variations below that
  • Mouser Data Sheet

 

 

 

As for the data, they are transmitted in digital. Therefore, there is no need to switch from analogue to digital as in other sensors. That complicated the code that has to be written in Arduino IDE, but in this case it is not needed and it is much easier. Although the sensor itself is analog, but includes a system to perform the conversion and can be connected directly to an Arduino digital input.

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

0011 0101 0000 0010 0001 1000 0000 0011 1001

In this case, 0011 0101 0000 0010 is the humidity value, and 0001 1000 0000 0000 for the temperature. A first part is for the whole part and the second part is for decimals. As for 0011 1001, that is, the last 8-bit are parity to avoid errors. That way you can check that everything is correct during the transmissions. It corresponds to the sum of the previous bits, so if the sum is equal to the parity it will be correct. In the example 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 the technical level of the DHT11 to be highlighted 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 is not connected, and GND for the ground connection.

Integration with Arduino

DHT11 connection 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 in a PCB, the pins will be three, as the NC is removed to make things easier. All you have to do is connect the ground pin to one of Arduino’s GND connections as shown in the diagram in the previous picture.

On the other hand, the power pin must be connected to Arduino’s 5v connection, so the sensor will be fully powered with GND and Vcc, but now the data pin is missing. To transmit the data from the DHT11 sensor to the Arduino board, you can use any of the digital inputs, like the 7 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 longer cables it should be proportionally higher. 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 they recommend taking measurements every 5 seconds, although the sampling frequency at which DHT11 can operate is higher, but if it is done more frequently it may not be as accurate.

Going directly to the code, say that in Arduino IDE you can use a series of existing libraries with functions 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 guide for beginners starting with Arduino in PDF that you can download for free from here and it can help you.

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

#include &amp;quot;DHT.h&amp;quot;

const int DHTPin = 7;
 
DHT dht(DHTPin, DHTTYPE);
 
void setup() {
   Serial.begin(9600);
   Serial.println(&amp;quot;Measuring...&amp;quot;);
 
   dht.begin();
}
 
void loop() {
   delay(2000);
 
   float h = dht.readHumidity();
   float t = dht.readTemperature();
 
   if (isnan(h) || isnan(t)) {
      Serial.println(&amp;quot;DHT11 sensor reading failure&amp;quot;);
      return;
   }
 
 
   Serial.print(&amp;quot;Relative humidity: &amp;quot;);
   Serial.print(h);
   Serial.print(&amp;quot; %t&amp;quot;);
   Serial.print(&amp;quot;Temperature: &amp;quot;);
   Serial.print(t);
   Serial.print(&amp;quot; ºC &amp;quot;);
}

Leave a Comment

*

*