#include <DHT.h> // Include DHT library
#define DHTPIN 23 // Define pin where DHT22 is connected
#define DHTTYPE DHT22 // Define type of DHT sensor
DHT dht(DHTPIN, DHTTYPE); // Create an instance of the DHT class
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud rate
dht.begin(); // Initialize the DHT sensor
}
void loop() {
delay(2000); // Wait for 2 seconds between readings
float h = dht.readHumidity(); // Read humidity
float t = dht.readTemperature(); // Read temperature in Celsius
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print temperature and humidity to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}