#include <DHT.h>
#define DHTPIN 23 // The pin where the DHT11 data pin is connected
#define DHTTYPE DHT22 // The type of sensor (DHT11)
DHT dht(DHTPIN, DHTTYPE); // Initialize the sensor
void setup() {
Serial.begin(115200); // Start the Serial Monitor at 115200 baud rate
dht.begin(); // Start the DHT sensor
}
void loop() {
delay(2000); // Wait 2 seconds before reading again
float humidity = dht.readHumidity(); // Read humidity
float temperature = dht.readTemperature(); // Read temperature in Celsius
// Check if the readings failed
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Error reading from DHT sensor!");
return;
}
// Print the temperature and humidity to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% ");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
}