CNAP

DHT22 Reference

A digital temperature and humidity sensor communicating over a single-wire protocol.

Pin names

NameDescription
VCCPower (3.3V–5V)
SDAData — single-wire digital output, connect to a GPIO
NCNot connected
GNDGround

Attributes

This component has no configurable attributes.

Usage

Connect VCC to 5V, GND to GND, and SDA to a GPIO pin. On real hardware, add a 10 kΩ pull-up resistor between SDA and VCC. Read the sensor with the DHT sensor library.

#include <DHT.h>

#define DHT_PIN  2
#define DHT_TYPE DHT22

DHT dht(DHT_PIN, DHT_TYPE);

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

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

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

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print("%  Temp: ");
  Serial.print(tempC);
  Serial.println("C");

  delay(2000); // DHT22 supports at most ~0.5 Hz sampling
}

Examples

DescriptionAttrs
Default DHT22{}

See also