ESP32 and DHT11 sensor example

DHT11 digital temperature and humidity sensor is a composite Sensor contains a calibrated digital signal output of the temperature and humidity. Application of a dedicated digital modules collection technology and the temperature and humidity sensing technology, to ensure that the product has high reliability and excellent long-term stability. The sensor includes a resistive sense of wet components and an NTC temperature measurement devices, and connected with a high-performance 8-bit microcontroller

dht11 module
dht11 module


Low cost, long-term stability, relative humidity and temperature measurement, excellent quality, fast response, strong anti-interference ability, long distance signal transmission, digital signal output, and precise calibration.


By using a module this avoids the need to build this on a breadboard

lolin32 and dht11
lolin32 and dht11


You need to add the DHT11 library from adafruit to the Arduino IDE –

#include "DHT.h"
//here we use 14 of ESP32 to read data
#define DHTPIN A13
//our sensor is DHT11 type
#define DHTTYPE DHT11
//create an instance of DHT sensor

void setup()
Serial.println("DHT11 sensor!");
//call begin to start sensor

void loop() {
//use the functions which are supplied by library.
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
// print the result to Terminal
Serial.print("Humidity: ");
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.println(" *C ");
//we delay a little bit for next read


Open the serial monitor and you should see something like this

Humidity: 23.00 % Temperature: 24.00 *C
Humidity: 23.00 % Temperature: 24.00 *C
Humidity: 24.00 % Temperature: 24.00 *C
Humidity: 24.00 % Temperature: 25.00 *C
Humidity: 24.00 % Temperature: 25.00 *C
Humidity: 25.00 % Temperature: 25.00 *C
Humidity: 29.00 % Temperature: 26.00 *C
Humidity: 29.00 % Temperature: 26.00 *C
Humidity: 29.00 % Temperature: 26.00 *C
Humidity: 28.00 % Temperature: 27.00 *C
Humidity: 29.00 % Temperature: 27.00 *C


DHT11 Temperature and Relative Humidity Sensor Module With Cable for Arduino