#include <Adafruit_Sensor.h> // Include the Adafruit Sensor library
#include <DHT.h> // Include the DHT library
#define DHTPIN 15 // Digital pin connected to the DHT22 sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor type
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor
void setup() {
Serial.begin(115200); // Start the Serial communication
dht.begin(); // Initialize the DHT sensor
}
void loop() {
delay(2000); // Wait a few seconds between measurements
float humidity = dht.readHumidity(); // Read humidity
float temperature = dht.readTemperature(); // Read temperature in Celsius
// 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;
}
// Print temperature and humidity to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}