#include "DHT.h"
// Define the pin where the DHT sensor is connected
#define DHTPIN 4 // GPIO4 (change it based on your wiring)
// Define the type of DHT sensor
#define DHTTYPE DHT22 // or DHT22 if you’re using that
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println("Temperature & Humidity Sensor Initialized");
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Celsius by default
// Check if any reads failed
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print values to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}