Erriez DHT22 library for Arduino  1.2.1
AM2302/AM2303 DHT22 temperature and humidity sensor library for Arduino
DHT22 - AM2302/AM2303 temperature and humidity sensor library for Arduino

This is an Arduino library for the calibrated AM2302/AM2303 digital temperature and relative humidity sensor on a DHT22 breakout PCB.

DHT22 temperature and humidity sensor

Library features

AM2302/AM2303 sensor specifications

Safety warning

According to the datasheet, the AM2302/AM2303 is a low cost consumer temperature sensor. It may not be used in safety critical applications, emergency stop devices or any other occasion that failure of AM2302/AM2303 may cause personal injury.

Hardware

Schematic DHT22 and Arduino UNO

Pull-up resistor DAT pin

External capacitor

Board - DHT22 pins VCC GND DAT
Arduino UNO / Nano / Micro (ATMega328 boards) 5V (or 3V3) GND 2 (DIGITAL pin)
Arduino Leonardo 5V (or 3V3) GND 2 (DIGITAL pin)
Arduino Mega2560 5V (or 3V3) GND 2 (DIGITAL pin)
Arduino DUE (ATSAM3X8E) 3V3 GND 2 (DIGITAL pin)
ESP8266 (ESP12E / WeMos D1 R2 / NodeMCU v2 or v3) 3V3 GND GPIO4 (D2)
ESP32 (WeMos Lolin32 OLED / WeMos LOLIN D32) 3V3 GND GPIO4

Notes:

Examples

Arduino IDE | Examples | Erriez DHT22 Temperature & Humidity:

Documentation

Usage

Initialization

{c++}
#include <ErriezDHT22.h>
// Connect DTH22 DAT pin to Arduino board
// Connect DTH22 DAT pin to Arduino DIGITAL pin
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_SAM_DUE)
#define DHT22_PIN 2
#elif defined(ESP8266) || defined(ESP32)
#define DHT22_PIN 4 // GPIO4 (Labeled as D2 on some ESP8266 boards)
#else
#error "May work, but not tested on this target"
#endif
DHT22 dht22 = DHT22(DHT22_PIN);
void setup()
{
// Initialize serial port
Serial.begin(115200);
Serial.println(F("DHT22 temperature and humidity sensor example\n"));
// Initialize sensor
dht22.begin();
}

Read temperature and humidity

{c++}
void loop()
{
// Check minimum interval of 2000 ms between sensor reads
if (dht22.available()) {
// Read temperature from sensor
int16_t temperature = dht22.readTemperature();
// Read humidity from sensor
int16_t humidity = dht22.readHumidity();
if (temperature == ~0) {
// Print error (Check hardware connection)
Serial.print(F("Temperature: Error"));
} else {
// Print temperature
Serial.print(F("Temperature: "));
Serial.print(temperature / 10);
Serial.print(F("."));
Serial.print(temperature % 10);
Serial.println(F(" *C"));
}
if (humidity == ~0) {
// Print error (Check hardware connection)
Serial.print(F("Humidity: Error"));
} else {
// Print humidity
Serial.print(F("Humidity: "));
Serial.print(humidity / 10);
Serial.print(F("."));
Serial.print(humidity % 10);
Serial.println(F(" %"));
}
Serial.println();
}
}

Serial output

DHT22 temperature and humidity sensor example
Temperature: 17.7 *C
Humidity: 41.0 %
Temperature: 17.8 *C
Humidity: 41.1 %
...

Library dependencies

Library installation

Please refer to the Wiki page.

Other Arduino Libraries and Sketches from Erriez