kr_alexander
Published © GPL3+

Reading DHT22 (or DHT11) with Arduino

Didn't find a working example of how to read the data of DHT22. So here is what I came up with

BeginnerFull instructions provided980
Reading DHT22 (or DHT11) with Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Through Hole Resistor, 4.7 kohm
Through Hole Resistor, 4.7 kohm
You need 5 v for the data line of the sensor, but have to run it through a 4,7k-10k pull-up resistor.
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Schematics

DHT22 wirings to arduino

Here are the wirings you need to do for the DHT22. Program that I used for the schematic, didn't have a DHT22 so I used an other temperature sensor to represent it. It differs by only having 3 legs, but you are anyway using only 3 legs from the DHT22 (or DHT11). From left to right (from the textured side):
1. Vcc, connect to 5 v.
2. Data line, connect to digital pin 12.
3. Empty, not used for anything.
4. Ground.

Code

Code for Arduino Uno to read DHT22

C/C++
Code for Arduino Uno to read DHT22 (or DHT11). The values get written to Serial monitor. Other explanations in the code.

Upload the code to your Arduino and then open the Serial Monitor (Windows 10: CTRL + SHIFT +M). Here you see the values of the sensor.
#include<DHT.h>      // Including library for dht

#define dht_dpin 12       //digital pin, that DHT's data line is connected
#define DHTTYPE DHT22     //When using DHT11, put here DHT11 instead of DHT22
DHT dht(12,DHT22);        //Not sure, what this line excatly does, but same aplies here as above

//int temp;               //Use for DHT11 instead of float
float temp;               //Use float for showing decimals of the temperature reading. I recommend using for DHT22, no point to use for DHT11

//int hum;                //Use for DHT11 instead of float
float hum;                //Use float for showing decimals of the humidity reading. I recommend using for DHT22, no point to use for DHT11

void setup() {
  
Serial. begin(9600);      //Initiate serial monitor
 dht.begin();             //Initiate DHT sensor
 
}

void loop() {

//delay(1000);                          //wait a sec (recommended for DHT11)  
delay(500);                           //wait a 0,5 sec (recommended for DHT22)

temp=dht.readTemperature(false);       //Read temperature of DHT and store it in to variable (temp). FALSE reads in celsius, leave empty for farenheit
hum=dht.readHumidity();               //Read humidity of DHTand store it iin variable (hum).

Serial.print("Temperature: ");        //Print text "Temperature: " in to serial monitor
Serial.println(temp);                 //Print variable (temperature value) in to serial port. ln for line break

Serial.print("Humidity: ");           //Print text "Humidity: " in to serial monitor
Serial.println(hum);                  //Print variable (temperature value) in to serial port. ln for line break
Serial.println(" ");                  //print empty line in to serial monitor
    
  delay(2500);                        //optional delay, not really any point reading the sensor more than once every 3 seconds
}

Credits

kr_alexander
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.