#include "DHT.h"
#define DHTPIN 4 // Define the GPIO pin where the data pin of DHT is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302), you can change to DHT11 if using DHT11
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.
void setup() {
Serial.begin(115200); // Initialize serial communication at a baud rate of 115200
dht.begin(); // Start the DHT sensor
}
void loop() {
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 the results to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C ");
delay(2000); // Wait a few seconds between measurements
}