#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT22
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
void setup() {
Serial.begin(9600);
Serial.println("DHT22 Sensor Reading...");
dht.begin(); // Start the DHT sensor
}
void loop() {
// Reading temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Celsius
float temperatureF = dht.readTemperature(true); // Fahrenheit
// Check if any reads failed
if (isnan(humidity) || isnan(temperature) || isnan(temperatureF)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the results
Serial.print("Humidity: "); Serial.print(humidity); Serial.print("%\t");
Serial.print("Temperature: "); Serial.print(temperature); Serial.print("°C\t");
Serial.print(temperatureF); Serial.println("°F");
delay(2000); // Delay between readings
}