The DHT22 is a reliable sensor for measuring temperature and humidity. It is a digital sensor that provides high accuracy and long-term stability, making it suitable for a wide range of applications, including weather stations, home automation systems, and environmental monitoring.
Pin Number | Name | Description |
---|---|---|
1 | VDD | Power supply (3.3 to 6V DC) |
2 | DATA | Digital data output |
3 | NC | Not connected (no function) |
4 | GND | Ground |
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Celsius (isFahrenheit = false)
float heatIndex = dht.computeHeatIndex(temperature, humidity, false);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.print("°C Heat index: ");
Serial.print(heatIndex);
Serial.println("°C");
}
Q: Can the DHT22 be used outdoors? A: Yes, but it should be protected from direct sunlight and water.
Q: How long does the DHT22 last? A: With proper care, the DHT22 can last for several years, although the accuracy may degrade over time.
Q: Is calibration required for the DHT22? A: The DHT22 comes pre-calibrated from the factory. However, for critical applications, periodic recalibration may be necessary.
Q: Can I use multiple DHT22 sensors on the same microcontroller? A: Yes, each DHT22 sensor can be connected to a separate digital pin on the microcontroller. Ensure each data line has its own pull-up resistor.