Reading Temperature & Humidity with the DHT22 Sensor
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.
Sensors used with microcontrollers generally fall into two broad categories:
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.
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.
The ESP32 ADC has a reputation for being inconsistent and poorly documented:
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.
| Feature | DHT11 (Blue, small) | DHT22 (White, larger) |
|---|---|---|
| Accuracy | Lower | Higher |
| Sensing Range | Narrower | Wider |
| Recommended Use | Basic hobby projects | Standard choice for most projects |
| Parameter | Value |
|---|---|
| Operating Temperature Range | -40°C to +80°C |
| Humidity Range | 0% to 100% RH |
| Operating Voltage | 3V to 6V |
| Output | Digital signal (single-wire protocol) |
| Pin # | Function | Notes |
|---|---|---|
| 1 | VCC | Power supply |
| 2 | DATA | Connects to any GPIO; needs pull-up resistor |
| 3 | NC | Not connected |
| 4 | GND | Ground |
Two libraries are required to read data from the DHT22 using the Arduino IDE:
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); }
DHTPIN matches the physical GPIO
you wired the DATA line to — a mismatch is the most common cause of failed readings.
It's completely normal not to get valid readings on your first attempt. Common causes include:
DHTPIN value in codenan (Not a Number), this typically means the ESP32 is not
successfully communicating with the sensor — recheck wiring before assuming the sensor
is defective.
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.