Arduino Nano - DHT22

This tutorial instructs you how to use Arduino Nano to read the temperature and humidity from DHT22 sensor. In detail, we will learn:

We suggest:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×DHT22 Temperature Humidity Sensor Module
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of DHT22 Temperature and Humidity Sensor

The DHT22 Temperature and Humidity Sensor Pinout

The DHT22 on the market comes with two forms: sensor and module.

The DHT22 sensor has four pins:

  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to VCC (5V)
  • DATA pin: used for communication between the sensor and Arduino Nano
  • NC pin: not necessary, this pin can be disregarded
DHT22 temperature and humidity sensor pinout

The DHT22 module has three pins:

  • GND pin (-): must be connected to GND (0V)
  • VCC pin (+): must be connected to VCC (5V)
  • OUT pin: used for communication between the sensor and Arduino Nano

※ NOTE THAT:

The arrangement of pins on a module may differ from one manufacturer to another. It is essential to always refer to the labels printed on the module when using it. Take a close look!

Wiring Diagram

Arduino Nano - DHT22 Sensor Wiring

A resistor with a resistance between 5K and 10K Ohms is necessary to keep the data line high, thus enabling communication between the sensor and the Arduino Nano.

The wiring diagram between Arduino Nano and DHT22 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano - DHT22 Module Wiring

The DHT22 sensor module comes with a built-in resistor, eliminating the need for extra wiring or soldering. This saves us time and effort.

The wiring diagram between Arduino Nano and DHT22 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

How To Program For DHT22 Temperature Sensor

  • The first step is to include the library:
#include "DHT.h"
  • Specify the Arduino Nano pin that is connected to the DHT22 sensor.
#define DHT_PIN 3 // Arduino Nano pin connected to DHT22 sensor
  • Specify the type of sensor: DHT22
#define DHT_TYPE DHT22
  • Create a DHT object.
DHT dht22(DHT_PIN, DHT_TYPE);
  • Begin the sensor setup process:
dht22.begin();
  • Read the humidity value.
float humi = dht22.readHumidity();
  • Read the temperature in Celsius.
float temperature_C = dht22.readTemperature();
  • Read the temperature in Fahrenheit.
float temperature_F = dht22.readTemperature(true);

Arduino Nano Code for DHT22

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-dht22 */ #include "DHT.h" #define DHT_PIN 3 // The Arduino Nano pin connected to DHT22 sensor #define DHT_TYPE DHT22 DHT dht22(DHT_PIN, DHT_TYPE); void setup() { Serial.begin(9600); dht22.begin(); // initialize the sensor } void loop() { // wait a few seconds between measurements. delay(2000); // read humidity float humi = dht22.readHumidity(); // read temperature as Celsius float temperature_C = dht22.readTemperature(); // read temperature as Fahrenheit float temperature_F = dht22.readTemperature(true); // check if any reads failed if (isnan(humi) || isnan(temperature_C) || isnan(temperature_F)) { Serial.println("Failed to read from DHT sensor!"); } else { Serial.print("Humidity: "); Serial.print(humi); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(temperature_C); Serial.print("°C ~ "); Serial.print(temperature_F); Serial.println("°F"); } }

Detailed Instructions

  • Connect a USB cable to the Arduino Nano and the PC.
  • Open the Arduino IDE, select the appropriate board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “DHT” and locate the DHT sensor library by Adafruit.
  • Press the Install button to install the library.
Arduino Nano DHT22 sensor library
  • You will be prompted to install some other library dependencies.
  • To install all of them, simply click the Install All button.
Arduino Nano Adafruit Unified sensor library
  • Copy the code for your sensor and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to compile and upload the code to the Arduino Nano.
  • Change the temperature of the environment around the sensor.
  • Check the results on the Serial Monitor.
COM6
Send
Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Additional Knowledge

Let's compare DHT11 and DHT22 sensors.

The commons between DHT11 and DHT22

  • Pinouts remain the same.
  • The wiring to an Arduino Nano is identical.
  • Programming, with the help of a library, is comparable, with only one line of code being distinct.

The differences between DHT11 and DHT22

DHT11 DHT22
Price ultra low cost low cost
Temperature Range 0°C to 50°C -40°C to 80°C
Temperature Accuracy ± 2°C ± 0.5°C
Humidity Range 20% to 80% 0% to 100%
Humidity Accuracy 5% ± 2 to 5%
Reading Rate 1Hz (once every second) 0.5Hz (once every 2 seconds)
Body size 15.5mm x 12mm x 5.5mm 15.1mm x 25mm x 7.7mm

It is evident that the DHT22 is more precise than the DHT11, has a wider range, yet is more costly.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!