#include <DHT.h> // Include the DHT sensor library
#define DHTPIN 2 // Define the digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // Define the type of DHT sensor being used (DHT22)
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object with the specified pin and type
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud
dht.begin(); // Initialize the DHT sensor
}
void loop() {
delay(1000); // Wait for 1 second between measurements.
float h = dht.readHumidity(); // Read humidity from the DHT sensor
float t = dht.readTemperature(); // Read temperature in Celsius from the DHT sensor
float f = dht.readTemperature(true); // Read temperature in Fahrenheit from the DHT sensor
Serial.print("Humidity: "); // Print the string "Humidity: " to the serial monitor
Serial.print(h, 1); // Print the humidity value with 1 decimal place to the serial monitor
Serial.print("% Temperature: "); // Print the string "% Temperature: " to the serial monitor
Serial.print(t, 1); // Print the temperature value in Celsius with 1 decimal place to the serial monitor
Serial.print("C & "); // Print the string "C & " to the serial monitor
Serial.print(f, 1); // Print the temperature value in Fahrenheit with 1 decimal place to the serial monitor
Serial.println("F"); // Print the string "F" followed by a newline to the serial monitor
}