Arduino DHT22 Tutorial: Measure Temperature and Humidity

Logan Aug 28, 2026

Arduino, a popular open-source electronic prototyping platform, is renowned for its simplicity and versatility, enabling hobbyists and professionals alike to хү to life their creative ideas. If you're interested in monitoring temperature and humidity, the DHT22 sensor is a staple in the Arduino community. This article guides you through integrating a DHT22 sensor with your Arduino board, ensuring you reap the benefits of precise environmental data.

an electronic device is connected to two wires and plugged into the other side of the board
an electronic device is connected to two wires and plugged into the other side of the board

The DHT22 is a reliable, low-cost humidity and temperature sensor, boasting a high precision of ±2% for RH (Relative Humidity) and ±0.5°C for temperature. To intertwine this sensor with your Arduino board, you'll need a basic understanding of electronics and programming. Let's dive into the details of this integration, exploring essential components, wiring techniques, and coding strategies.

an electronic device with a clock on it and wires coming out of the back ground
an electronic device with a clock on it and wires coming out of the back ground

Understanding the DHT22 Sensor

The DHT22 is a capacitive digital sensor, implying it has a digital output that makes interface with microcontrollers like Arduino straightforward. It comes with a built-in Dickinson to mitigate the impact of moisture on the sensing element, enhancing its longevity and performance.

an electronic device with a white background
an electronic device with a white background

Under the hood, the DHT22 employs a humidity sensing element and a temperature-sensing element. It transmits data in a 40-bit packet, encapsulating a quick check code, humidity data, and temperature data. This feature enables easy data verification, ensuring the received data is correct and consistent.

DHT22 Pinout and Wiring

Sensor de temperatura e umidade DHT22 (AM2302) - Arduino e Cia
Sensor de temperatura e umidade DHT22 (AM2302) - Arduino e Cia

Wiring the DHT22 to your Arduino board is a seamless process, given the sensor's four-pin layout. The pins consist of VCC (Power), GND (Ground), DATA (Output), and a No Connection (NC) pin that remains unutilized during typical operations.

To wire the DHT22, connect the VCC and GND pins to the 3.3V and GND pins on your Arduino board respectively. The DATA pin should connect to an Arduino digital pin, with Pin 2 and Pin 4 being the typical choices due to their internal PUR submersion.

DHT22 Libraries for Arduino

DHT22(AM2302) Pinout
DHT22(AM2302) Pinout

The Arduino Integrated Development Environment (IDE) hosts several libraries dedicated to the DHT22 sensor, simplifying your programming journey. The/zarduino/DHT library is our recommended choice, thanks to its extensive features and community support.

To install the DHT library, navigate to Sketch -> Include Library -> Manage Libraries, then search for 'DHT' and install. Once in your Arduino IDE, include the library with '(DHTPIN, DHTTYPE);' in your setup function, ensuring the DHTPIN and DHTTYPE are correctly defined according to your hardware setup.

Coding the DHT22 with Arduino

Arduino - DHT22 - LCD | Arduino Tutorial
Arduino - DHT22 - LCD | Arduino Tutorial

Post-library installation, proceed to write your Sketch, i.e., your Arduino program. Since we're utilizing the DHT22, obtaining temperature and humidity readings, then outputting them, is the primary goal.

Below is a concise example of how your Arduino code might appear, utilizing the DHT library mentioned earlier:

Arduino based Temperature and Humidity/Humidex meter with DHT22 sensor and color LCD | electronicsblog.net
Arduino based Temperature and Humidity/Humidex meter with DHT22 sensor and color LCD | electronicsblog.net
Sensor de temperatura dht22
Sensor de temperatura dht22
Arduino Humidity Sensor using the DHT22
Arduino Humidity Sensor using the DHT22
Excel datalogger using Arduino and DHT22 sensor
Excel datalogger using Arduino and DHT22 sensor
High Precision AM2302 DHT22 Digital Temperature & Humidity Sensor Module For arduino Uno R3
High Precision AM2302 DHT22 Digital Temperature & Humidity Sensor Module For arduino Uno R3
How to use DHT11 and DHT22 Sensors with Arduino
How to use DHT11 and DHT22 Sensors with Arduino
DHT11/DHT22 Sensor with Arduino | Random Nerd Tutorials
DHT11/DHT22 Sensor with Arduino | Random Nerd Tutorials
an electronic device with the words dht - 22 temperature and humidity sensor
an electronic device with the words dht - 22 temperature and humidity sensor
Medir temperatura y humedad con Arduino y sensor DHT11-DHT22
Medir temperatura y humedad con Arduino y sensor DHT11-DHT22

```cpp #include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { delay(2000); float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } float f = dht.readTemperature(true); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\tTemperature: "); Serial.print(t); Serial.print(" °C "); Serial.print(f); Serial.println(" °F\n"); } ```

Obtaining and Displaying Data

In this code, the 'dht.begin()' function initializes the sensor. The 'dht.readHumidity()' and 'dht.readTemperature()' functions fetch the current humidity and temperature data, storing it in the 'h' and 't' variables respectively. A simple '/n' Serial print command contributes to a neat and readable display of data in your serial monitor.

Regardless of your project's scope and complexity, understanding these code fragments will prove invaluable in integrating the DHT22 sensor with your Arduino board. Now that you're equipped with essential knowledge, you're prepared to expand your skills and implement the DHT22 sensor in more intricate Arduino projects.

Remember, the digital age thrives on innovation and continuous learning. Explore, experiment, and improve your skills to unlock the boundless potential of Arduino and the DHT22 sensor. Happy making! With these insights, you're well on your way to harnessing the power of the DHT22 for precise temperature and humidity measurements in your next Arduino project.