Codey OnlineArduino & ESP32 component library › DHT22
DHT22
Sensors

DHT22

The DHT22 is a digital temperature and relative humidity sensor used with microcontrollers. It communicates over a single-wire data interface and is commonly wired with VCC, GND, and a DATA line.

Width: 17.7 mm

Pinout — DHT22

Pin Signal Description
GND ground Ground reference for the DHT22 module power and data signal.
VCC power Positive supply input for the DHT22 module, typically 3.3V to 5V.
DATA data Single-wire digital data output from the DHT22 temperature and humidity sensor.

Example wiring diagram — DHT22

From To Wire
board:5V x1:VCC red
board:GND x1:GND black
board:D2 x1:DATA green

Example Arduino code — DHT22

This example was written and compile-checked for the arduino-uno. Codey Online adapts it to any other supported board for you.

#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  dht.begin();
  Serial.println("DHT22 sensor example");
}

void loop() {
  delay(2000); // DHT22 needs a slow sampling rate

  float humidity = dht.readHumidity();
  float temperatureC = dht.readTemperature();

  if (isnan(humidity) || isnan(temperatureC)) {
    Serial.println("Failed to read from DHT22 sensor");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\tTemperature: ");
  Serial.print(temperatureC);
  Serial.println(" °C");
}

Related components

18650 Battery Shield with USB-C Charger and 5V Boost Converter

18650 Battery Shield with USB-C Charger and 5V Boost Converter

2.4 inch OLED Display I2C 3.3V

2.4 inch OLED Display I2C 3.3V

3.5 inch TFT LCD Shield

3.5 inch TFT LCD Shield

50kg Load Cell Weight Sensor

50kg Load Cell Weight Sensor

Build your Arduino or ESP32 project with AI

Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.

Start for free