Temperature sensor – ESP32 board

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 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(" %");
}