#define PIN 12 // Define the pin connected to DHT22
#define TYPE DHT22 // Define the sensor type
#include <DHT.h>
DHT dht(PIN, TYPE); // Create DHT object
void setup() {
Serial.begin(115200); // Start serial communication
dht.begin(); // Initialize the DHT sensor
}
void loop() {
// Read temperature and humidity
float t = dht.readTemperature(); // Temperature in Celsius
float h = dht.readHumidity(); // Humidity percentage
// Check if the readings are valid
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read sensor data.");
return;
}
// Reset temperature and humidity if out of bounds
if (t > 50.00 || t < -10.00) {
t = 0;
}
if (h > 100) {
h = 0;
}
// Output temperature and humidity to the serial monitor
Serial.println();
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("'C");
Serial.println();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("%");
Serial.println();
delay(1000); // Delay of 1 second
}