#include <DHT.h>
// Define the type of DHT sensor you're using
#define DHTTYPE DHT22 // DHT22 is the sensor type
// Define the pin where the data pin is connected
#define DHTPIN 12
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start serial communication for displaying data
Serial.begin(9600);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature(); // Read temperature as Celsius
float temperatureF = dht.readTemperature(true); // Read temperature as Fahrenheit
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the values to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print(" °C ");
Serial.print(temperatureF);
Serial.println(" °F");
}