DHT22 - AM2303 temperature and relative humidity sensor library for Arduino
MIT License
This is an Arduino library for the calibrated AM2302/AM2303 digital temperature and relative humidity sensor on a DHT22 breakout PCB.
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.
3k3..10k
pull-up resistor between the DAT
and VCC
pins only when:
3k3
pull-up resistor between DAT
and VCC
.100nF
capacitor between the sensor pins VCC
and GND
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 number 4
and is labeled as D2
on some WeMos ESP8266 boards.Arduino IDE | Examples | Erriez DHT22 Temperature & Humidity:
#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 for DHT22LowPower.ino
.Please refer to the Wiki page.