The DHT22 is a reliable sensor for measuring temperature and humidity. This sensor is suitable for a wide range of applications due to its ability to provide long-term stability and relatively high measurement accuracy. Common applications include HVAC systems, weather stations, home environment monitoring, and IoT projects.
Pin Number | Name | Description |
---|---|---|
1 | VDD | Power supply (3.3 to 6V DC) |
2 | DATA | Digital output pin |
3 | NC | Not connected |
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");
}
Note: This code requires the DHT sensor library, which can be installed via the Arduino Library Manager.
Q: Can I use the DHT22 sensor outdoors? A: Yes, but it should be protected from direct sunlight, rain, and condensation.
Q: How long should I wait between readings? A: The DHT22 has a sampling rate of up to once every 2 seconds. It is recommended to wait at least this long to avoid self-heating and to ensure accurate measurements.
Q: What should I do if the sensor is not responding? A: Check the power supply, wiring, and pull-up resistor. If the issue persists, replace the sensor as it may be defective.
For further assistance, consult the manufacturer's datasheet and technical support resources.