This is an Arduino library for the calibrated AM2302/AM2303 digital temperature and relative humidity sensor on a DHT22 breakout PCB.
- Read 16-bit temperature (synchronous blocking)
- Read 16-bit relative humidity (synchronous blocking)
- Configurable number of read retries when a read error occurs (default is 1 read + 2 retries)
- Long time duration example
- Temperature and humidity average with a configurable number of samples to remove jitter
- Voltage: 3.3 .. 5V
- Ultra-low power:
- Typical 15uA dormancy
- Typical 500uA measuring
- Single wire digital serial interface
- Calibrated digital signal
- Outstanding long term stability
- No additional electronic components needed
- Humidity:
- Range: 0 .. 99.9 %RH (Relative Humidity)
- Resolution: 0.1 %RH
- Accuracy: +/-2 %RH (at 25 degree Celsius)
- Temperature:
- Range: -40 .. +125 degree Celsius
- Resolution: 0.1 degree Celsius
- Accuracy: +/- 0.4 degree Celsius
- Minimum read interval: 2000 ms
- ~31ms to synchronous read humidity, temperature and parity data from sensor (5 Bytes)
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.
- Connect an external
3k3..10k
pull-up resistor between theDAT
andVCC
pins only when:- Using a AM2302/AM2303 sensor without a DT22 breakout PCB and the MCU IO pin has no built-in or external pull-up resistor.
- The DHT22 breakout PCB contains a
3k3
pull-up resistor betweenDAT
andVCC
. - Please refer to the MCU datasheet or board schematic for more information about IO pin pull-up resistors.
- Tip: Connect a
100nF
capacitor between the sensor pinsVCC
andGND
when read errors occurs. This may stabilize the power supply.
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:
GPIO4
uses sketch pin number4
and is labeled asD2
on some WeMos ESP8266 boards.- Other MCU's may work, but are not tested.
Arduino IDE | Examples | Erriez DHT22 Temperature & Humidity:
- DHT22 Getting started example.
- DHT22Average Calculate average temperature and humidity.
- DHT22DurationTest Test reliability connection.
- DHT22Logging Write temperature and humidity every 10 minutes to .CSV file on SD-card with DS3231 RTC.
- DHT22LoggingAVR LowPower SD-card logging for AVR targets only. Arduino Pro or Pro Mini at 8MHz is recommended.
- DHT22LowPower LowPower AVR targets only. Arduino Pro or Pro Mini at 8MHz is recommended.
#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();
}
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();
}
}
DHT22 temperature and humidity sensor example
Temperature: 17.7 *C
Humidity: 41.0 %
Temperature: 17.8 *C
Humidity: 41.1 %
...
LowPower
library forDHT22LowPower.ino
.
Please refer to the Wiki page.