ESP32 and DHT11/22

This is example using ESP32 module, DHT11/22 temperature and humidity sensor with using arduinoIDE. Which will later be used as one module of my home weather station.

Parts required:

  • ESP32
  • DHT11 or DHT22
  • arduinoIDE with installed board ESP32

If you don’t have installed board ESP32 in arduinoIDE, or arduinoIDE don’t have installed go to my previous article: Installing ESP32 and ESP 8266 in arduinoIDE.

Schematic diagram:

DHT11 or DHT22 module – different manufacturer have diferent pinout. Check your piout and connect:

  • VCC – 3.3V
  • GND – GND
  • Signal – D4 – GPIO4

Libraries arduinoIDE required:

Libraries find in arduinoIDE Sketch > Include Library > Manage Libraries.

First install DHT sensor library. On the searchbox find “DHT”, select “DHT sensor library” by Adafruit and click to install:

After instalation search in the same box “Adafruit Unified Sensor” and install:

Sketch:

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////  Author: Jiri Kucera                           ////
////  Description: ESP32 and DHT11 or DHT22         ////
////               humidity and temperature sensor  ////
////  Using board: ESP32 DEV KIT V1                 ////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
////  include libraries for sensors                 ////
////////////////////////////////////////////////////////

#include "DHT.h"
#include <Adafruit_Sensor.h>

////////////////////////////////////////////////////////
////  define and set sensors                        ////
////////////////////////////////////////////////////////

#define DHTPIN 4     
DHT dht(DHTPIN, DHT11); //if using DHT22 must set DHT22

////////////////////////////////////////////////////////
////  Setup                                         ////
////////////////////////////////////////////////////////

void setup() {
//initialize serial for debugging and print result
  Serial.begin(115200);

//initialize DHT
  dht.begin();
}

////////////////////////////////////////////////////////
////  Loop                                          ////
////////////////////////////////////////////////////////

void loop() { 
  //DHT sensor read data
  float humidity    = dht.readHumidity();
  float temperature = dht.readTemperature();

  //print result to serial
  Serial.print("Humidity = ");
  Serial.print(humidity, 2);
  Serial.println(" %");
  
  Serial.print("Temperature = ");
  Serial.print(temperature, 2);
  Serial.println(" *C");
  
  delay(2000); //delay between readings
}

Result:

Result is in arduinoIDE Tools > Serial monitor. In window select baud rate, in this example is 115200.

Source code in GitHub:

https://github.com/kuca171/ESP32_DHT11_DHT22

Leave a comment

Design a site like this with WordPress.com
Get started