2.32 DHT11 Module¶
In this lesson, you will learn how to use DHT11 Module. The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins are needed).
In this example, we can directly connect the pins of DHT11 Module to the pins of Mega 2560 Board, and we use pin 4 to read the signal of DHT11 Module. Connect the pin「+」of DHT11 Module to 5V, the pin「-」 to GND, and the pin OUT to pin 4.
You can open the file
2.32_dhtModule.inounder the path of
Or copy this code into Arduino IDE 1/2.
Then Upload the Code to the board.
Please make sure you have added the library called
dht, detailed tutorials refer to Add Libraries.
After the codes are uploaded to the Mega2560 board, the serial monitor will continue to output the current temperature and humidity values of the environment.
The function of the module is included in the library dht.h.
Creates a new instance of the dht class that represents a particular DHT-11 module attached to your Arduino board.
int read11(uint8_t pin)
This function will return CHECK values.
DHTLIB_OKmeans that DHT-11 is in good condition;
DHTLIB_ERROR_CHECKSUMrepresents that the value may be abnormal;
DHTLIB_ERROR_TIMEOUTindicates that there is timeout.
The function will store the detected humidity and temperature into the variables with the same name in dht class.
The function should be called and used directly in the main program. (e.g. Serial.println(DHT.temperature,1);)