Temperature and humidity reading by ESP32

In this project, you can read temperature and humidity in the air with an ESP32 and DHT11 or DHT22 that displays the temperature and humidity using the Arduino IDE programming environment. A DHTxx sensor is a digital temperature, humidity sensor and it provides fully calibrated digital outputs for the two measurements. These sensors are based on a custom protocol which uses a single wire/bus for communication.

Setup the ESP32 board in Arduino IDE

If you want to learn more about the ESP32 development board visit the official website of esp32:
There is an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language. Follow this tutorial to prepare your Arduino IDE:

Components Required

  • ESP32 development board
  • DHT11/DHT22 sensor
  • 4700 Ohm Resistor
  • Breadboard
  • Jumper wires

Circuit Diagram

Connect the pin of ESP32 to the pinof DHTxx sensor. The user can use any of DHT sensors like DHT11, DHT22 etc.

    • ESP32 IO14 – DHTxx DATA
    • ESP32 3.3 V- DHTxx VCC
    • ESP32 GND- DHTxx GND
    • NC – No Connection

Code Snippet

#include "DHT.h"

#define DHTPIN 14     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11  (AM2302)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}