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

Features

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.

Schematic

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

lolin32 and dht11
lolin32 and dht11

Code

You need to add the DHT11 library from adafruit to the Arduino IDE – https://github.com/adafruit/DHT-sensor-library


#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
DHT dht(DHTPIN, DHTTYPE);

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

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!");
return;
}
// print the result to Terminal
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
//we delay a little bit for next read
delay(2000);
}

Output

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

Link

DHT11 Temperature and Relative Humidity Sensor Module With Cable for Arduino

SHARE
Previous articleESP32 and MPU-9250 sensor example
Next articleUse a ESP32 to display DHT11 readings on a web page