#include <DHT.h>
#define DHTPIN 2 // Pin connected to the DHT22 sensor
#define DHTTYPE DHT22 // DHT22 (AM2302) sensor type, change to DHT11 if you're using a DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000); // Wait for a few seconds between readings
float temperature = dht.readTemperature(); // Read temperature in Celsius
float humidity = dht.readHumidity(); // Read humidity
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the temperature and humidity values
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
}