ESP32: DHT11/22 Humidity Temperature Sensor Interfacing Example

In this tutorial we are interfacing DHT11 or DHT22 Humidity temperature sensor with ESP32 DevKit.

The DHT11 (or DHT22 and similar) are cheap temperature and humidity sensors. The communicate with a ESP32 is over a single wire, but unfortunately it is not compatible with the 1-Wire protocol defined by Dallas Semiconductors.

The electric connection to the ESP32 DevKit is very simple, as the DHT series can be powered direct with 3.3V. Only 3 wires are needed: VCC, GND and the data line. Data Line is connected to GPIO15 (D15).

DHT11 Connections with ESP32

ESP32 DHT11 Interfacing webserver

 

DHT11 Sensor Specifications

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). Its fairly simple to use, but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds, so when using our library, sensor readings can be up to 2 seconds old. Compared to the DHT22, this sensor is less precise, less accurate and works in a smaller range of temperature/humidity, but its smaller and less expensive.

If you are using module it comes with a 4.7K or 10K resistor. You need external 10K or 4.7K pull up on data pin to VCC.

DHT11 Pinout for ESP8266

DHT11 Specifications:

  • Low cost
  • 3 to 5V power and I/O
  • 2.5mA max current use during conversion (while requesting data)
  • Good for 20-80% humidity readings with 5% accuracy
  • Good for 0-50°C temperature readings ±2°C accuracy
  • No more than 1 Hz sampling rate (once every second)
  • Body size 15.5mm x 12mm x 5.5mm
  • 4 pins with 0.1″ spacing

ESP32 Program for DHT11 Sensor

Before moving directly on coding install required libraries.

Go to menu Sketch >> Include Library >> Manage Library

Install DHT sensor library for ESPx by beegee library. or DownloadNodeMCU DHT11 LibraryAfter installing library write below code.

/* 
 * ESP32 NodeMCU DHT11 - Humidity Temperature Sensor Example
 * https://circuits4you.com
 * 
 * References
 * https://circuits4you.com/2017/12/31/nodemcu-pinout/
 * 
 */
 
#include "DHTesp.h"

#define DHTpin 15    //D15 of ESP32 DevKit

DHTesp dht;

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex (C)\t(F)");
  
  // Autodetect is not working reliable, don't use the following line
  // dht.setup(17);

  // use this instead: 
  dht.setup(DHTpin, DHTesp::DHT11); //for DHT11 Connect DHT sensor to GPIO 17
  //dht.setup(DHTpin, DHTesp::DHT22); //for DHT22 Connect DHT sensor to GPIO 17
}

void loop()
{
  delay(dht.getMinimumSamplingPeriod());

  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();

  Serial.print(dht.getStatusString());
  Serial.print("\t");
  Serial.print(humidity, 1);
  Serial.print("\t\t");
  Serial.print(temperature, 1);
  Serial.print("\t\t");
  Serial.print(dht.toFahrenheit(temperature), 1);
  Serial.print("\t\t");
  Serial.print(dht.computeHeatIndex(temperature, humidity, false), 1);
  Serial.print("\t\t");
  Serial.println(dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true), 1);
}

Results

After uploading program in ESP32 open serial monitor with 115200 baud rate.

DHT11 NodeMCU Readigns

References:

Leave a Reply