DHT22 – Temperature and humidity sensor

DHT22 – digital temperature and humidity sensor with its pin numbers.
Fig.: DHT22 – digital temperature and humidity sensor with its pin numbers.

Technical Data

Operating voltage DC 3.3 to 5.5V
Humidity range 0% to 100% relative humidity
Humidity accuracy ±2% RH
Temperature range -40°C to +80°C
Temperature accuracy ±0.5°C
Signal output Single-bus, bidirectional serial data

📄 DHT22 datasheet (396 kB)

Connections

DHT22-Pin Function Arduino Uno
1 Vdd (3.3V - 5.5V) 3.3V or 5V
2 SDA (Serial Data) D2 (or other digital Pin)
3 -not connected- -
4 GND (Ground) GND

Used Components

Setup

According to the datasheet for the DHT22, a pull-up resistor of 5.1kΩ must be used on the data pin. Here, a 4.7kΩ resistor is used, as it is more common and possibly more readily available.
In some cases, this external pull-up resistor can be omitted if the library used below is utilized. This library makes use of the Arduino's internal pull-up and delivers the same results even without the external pull-up.

Circuit setup with a DHT22 on an Arduino Uno
Fig.: Circuit setup with a DHT22 on an Arduino Uno

Programming

For simple control and programming of the DHT22, the following two libraries are used:
Adafruit DHT-Sensor and
Adafruit Unified Sensor.

#include "DHT.h"

#define DHTTYPE DHT22
#define PIN_DHT22 2

DHT dht(PIN_DHT22, DHTTYPE);
float humidity, temperature;

void setup() {
    Serial.begin(9600);
    dht.begin();
}

void loop() {
    humidity = dht.readHumidity();
    temperature = dht.readTemperature();

    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.print("%, Temperature: ");
    Serial.print(temperature);
    Serial.println("°C");

    delay(1000);
}
Output of the measured values on the serial console of the Arduino IDE
Fig.: Output of the measured values on the serial console of the Arduino IDE

Alternative

The second programm essentially does the same thing, with the difference that the measured values are sent to the serial interface separated by spaces. This allows the use of the serial plotter in the Arduino IDE as a graphical output of the measurements.

#include "DHT.h"

#define DHTTYPE DHT22
#define PIN_DHT22 2

DHT dht(PIN_DHT22, DHTTYPE);
float humidity, temperature;

void setup() {
    Serial.begin(9600);
    dht.begin();
}

void loop() {
    humidity = dht.readHumidity();
    temperature = dht.readTemperature();

    Serial.print(humidity);
    Serial.print(" ");
    Serial.println(temperature);

    delay(1000);
}
Output of the measured values with the serial plotter (blue = Humidity; red = Temperature)
Fig.: Output of the measured values with the serial plotter (blue = Humidity; red = Temperature)
Last edited by Christian Grieger on 2025-05-12
X
  1. [top]
  2. Technical Data
  3. Connections
  4. Used Components
  5. Setup
  6. Programming