/*
author : Rajeev TR
date : 29/08/2024
https://github.com/HoNtErBoT
*/
#include <DHT.h>
#define DHTPIN 2 // Define the pin where the DHT22 is connected
#define DHTTYPE DHT22 // Define the type of DHT sensor
DHT dht(DHTPIN, DHTTYPE); // Create an instance of the DHT class
void setup()
{
Serial.begin(9600); // Initialize serial communication
dht.begin(); // Initialize the DHT sensor
}
void loop()
{
delay(2000); // Wait a few seconds between measurements
float humidity = dht.readHumidity(); // Read humidity (percentage)
float temperature = dht.readTemperature(); // Read temperature in Celsius
if (isnan(humidity) || isnan(temperature)) // Check if any reads failed and exit early (to try again)
{
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the results to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}