ESP32 Sensor Interfacing

Reading Temperature & Humidity with the DHT22 Sensor

Introduction

One of the core motivations behind learning the ESP32 is to build practical Internet of Things (IoT) applications. A vital part of any IoT project is the ability to read real-world sensor data and transmit it — whether to the cloud, a local dashboard, or another device. In this lesson, we begin our journey into sensor interfacing with the ESP32, focusing on one of the most widely used environmental sensors: the DHT22.

Types of Sensors

Sensors used with microcontrollers generally fall into two broad categories:

1. Analog Sensors

Analog sensors output a variable voltage proportional to the physical quantity being measured. For example, the classic LM35 temperature sensor outputs 10 mV per °C. At 25°C room temperature, it would output roughly 250 mV. To use these with a microcontroller, an Analog-to-Digital Converter (ADC) is required to translate the voltage into a usable digital value.

2. Digital Sensors

Digital sensors output discrete logic levels or structured digital data. The simplest type outputs just HIGH (1) or LOW (0) — used in basic fire sensors, sound sensors, and PIR (motion) sensors. More advanced digital sensors communicate over defined protocols such as I2C, or send data as timed pulse trains, like the DHT22 and ultrasonic distance sensors.

Why This Course Avoids the ESP32's Built-in ADC

Important Note: The ESP32's analog input (ADC) is intentionally not covered in this course for practical reasons.

The ESP32 ADC has a reputation for being inconsistent and poorly documented:

Recommendation: If your project genuinely requires analog sensing, consider using an external, dedicated ADC module (e.g., ADS1115) rather than relying on the ESP32's internal ADC.

Meet the DHT22 (and DHT11)

For this course, we use the DHT22 temperature and humidity sensor because it is easy to interface, inexpensive, and globally available. It is popular enough that official IoT tutorials from Microsoft Azure and Amazon AWS use it as their reference sensor for cloud demonstrations.

DHT22 vs DHT11

FeatureDHT11 (Blue, small)DHT22 (White, larger)
AccuracyLowerHigher
Sensing RangeNarrowerWider
Recommended UseBasic hobby projectsStandard choice for most projects

Key Specifications (DHT22)

ParameterValue
Operating Temperature Range-40°C to +80°C
Humidity Range0% to 100% RH
Operating Voltage3V to 6V
OutputDigital signal (single-wire protocol)

Sensor Pinout (Bare 4-Pin Sensor)

Pin #FunctionNotes
1VCCPower supply
2DATAConnects to any GPIO; needs pull-up resistor
3NCNot connected
4GNDGround
Most breakout modules (as opposed to the bare sensor) simplify this to only 3 pins — VCC, GND, and DATA — because the required pull-up resistor is already built into the module's PCB.

Wiring Diagram

Bare 4-Pin Sensor (Manual Pull-up Required)

DHT22 (Bare Sensor) ┌───────────────┐ │ 1 2 3 4 │ │ VCC DATA NC GND│ └─┬────┬──────┬──┘ │ │ │ 3.3V │ GND │ ┌──┴──┐ │ 10kΩ│ (Pull-up resistor to VCC) └──┬──┘ │ ESP32 GPIO (e.g. GPIO27)

Module Version (Pull-up Already Included)

DHT22 Module ┌───────────────┐ │ + OUT - │ └──┬────┬────┬──┘ │ │ │ 3.3V GPIO GND (e.g. 27)
Tip: Keep your wiring color-coded and your pin naming consistent in code — this greatly reduces confusion when debugging connections later.

Installing Required Libraries

Two libraries are required to read data from the DHT22 using the Arduino IDE:

Steps to Install

  1. Open the Arduino IDE
  2. Go to Sketch → Include Library → Manage Libraries
  3. Search for "DHT"
  4. Select "DHT sensor library" by Adafruit
  5. Click Install
  6. Accept the prompt to also install "Adafruit Unified Sensor"

Example Code: Reading Temperature & Humidity

Below is a simplified sketch that initializes the DHT22 on GPIO 27 and prints the humidity and temperature readings to the Serial Monitor every 2 seconds.

#include <DHT.h>

#define DHTPIN 27       // GPIO connected to DATA pin
#define DHTTYPE DHT22   // Sensor type: DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println("°C");

  delay(2000);
}
Note: Always double-check DHTPIN matches the physical GPIO you wired the DATA line to — a mismatch is the most common cause of failed readings.

Troubleshooting Common Issues

It's completely normal not to get valid readings on your first attempt. Common causes include:

If readings show nan (Not a Number), this typically means the ESP32 is not successfully communicating with the sensor — recheck wiring before assuming the sensor is defective.

Testing & Next Steps

Once your setup works, try a simple experiment: breathe near the sensor or place it near a humid surface and watch the humidity percentage rise in real time — a great way to confirm the sensor is actively responding. Hands-on Tip

This DHT22 setup, along with a general-purpose relay module, will form the foundation for a series of upcoming IoT experiments — including cloud data logging, automated climate control triggers, and remote monitoring dashboards.