DHT11 tells you the weather 🌡️

Updated: May 12, 2020



Know it

DHT11 is a temperature and humidity sensor so it checks the surrounding area and gives you the results on the Arduino interface or LCD display, depending where you’d like to display the output.



Wire it

Now let's connect the sensor and see how hot will this summer be.


For that, you need the items below:

  • Jumper Wires

  • DHT11 temperature and humidity sensor

  • Arduino board and USB connector


Connections:

  • Arduino pin GND DHT11 1st pin from right

  • Arduino pin 5V DHT11 1st pin from left

  • Arduino pin 2 DHT11 Middle pin



Code it

This is a simple example sketch that reads the temperature and humidity values from the DHT11 sensor, and displays it on the Serial Monitor.


Try it out! Copy the code below:

/*  

Measuring the Temperature and Humidity using DHT11
  
Tutorial link: https://www.learn.voltaat.com/post/dht11
  
This is an Arduino sketch that reads the temperature and humidity 
values from the DHT11 sensor, and displays it on the Serial Monitor.
  
Components Needed:
1. DHT11 Temperature and humidity sensor ....x1

Connections: 
- Arduino pin GND → DHT11 sensor GND pin 
- Arduino pin 5V  → DHT11 sensor VCC pin 
- Arduino pin 2   → DHT11 sensor middle pin
  
Libraries Needed:
1. DHT.h - Download link: 

How to install/use library:

*/

#include "DHT.h"         //include this library in the sketch
#define DHTTYPE DHT11 // The type of DHT sensor we are using 
DHT dht(DHTPIN, DHTTYPE);      // initialize dht sensor

float hum, temp;   //variables to read sensor readings

int DHTPIN = 2;   // The pin that DHT11 is connected to: Pin 2

// This routine run once
void setup()
{
  Serial.begin(9600); 
  Serial.println(F("DHT11 sensor Starting!"));   //write this text on the monitor
  dht.begin();         //Start the DHT1 Sensor
}

// This routine loops forever
void loop()
{
  delay(2000);    // Wait a two seconds between measurements
  hum =   dht.readHumidity();     // Read humidity value
  temp =  dht.readTemperature();  // Read temperature value as Celsius (the default)
  
  if (isnan(hum) || isnan(temp) || isnan(tempF))  // Check if any reads failed and exit early (to try again)
  {
    Serial.println("Failed to read from DHT sensor!");  //if failed to read then write that text on monitor
    
    return; //and start again the loop
  }
  
  Serial.print("Humidity: ");
  Serial.print(hum);    //display humidity value
  Serial.println("%  Temperature: ");
  Serial.print(temp);    //display temperature value
  Serial.println("°C ");
}