DHT11 is a temperature and humidity sensor with a calibrated digital signal output. In this example it is used the DHT11 to print on the Serial port the values of temperature in Celsius degrees and humidity of the environment monitored.

Hardware Required

Wiring

Schematic

DHT11 exmple schematic

Code

Connect the board to PC and upload the below code using the Arduino IDE.
The sketch includes the library DHT.h. You can download it here.
The code defines the pin 3, used to connect the sensor, as DHTPIN and also it defines the sensor used( in this case DHT11).
In the setup it is initialized the Serial communication and it is created a table with two column: T(C) to insert the temperature in Celsius degrees and H(%)to insert the humidity.
The loop checks the values read by sensor and it prints them on Serial Monitor.

The complete code and its detailed description are shown down.

// DHT11 Temperature and Humidity Sensors Example
#include "DHT.h"         //include DHT library
#define DHTPIN 3         //define as DHTPIN the Pin 3 used to connect the Sensor
#define DHTTYPE DHT11    //define the sensor used(DHT11)
DHT dht(DHTPIN, DHTTYPE);//create an instance of DHT
/*setup*/
void setup() {
  Serial.begin(9600);    //initialize the Serial communication
  delay(6000);           //wait 6 seconds
  Serial.println("Temperature and Humidity test!");//print on Serial monitor
  Serial.println("T(C) \tH(%)");                   //print on Serial monitor
  dht.begin();           //initialize the Serial communication
}
/*loop*/
void loop() {
 
  float h = dht.readHumidity();    // reading Humidity 
  float t = dht.readTemperature(); // read Temperature as Celsius (the default)
  // check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t)) {    
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print(t, 2);    //print the temperature
  Serial.print("\t");
  Serial.println(h, 2);  //print the humidity
  delay(2000);           //wait 2 seconds
}

Click the Serial Monitor button and you will see the program running result like this:

 dht11 running result

You should see the temperature and humidity. You can see changes by breathing onto the sensor (like you would to fog up a window) which should increase the humidity.

 Reference:

1. DHT11 basic temperature-humidity sensor: http://osoyoo.com/2017/05/04/dht11-basic-temperature-humidity-sensor/

2. Download the DHT11 library here: http://osoyoo.com/wp-content/uploads/samplecode/DHT.zip Unzip and put it in the libraries file of Arduino IDE by the path: ..\arduino-x.x.x\libraries,then use the Arduino IDE to start your first DHT11 project. [library]

3. DHT11 datasheet: http://osoyoo.com/driver/DHT11-datasheet.pdf [datasheet]

4.” A DHT11 Class for Arduino” on arduino.cc [library & simple examples]

5. Use NodeMCU to send temperature/humidity data to MQTT IOT broker [project]

6. Use the DHT Temperature Sensors in C# code via a C++ Windows Runtime Component to implement a one-wire protocol on Windows 10 and the RPI2.[project]

Leave a Reply

WordPress spam blocked by CleanTalk.
Previous Post
«
Bombax Theme designed by itx