Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
The DHT22 is a popular sensor for measuring temperature and humidity, known for its accuracy and ease of use. It is widely used in Arduino projects due to its compatibility and straightforward interfacing. This article will guide you through the process of connecting the DHT22 sensor to an Arduino board and writing a simple program to read and display the sensor data.
The DHT22 sensor, also known as AM2302, is a digital-output relative humidity and temperature sensor. It provides high reliability and long-term stability. The sensor can measure humidity from 0 to 100% with an accuracy of ±2% and temperature from -40°C to 80°C with an accuracy of ±0.5°C.
To read data from the DHT22 sensor, you will need to use a library that simplifies the communication between the Arduino and the sensor.
Install the DHT Library:
Write the Arduino Sketch:
#include "DHT.h"
#define DHTPIN 2 // Pin where the data line is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000); // Wait a few seconds between measurements
float humidity = dht.readHumidity();
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;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}
Once the code is uploaded, open the Serial Monitor from the Arduino IDE (set the baud rate to 9600). You should see the temperature and humidity readings displayed every two seconds.
By following these steps, you have successfully interfaced the DHT22 sensor with an Arduino board to measure temperature and humidity. This setup can be expanded into more complex projects, such as weather stations or home automation systems.