How to connect DHT11 to Arduino and display data

When creating projects where you need to know the humidity and temperature, you need an appropriate sensor. With a large assortment, the eyes on the market are scattered, and you do not know what to choose. However, there are proven sensors that are already used by thousands of ham enthusiasts. One such is DHT11. This article will discuss the features of connecting DHT11 to Arduino.

use of I2C with DHT11 and Arduino

About DHT11

This sensor includes a hygrometer and a thermometer, which gives him the opportunity to know the temperature and humidity:

  • As for the accuracy of the device, then everything is in order, given its low price. The percentage of maximum deviations is only 5%.
  • Measures temperature in the range from 0 ° to +50 ° . As you can see, in the cold weather you can’t work with this sensor, unfortunately.
  • As for the hygrometer inside the DHT11, it is also not bad, it measures humidity in the range from 20 to 80%.
  • The speed is average, you can send no more than 1 request per second. A lot or a little depends on the project. In most cases, this sensor is used for training or for small projects, for example, measuring temperature in an automatic greenhouse. There is no need to measure temperature and humidity faster than 1 time per second, rather, on the contrary, you need to take care of energy saving so that your project works as much as possible from a portable power source.
  • When requested, DHT11 consumes 2.5 mA.
DHT11 sensor for measuring temperature and humidity

Where to get

You can buy the described device in Aliexpress at a very low price - on average, 50 rubles. It is mainly sold always with wires for connecting the DHT11 to the Arduino. But buying a sensor is only from trusted sellers who have positive reviews, as it happens that the device does not work correctly.

By the way, connecting DHT11 to Arduino with an analog input is not used, since the sensor is designed in such a way that it converts the values ​​from analog to digital, so that it is more convenient to work.

Work with DHT11

The sensor is on sale in 2 versions:

  1. Pure sensor. When buying such a version, you will have to connect it through a resistor, which causes some inconvenience during operation. It has 4 pins, but only 3 are used.
  2. Module. On the module 3 contacts, a resistor is not required when connecting, as it is already on the module. It is preferable to take this option, since the price is not different, and working with the module is much more convenient.

As already mentioned, the sensor has 3 contacts: ground, power, logic (data output). To work and connect DHT11 to Arduino, you need:

  • Install a library that allows operations with this sensor. You can download it on the Internet, and to install, you need to transfer the contents of the archive to the Libraries folder in the Arduino root folder.
  • We connect the power to 5V in Arduino, GND-ground and the third contact - data output - we connect to some digital pin in Arduino, for example, 2.
  • After connecting DHT11 to Arduino, you need to fill in the program code in the board. Code example:
#include "DHT.h" #define DHTPIN 2 //  ,     //   DHT dht(DHTPIN, DHT11); void setup() { Serial.begin(9600); dht.begin(); } void loop() { //  2    delay(2000); //  float h = dht.readHumidity(); //   float t = dht.readTemperature(); //     . if (isnan(h) || isnan(t)) { Serial.println("   "); return; } Serial.print(": "+h+" %\t"+": "+t+" *C "); } 

What does the code do:

  • First of all, we connect the library to work with Arduino.
  • Next, we designate the contact to which the DHT11 is connected and tell the program which sensor and which contact is located.
  • We begin to read the values, we find out whether the reading was successful. If there are no problems, the humidity and temperature readings displayed by the sensor appear on the port monitor.
Connect DHT11 to Arduino

Display output

Displaying a port on a monitor is not very interesting, let's look at connecting a DHT11 to an Arduino with output to an LCD 1602 I2C.

This is a small display with an I2C card to make working with it more convenient. The display also requires a library that can be downloaded on the Internet.

I2C is connected to Arduino as follows: ground and power, and SDA and SCL on I2C are connected to Arduino on pins A4 and A5, respectively. Then we load the program code into Arduino:

 #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); #include <dht11.h> dht11 sensor; #define DHT11PIN 2 byte degree[8] = //    { B00111, B00101, B00111, B00000, B00000, B00000, B00000, }; void setup() { lcd.init(); lcd.backlight(); lcd.createChar(1, degree); //     1 } void loop() { int chk = sensor.read(DHT11PIN); lcd.setCursor(0, 0); lcd.print("Hum: %"); lcd.setCursor(11, 0); lcd.print(sensor.humidity); lcd.setCursor(0, 1); lcd.print("temp: 1C"); lcd.setCursor(11, 1); lcd.print(sensor.temperature); delay(2000); } 

Again, the code is very simple:

  • First, libraries are connected to work with the display and the DHT11 sensor.
  • Then we encode the degree symbol so that the display can display it.
  • Then we read the values ​​from the sensor and transfer them to the display.

As a result, using the display and DHT11 we get a device that displays the humidity and temperature of our room!

Source: https://habr.com/ru/post/F6853/


All Articles