How to Measure Temperature and Humidity – DHT11

In this tutorial, you will learn how to measure temperature and humidity with a DHT11 sensor and an ESP32.

Parts Required

Circuit

Code

#include "DHT.h"

#define DHTPIN A13

#define DHTTYPE DHT11

DHT dht (DHTPIN, DHTTYPE); //Create an instance of DHT sensor

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT11 Sensor!");
  
  dht.begin(); //Start the sensor
}

void loop()
{
  float h = dht.readHumidity(); //Read Humidity
  float t = dht.readTemperature(); //Read Temperature
  
  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 ");
  
  delay(2000);
}

References

[1] http://www.esp32learning.com/code/esp32-and-dht11-sensor-example.php

Leave a Reply

Your email address will not be published. Required fields are marked *