ElectroPeak
Published © CC BY

Beginner’s Guide to Use DHT11/DHT22 Sensors w/ Arduino

In this tutorial, you will learn how to set up DHT11 and DHT22 sensors, and measure the environment temperature and humidity.

BeginnerProtip1 hour3,285
Beginner’s Guide to Use DHT11/DHT22 Sensors w/ Arduino

Things used in this project

Hardware components

Arduino Uno R3
×1
ElectroPeak DHT11 Sensor
×1
ElectroPeak DHT22 Sensor
×1
Breadboard
×1
ElectroPeak Jumper wire
×1

Software apps and online services

Arduino IDE

Story

Read more

Schematics

DHT Sensor Library

Necessary Files and Downloads

Code

code 1

Arduino
/*

DHT11 
Tempertature and Hummidity 

modified on 7 Feb 2019
by Saeed Hosseini @ Electropeak
https://electropeak.com/learn/guides/

*/
#include "dht.h"

conat int dht_pin = 8;
 
dht DHT;
 
void setup(){
 
  Serial.begin(9600);
  delay(500);
  Serial.print("***Electropeak***\n\n");
 
}
 
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(3000);//We have to wait at least 2 seconds before accessing the sensor again.
}

}

code 2

Arduino
/*
DHT22 
Tempertature and Hummidity 
modified on 7 Feb 2019
by Saeed Hosseini @ Electropeak
https://electropeak.com/learn/guides/
*/
#include 
const int dataPin = 8; 
dht DHT; 
void setup() {
  Serial.begin(9600);
}
void loop() {
  int readData = DHT.read22(dataPin); 

  float t = DHT.temperature; 
  float h = DHT.humidity; 
  
  Serial.print("Temperature = ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("    Humidity = ");
  Serial.print(h);
  Serial.println(" % ");
  
  delay(2000); 
}

Credits

ElectroPeak

ElectroPeak

57 projects • 730 followers
At ElectroPeak we want to teach you to enjoy electronics more. We offer Top-notch guides and worry-free shopping experience.

Comments