Here are the different pins of the DHT11 sensor:
To be able to compile the programs that operate the temperature sensor, you will need the DHT11 library for ESP32.
To do this in Arduino IDE, click on Library Manager then search for the following library:
#include <DHT.h> // Include DHT library
//Set the pin the sensor is connected to
#define DHTPIN 23 // Pin GPIO23
#define DHTTYPE DHT11 // Define the DHT sensor type (here DHT11)
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object
void setup() {
Serial.begin(115200); // Initialize serial communication to display results
dht.begin(); // Initialize the DHT sensor
}
void loop() {
// Wait 2 seconds between each reading
delay(2000);
// Read humidity and temperature
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Read the temperature in degrees Celsius
// Check if reading failed and try again
if (isnan(humidity) || isnan(temperature)) {
Serial.println("DHT sensor reading error");
return;
}
// Display the values read on the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\t");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}