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

When you have a DHT22 / AM2302 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 DHT22 sensor.

DHT22 sensor connected to ESP32 development board

Wiring your DHT22 sensor to your ESP32 development board

When you look at your DHT22 sensor, you can see three pins labeled +, out and -. Connect the:

  • + pin to a 3v3 pin on your board
  • out pin to GPIO27 on your board
  • - pin to a GND pin on your board

DHT22 sensor with gpio wires

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 DHT22 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 DHT22 sensor

After you had started your Arduino IDE, proceed to install a Arduino Library to read temperature and humidity from DHT22 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 DHT22 and install the DHT sensor library for ESPx by beegee_tokyo:

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

After you had installed the library, you will be able to write the Arduino sketch to read temperature and humidity from your DHT22 sensor.

Arduino sketch example to read temperature and humidity from the DHT22 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::DHT22);
  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 DHT22 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