Interactive Mission

Read Temperature with DHT22

Lights are fun — but sensors make your ESP32 smart. This mission connects a DHT22 so your board can feel the temperature and humidity in the room.

Mission 02Beginner10–15 minutesParent SafeTeacher Friendly
Mission 02

Teach Your ESP32 to Feel the Weather!

The Story

Your ESP32 already knows how to blink. Now it needs senses — like a robot opening its eyes for the first time.

The DHT22 is a small sensor that measures how warm and how humid the air is. You'll wire it up, ask it for readings, and watch numbers appear in the Serial Monitor. That's the same skill used in weather stations and greenhouse monitors worldwide.

Explain Like I'm 12

The DHT22 is a tiny weather box with three legs. One leg gets power, one connects to ground, and the data leg sends numbers back to the ESP32 — like sending a text message that says "it's 24 degrees and kind of sticky outside."

What You'll Build

A breadboard circuit with DHT22 connected to ESP32. Your code reads temperature (°C) and humidity (%) every two seconds and prints them to Serial Monitor on your computer.

Things You'll Need

Component Spotlight

Learn more about your ESP32 board.

How Does the DHT22 Send Data?

Concept Illustration DHT22 sensor with VCC, GND, and single data line connected to ESP32 GPIO4 with pull-up resistor

The DHT22 uses one data wire for communication. The ESP32 sends a start signal, then the sensor replies with a packet of bits that the library converts into temperature and humidity numbers.

A pull-up resistor (often built into modules) keeps the data line stable at 3.3 V until the sensor pulls it low to send data.

Wiring Diagram

Follow these steps in order. Unplug USB before you change any wires.

Wiring Diagram DHT22 VCC to 3.3V, GND to GND, DATA to GPIO4
  1. 1

    Unplug USB. Place DHT22 module on the breadboard.

  2. 2

    Connect VCC on the module to ESP32 3.3 V.

  3. 3

    Connect GND on the module to ESP32 GND.

  4. 4

    Connect DATA on the module to ESP32 GPIO4.

  5. 5

    If using bare DHT22 (not module), add a 10 kΩ pull-up from DATA to 3.3 V.

  6. 6

    Install the DHT sensor library in Arduino IDE (Library Manager → search DHT).

Code

Copy this into Arduino IDE, then click Upload.

dht22_read.ino
#include "DHT.h"

#define DHTPIN 4
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
  Serial.println("DHT22 ready!");
}

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

  if (isnan(humidity) || isnan(tempC)) {
    Serial.println("Read failed — check wiring");
    delay(2000);
    return;
  }

  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print(" °C  |  Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  delay(2000);
}

Expected Output

Open Serial Monitor at 115200 baud. Every two seconds you'll see lines like:

Temperature: 24.3 °C | Humidity: 55.0 %

Breathe warm air near the sensor — temperature should rise slightly. If you see "Read failed", check DATA on GPIO4 and 3.3 V power.

Mini Quiz

Quick check — no grades, just confidence!

Q1. How often should you read the DHT22 at minimum?

Q2. What does NaN mean in the Serial output?

Challenge Yourself

Add an alert: if temperature goes above 28 °C, print "It's getting warm!" If humidity goes above 70 %, print "Feels humid!". Try holding an ice cube near the sensor (not touching water to the board).

Mission Complete!

Your ESP32 can now read the real world! Temperature and humidity data powers weather stations, smart thermostats, and plant monitors. You learned sensor wiring, libraries, and Serial debugging — core skills every engineer uses.

  • Wire a DHT22 to 3.3 V, GND, and GPIO4
  • Install and use the DHT sensor library
  • Read and debug sensor values in Serial Monitor

Continue Your Journey