Arduino - DHT22

In this tutorial, we are going to learn:

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×DHT22 Temperature Humidity Sensor Module
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About DHT22 Temperature and Humidity Sensor

DHT22
Temperature Range -40°C to 80°CW
Temperature Accuracy ± 0.5°C
Humidity Range 0% to 100%
Humidity Accuracy ± 2 to 5%
Reading Rate 0.5Hz (once every 2 seconds)
Operating Voltage3 to 5V

Pinout

DHT22 has two forms: sensor and module.

DHT22 temperature and humidity sensor Pinout

DHT22 sensor has four pins:

  • GND pin: needs to be connected to GND (0V)
  • VCC pin: needs to be connected to VCC (5V, or 3.3V)
  • DATA pin: the pin is used to communicate between the sensor and Arduino
  • NC pin: Not connected, we can ignore this pin

DHT22 module has three pins:

  • GND pin: needs to be connected to GND (0V)
  • VCC pin: needs to be connected to VCC (5V, or 3.3V)
  • DATA pin: the pin is used to communicate between the sensor and Arduino

Some manufacturers provide DHT22 sensor in module form with three pins: GND, VCC and DATA pins (or alternatively: -, +, and OUT pins).

Wiring Diagram

In sensor form, A resistor from 5K to 10K Ohms is required to keep the data line high and in order to enable the communication between the DHT22 sensor and the Arduino

Arduino - DHT22 Sensor Wiring

Arduino DHT22 Temperature and humidity Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino - DHT22 Module Wiring

Most of DHT22 sensor modules have a built-in resistor, so you don't need to add it. it saves us some wiring or soldering works.

Arduino DHT22 Temperature and humidity Module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How To Program For DHT22 Temperature Sensor

Programming for both sensors is similar. There is only one line of code different.

  • Include the library:
#include "DHT.h"
  • Define the Arduino pin connected to DHT sensor:
#define DHT22_PIN 2
  • Declare DHT22 object
DHT dht22(DHT22_PIN, DHT22);
  • Initialize the sensor:
dht22.begin();
  • Read humidity:
float humi = dht22.readHumidity();
  • Read temperature in Celsius:
float tempC = dht22.readTemperature();
  • Read temperature in Fahrenheit:
float tempF = dht22.readTemperature(true);

Arduino Code - DHT22

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dht22 */ #include "DHT.h" #define DHT22_PIN 2 DHT dht22(DHT22_PIN, DHT22); void setup() { Serial.begin(9600); dht22.begin(); // initialize the DHT22 sensor } void loop() { // wait a few seconds between measurements. delay(2000); // read humidity float humi = dht22.readHumidity(); // read temperature as Celsius float tempC = dht22.readTemperature(); // read temperature as Fahrenheit float tempF = dht22.readTemperature(true); // check if any reads failed if (isnan(humi) || isnan(tempC) || isnan(tempF)) { Serial.println("Failed to read from DHT22 sensor!"); } else { Serial.print("DHT22# Humidity: "); Serial.print(humi); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); } }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “DHT”, then find the DHT sensor library by Adafruit
  • Click Install button to install the library.
Arduino DHT sensor library
  • You will be asked for intalling some other library dependencies
  • Click Install All button to install all library dependencies.
Arduino Adafruit Unified sensor library
  • Copy the above code corresponding to the sensor you have and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Make enviroment around sensor hotter or colder
  • See the result on Serial Monitor.
COM6
Send
DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F DHT22# Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F DHT22# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES