Member-only story

Setting up the DHT22 to the ESP32 with the Arduino IDE

Temperature | Humidity

⚗ Kevin Summersill 🔋
3 min readNov 13, 2020

In this article, we will connect the DHT22 to the ESP32 chip. This means we should be able to tell the humidity and temperature within the location you have your DHT22 chip connect. So let’s get started.

Photo by Inge Maria on Unsplash

What I will be utilizing in this article

I will be of course using an ESP32. The power is done via a USB cable from my pc to the board. I will also be utilizing a breadboard. https://amzn.to/3ppyVas

I will also be utilizing a DHT22 device which will be used to gather temperature and humidity data.

https://amzn.to/2GWb3K6

When you buy something using the retail links in the article, I may earn a small commission. (FTC Required Statement)

Install the DHT Libraries to Arduino IDE

In order to use the DHT22, we must install the libraries first.

  1. Go to Sketch > Manage Libraries > Library Manager
  2. Search for “DHT” and select the “DHT sensor library”. You can install it by click install at the bottom right. Make sure to “Install all” if asked. This will install the Adafruit Unified Sensor dependency for the DHT Sensor library. Close when done. The library Github repo can be found at https://github.com/adafruit/DHT-sensor-library

3. Now add the following code within the Arduino IDE:

/*
Author: Kevin Summersill
*/
// Add the DFH Library
#include "DHT.h"
// Define the DHT Type
#define DHTTYPE DHT22
// Define the GPIO Pin Associated to the ESP32
#define DHTPIN 4
void initializeDHT22Sensor() {
// Initialize DHT22 Sensor
DHT dht(DHTPIN, DHTTYPE);
pinMode(DHTPIN, INPUT);
dht.begin();

// Get the Temperature
float temperature = dht.readTemperature(true);
Serial.println(temperature);

// Get Humidity Data
float humidity = dht.readHumidity();
Serial.println(humidity);

--

--

⚗ Kevin Summersill 🔋
⚗ Kevin Summersill 🔋

Written by ⚗ Kevin Summersill 🔋

Enterprise Solution Architect | Certified K8s Administrator/Developer ⚓ | SAFe SPC | Cert Terraform | AWS Solutions Architect | Dev*Ops/GitOps Engineer 🔥

No responses yet

Write a response