← Back to list

How do I measure humidity using a DHT11 or DHT22?

To measure humidity using a DHT11 or DHT22, you’ll need a microcontroller (like Arduino, ESP32, STM32, or Raspberry Pi) and follow these…

Ampheo · 2025-08-19 06:31 · 1 claps · 1.4 min read
#dht11 #dht-22 #arduino #stm32 #raspberry-pi
Open on Medium ↗
Wiki topics: 📟 · Gadgets & IoT

How do I measure humidity using a DHT11 or DHT22?

To measure humidity using a **DHT11 or [DHT22](https://www.ampheo.com/product/part-dht22-26872164)**, you’ll need a microcontroller (like Arduino, ESP32, STM32, or Raspberry Pi) and follow these steps:

1. Understand the Sensor

  • DHT11: Lower cost, less accurate, slower sampling (±5% humidity, 1 Hz).
  • DHT22 (AM2302): Better accuracy, wider range, faster sampling (±2–3% humidity, 0.5 Hz).

Both sensors output digital signals (not analog), using a single-wire protocol.

2. Wiring

  • VCC → 3.3V or 5V (check datasheet; DHT22 works from 3.3–6V).
  • GND → Ground.
  • DATA → Any GPIO pin on the microcontroller.
  • Pull-up resistor: 4.7k–10kΩ between DATA and VCC.

3. Communication Protocol

  • Microcontroller sends a start signal (low for ~18 ms).
  • Sensor responds with a data frame: 40 bits (5 bytes).
  • Byte 1–2: Humidity
  • Byte 3–4: Temperature
  • Byte 5: Checksum

Humidity = first two bytes (integer + decimal part depending on sensor type). You must verify with the checksum to confirm valid data.

4. Example with Arduino

#include "DHT.h"

#define DHTPIN 2       // GPIO pin connected to DHT
#define DHTTYPE DHT22  // or DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  dht.begin();
}
void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // Celsius
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C");
  delay(2000); // sensor needs ~2s interval
}

5. Example with Raspberry Pi (Python)

import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22   # or DHT11
DHT_PIN = 4  # GPIO pin number
humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
if humidity is not None and temperature is not None:
    print("Humidity={0:0.1f}%  Temp={1:0.1f}C".format(humidity, temperature))
else:
    print("Failed to retrieve data from sensor")

6. Best Practices

  • Place the sensor away from heat sources.
  • Allow 1–2 seconds between readings.
  • Use a pull-up resistor on the data line.
  • Calibrate if you need higher accuracy.

메타데이터
post_id
c00add7e37da
slug
how-do-i-measure-humidity-using-a-dht11-or-dht22-c00add7e37da
url
https://medium.com/@pqshedy33/how-do-i-measure-humidity-using-a-dht11-or-dht22-c00add7e37da
canonical_url
https://medium.com/@pqshedy33/how-do-i-measure-humidity-using-a-dht11-or-dht22-c00add7e37da
author_url
https://medium.com/@pqshedy33
status
ok
fetched_at
2026-06-26 21:52:29