#include "DHT.h"
// Define the DHT type and the GPIO pin it's connected to
#define DHTPIN 15 // Pin where the DHT22 data pin is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200); // Initialize serial communication
dht.begin(); // Start DHT sensor
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Reading in Celsius
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(temperature)) {
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");
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4