The DHT11 is an economical and popular sensor used to measure temperature and humidity in various electronic projects. The DHT11 is a digital sensor that communicates with the microcontroller via a single data wire (in “one-wire” mode).
It can measure temperatures ranging from 0 to 50°C with an accuracy of ±2°C and humidity levels between 20 and 80% with an accuracy of ±5% RH (relative humidity).
The sensor returns these values as digital data, simplifying integration with platforms like the ESP32.
Here are the different pins of the DHT11 sensor:
Signal: Used to send the value to the ESP32
Vcc: The supply voltage (5V)
GND: The mass
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 DHTPIN23// Pin GPIO23
#define DHTTYPEDHT11// Define the DHT sensor type (here DHT11)DHTdht(DHTPIN,DHTTYPE);// Create a DHT objectvoidsetup(){Serial.begin(115200);// Initialize serial communication to display results
dht.begin();// Initialize the DHT sensor}voidloop(){// Wait 2 seconds between each readingdelay(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 againif(isnan(humidity)||isnan(temperature)){Serial.println("DHT sensor reading error");return;}// Display the values read on the serial monitorSerial.print("Temperature: ");Serial.print(temperature);Serial.print(" °C\t");Serial.print("Humidity: ");Serial.print(humidity);Serial.println(" %");}