// Include the DHT sensor library. #include "DHT.h" // Define the digital pin and type of DHT sensor. #define DHTPIN D4 // Data pin connected to the DHT11 #define DHTTYPE DHT11 // Specifies the DHT sensor type // Create a DHT sensor object. DHT dht(DHTPIN, DHTTYPE); // Initializes the DHT sensor void setup() { Serial.begin(115200); // Begins serial communication dht.begin(); // Starts the DHT sensor } void loop() { // Waits a few seconds between measurements (DHT sensors should not be read more than once every 2 seconds) delay(2000); // Reads the humidity percentage float humidity = dht.readHumidity(); // Reads the temperature in Celsius degrees float temperature = dht.readTemperature(); // Checks if the readings failed and if so, exits the loop function. if (isnan(humidity) || isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); return; } // Prints the results to the serial monitor Serial.print("Humidity: "); Serial.print(humidity); Serial.print("% Temperature: "); Serial.print(temperature); Serial.println("°C"); }