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.
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
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:
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: