Arduino Tutorials

DHT11 Temperature and Humidity Sensor With Arduino

DHT11 TEMPERATURE AND HUMIDITY SENSOR WITH ARDUINO

 

In this tutorials, we will measure Temperature and Humidity by using DHT11 with Arduino. Here we also display those readings on an LCD Display.

Read More about DHT11 Sensor

Read More about LCD Display

 

COMPONENTS LIST

  • Arduino Board
  • DHT11 or DHT22
  • Jumper Wires
  • LCD Display
  • 1 x Breadboard

Pin Connection of DHT11

DHT11 Arduino Pins
Vcc +5v 5v
GND GND
Vout (out) A0

CIRCUIT DIAGRAM

Circuit Diagram of DHT11 with Arduino

Library Used in Below Code

DHT Library

CODE 1

#include <dht.h>

#define dht_pin A0     // Analog Pin A0 of Arduino is connected to DHT11 out pin
 
dht DHT;
 
void setup()
{
  Serial.begin(9600);
  delay(500);
  Serial.println("DHT11 Humidity & temperature Sensor\n\n");
  delay(1000);
}
 
void loop()
{
    DHT.read11(dht_pin);
    
    Serial.print("Humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%    ");
    Serial.print("Temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println(" C");
    
    delay(5000); //Reduce Time for Quick Reply 
}

OUTPUT OF CODE 1

OUTPUT OF DHT11 WITH ARDUINO CODE 1

Circuit Diagram of DHT11 with LCD Display

Circuit of DHT11 with LCD Display

Code 2 DHT11 with LCD Display

//LiquidCrystal Library for LCD :- https://github.com/arduino-libraries/LiquidCrystal

#include <dht.h>
#include <LiquidCrystal.h>

#define dht_pin A0     // Analog Pin A0 of Arduino is connected to DHT11 out pin
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;
 
void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  delay(500);
  Serial.println("DHT11 Humidity & temperature Sensor\n\n");
  delay(1000);
}
 
void loop()
{
    DHT.read11(dht_pin);
    
    Serial.print("Humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%    ");
    Serial.print("Temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println(" C");
    lcd.setCursor(0, 0);
    lcd.print("Humidity=");
    lcd.print(DHT.humidity);
    lcd.print("%");
    lcd.setCursor(0, 1);
    lcd.print("Temp = ");
    lcd.print(DHT.temperature);
    lcd.print(" C");    
    delay(5000); //Reduce Time for Quick Reply 
}

Library Used in Below Code

DHT Library
Adafruit Unified Sensor

CODE 2

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN A0        // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() 
{
  Serial.begin(9600); 
  // Initialize device.
  dht.begin();
  Serial.println("DHTxx Unified Sensor Example");
  
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.println("Temperature");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" *C");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" *C");
  Serial.println("------------------------------------");
  
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.println("Humidity");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println("%");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println("%");
  Serial.println("------------------------------------");
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;
}

void loop() 
{
  // Delay between measurements.
  delay(delayMS);
  
  // Get temperature event and print its value.
  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) 
  {
    Serial.println("Error reading temperature!");
  }
  else 
  {
    Serial.print("Temperature: ");
    Serial.print(event.temperature);
    Serial.print(" *C    ");
  }
  
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) 
  {
    Serial.println("Error reading humidity!");
  }
  else 
  {
    Serial.print("Humidity: ");
    Serial.print(event.relative_humidity);
    Serial.println("%");
  }
}

OUTPUT OF CODE 2

OUTPUT OF DHT11 WITH ARDUINO CODE 1

Our Youtube Channel