Hello dear friends. Hope you will be fine. Today we will learn that how to use a DHT (DHT11 version) Temperature and Humidity Sensor. In this tutorial, you will see the result in the serial monitor of Arduino but later I will write some more tutorial for getting the result into the database and then generate the dynamic chart to show your sensor’s temperature and humidity. So let’ start our first tutorial of this series.
We need the following hardware for this project:
- DHT11, a temperature and humidity sensor.
- Wemos d1 mini or UNO arduino
- Three cables
Again we will be using a Library specifically designed for these sensors that will make our code short and easy to write.
As you can see we only need 3 connections to the sensor since one of the pin is not used.
The connections are : Voltage, Ground, and Signal which can be connected to any Analog Pin on our UNO.
Note: You can use UNO arduino for this project and set the pin as shown below.
THE CODE
You have to download DHT library for this project that is available for this sensor. I will try to make code very short and simple. Download link for DHT library is given below. Once you have the library, just go ahead and extract it to the Library folder inside your Arduino IDE software folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include "DHT.h" #define dht_apin D2 // Analog Pin sensor is connected to dht DHT; void setup(){ Serial.begin(9600); delay(500);//Delay to let system boot Serial.println("DHT11 Humidity & temperature Sensor\n\n"); delay(1000);//Wait before accessing Sensor }//end "setup()" void loop(){ //Start of Program DHT.read11(dht_apin); Serial.print("Current humidity = "); Serial.print(DHT.humidity); Serial.print("% "); Serial.print("temperature = "); Serial.print(DHT.temperature); Serial.println("C "); delay(5000);//Wait 5 seconds before accessing sensor again. //Fastest should be once every two seconds. }// end loop() |
Change pin port on line #define dht_apin D2.
Download
You can download the Library we used here: DHT_Library.zip
Arduino: 1.8.10 (Windows 10), Alaplap:”LOLIN(WEMOS) D1 R2 & mini, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 115200″
sketch_jan22b:1:17: error: DHT.h: No such file or directory
#include “DHT.h”
^
compilation terminated.
exit status 1
DHT.h: No such file or directory
Ennek a jelentésnek több információja lehetne
a “Bővebb kimenet mutatása fordításkor”
opció engedélyezésével a Fájl -> Beállítások-ban.