How to use a ESP32 development board to read temperature and humidity from a DH11 sensor

When you have a DHT11 sensor, you will be able to get the temperature and humidity of your environment.

Given that, this post shows how to use ESP32 development board to read temperature and humidity from a DHT11 sensor.

DHT11 sensor connected to ESP32 board

Wiring your DHT11 sensor to your ESP32 development board

When you look at your DHT11 sensor, you can see three pins labeled +/VCC, OUT/S/DATA and -/GND. Connect the:

  • +/VCC pin to a 3v3 pin on your board
  • OUT/s/DATA pin to GPIO27 on your board
  • -/GND pin to a GND pin on your board

For example, this is how I had wired my DHT11 sensor to my ESP32 development board:

DHT11 sensor with GPIO cables

ESP32 with gpio wires connected to 3v3 gnd and gpio27 pins

Enabling ESP32 Development on Arduino IDE

At this point in time, you are ready to flash a program into your ESP32 board to read from the DHT11 sensor. In order to do so, we need to use a tool to write our program into the flash memory of the development board.

Since it is easy to write code and flash programs with Arduino IDE, we can use it to serve our purpose.

In order to use Arduino IDE for this guide, be sure to enable ESP32 development on Arduino IDE before continuing.

Installing the DHT sensor library for ESPx to read temperature and humidity from DHT11 sensor

After you had started your Arduino IDE, proceed to install a Arduino Library to read temperature and humidity from DHT11 sensor.

In order to do so, first go to Tools -> Manage Libraries.... After you had done so, the Library Manager window will appear. Search for DHT11 and install the DHT sensor library for ESPx by beegee_tokyo:

Mac Arduino IDE 1.8.9 Library Manager with libraries filtered by DHT11 and DHT sensor library for ESPx highlighted

Arduino sketch example to read temperature and humidity from the DHT11 sensor

Once you had installed the DHT sensor library for ESPx, upload the following sketch to your ESP32 board:

#include "DHTesp.h"
DHTesp dht;
  
void setup()
{
  Serial.begin(9600);
  dht.setup(27, DHTesp::DHT11);
  Serial.println();
  delay(1000);
}
  
void loop()
{
  float temperature = dht.getTemperature();
  float humidity = dht.getHumidity();
  
  Serial.print("Temperature: ");
  Serial.println(temperature);
  Serial.print("Humidity: ");
  Serial.println(humidity);
  
  delay(2000);
}

After your Arduino IDE had uploaded the above sketch to your ESP32 board, you should see output similar to the following:

Mac Arduino 1.8.9 serial monitor showing temperature and humidity from DHT11 sensor

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.

Udemy.com Homepage 300x250 Domain names for just 88 cents!

Find a domain starting at $0.88

powered by Namecheap