Arduino foundation introduction 13 temperature and humidity sensor DHT11

Posted by levidyllan on Sun, 31 Oct 2021 08:02:07 +0100

One hardware

        DHT11 temperature and humidity sensor has accuracy of ± 5%RH and temperature of ± 2 ℃, as shown in the figure below [1]

        The wiring of the four pins is clear at a glance. No. 2 data is used for the communication between the sensor and the microcontroller. It is connected to a digital pin of UNO, and No. 3 NC is empty. At present, I only know the surface function and connection mode of the newly introduced hardware, but I don't understand some of the internal mechanisms of the hardware. For example, when you search DHT11, you find that other bloggers write communication process and high-low level sequence diagram [2, 3], which you can't understand!

        In addition, refer to [3], which is to connect No. 2 Data external pull-up resistor to ensure that the bus is at high level when idle. The wiring is shown in the figure below

  I connect pin 2 directly to the UNO digital pin. There is no problem at present, so it will not be changed. The figure above is reserved for future reference.

Two codes

1. The function is to display the temperature and humidity data read by DHT11 on LCD1602, and the setting and wiring of LCD1602 are consistent with the previous content [4]

2. The specific code, in which the flashing and scrolling out of the screen refer to [ 5 ]

//The LCD displays the value read by the temperature and humidity sensor DHT11
//LCD character flashing and scrolling
#Include < dht. H > / / declare to call the dht Library
dht DHT;//Create an object
#define DHT_ Pin 9 / / digital pin connected to DHT11
#Include < liquidcrystal. H > / / declare to call the relevant library of LCD
const int rs=12,en=11,d4=5,d5=4,d6=3,d7=2;//LCD pin
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);//Create an instance (object) called lcd. Here is the 4-wire method
void setup() {
  lcd.begin(16,2);//Initialize the width and height of LCD, set 16 columns cols and 2 rows, column first and row later  

void loop() {
  // Display "Rick,What's the weather like?", flash three times and disappear, and then display temperature and humidity

    DHT.read11(DHT_pin);//Read from DHT sensor pin
    lcd.print("Rick, What's the");//
    lcd.setCursor(0,1);//Set the cursor on row 1 of column 0 (beginning of row 2)
    lcd.print("weather like?");
    //The character flashes 3 times
    for(int i=0;i<3;i++){

    //The question disappears and displays humidity and temperature again

      lcd.clear();//Clear the screen and return the cursor to the upper left corner
      lcd.print("Humidity(%):");//12 characters in total
      lcd.setCursor(12,0);//The cursor is positioned after the above characters, that is, line 1 and column 13
      lcd.print(DHT.humidity,1);//Display the collected humidity value and keep 1 digit after the decimal point
      lcd.setCursor(0,1);//The cursor is positioned at column 1 and row 2
      lcd.print("Temp(C):");//8 characters
      lcd.setCursor(8,1);//Position the cursor to column 9, row 2
      lcd.print(DHT.temperature,1);Display the collected temperature value and keep 1 digit after the decimal point

    //Scroll left out
     for(int j=0;j<16;j++){
        delay(500);//This delay time can adjust the rolling speed. The smaller the delay time, the faster the rolling

3. Knowledge points

1) For the use of DHT11, first load the dht.h library. There is a DHT folder in the source code sent by the seller, which contains two files dht.h and dht.cpp. I copy this folder into \ libraries and combine #include statements to load the library.

2) Through the search, we found that DHT11 is not the only dht.h library, but also dht11.h. If the latter library is referenced, the statements for reading data in the code are also different. There are two kinds of Libraries in different places in the data sent by the seller to me, which makes me wonder where I was wrong. My reference library is #include < dht.h >   The following statement should be the dht object. read11(pin), and the result of some web pages is the object. read(). The reason is that the libraries referenced at the beginning of the code are different. It is estimated that only a white man like me will be dizzy about this problem for a long time! In addition, you can write your own dht header file and cpp file, which is more convenient for customization, but it's a little far from me.

3) After having the read11() statement, write the object. Temperature object. Humidity to get the temperature and humidity values. I also tried not to write read11(), directly object. Temperature object. Humidity, and the result has always been 0. Alas, I won't look up this library file anyway. I feel it's a little strange to specify this form. Why not get the values directly with the read11() statement? Just like UNO's digitalRead () statement, do you want to write it separately or use three sentences of code!? In the future, I can change the library files myself. I'll try customization! I wonder if this can be changed?

4) The preliminary display function of LCD screen is written in [4], so this time we will make the display effect more. Of course, we need to find out what functions and corresponding functions are in the LIquidCrystal library. There are still many references [6, 7]. I have marked the specific functions in the code. At present, I just copy them. The problem is the same as before. If this library function is not so common and easy to search, can I know what functions and functions there are by viewing the library file? It should be OK, but I still can't understand header files and cpp files.

The final results are shown in the figure below

The video and motion pictures are too large, so they won't be transmitted. Another problem is that the symbol of Celsius is not fully marked.

 [1]How DHT11 and dht22 temperature and humidity sensors work with Arduino and their interfaces - know

[2]DHT11 temperature and humidity sensor (detailed)_ Be a crazy programmer blog - CSDN blog_ dht11

[3]Arduino experiment - DHT11 reading temperature and humidity_ Mr. R feels bad all day without study - CSDN blog

[4]Arduino foundation introduction 12 LCD1602A preliminary display_ u013978070 column - CSDN blog

[5]Arduino learning notes: run LCD1602 based on LiquidCrystal Library_ Echobox blog - CSDN blog

[6]LiquidCrystal library function - Dumblidor - blog Garden

[7]Arduino - LiquidCrystal

Topics: Single-Chip Microcomputer arduino