PinoutSearch

Read Temperature & Humidity (DHT22 + Arduino)

Read temperature and humidity from a DHT22 sensor and print them to the Serial Monitor. Learn libraries and Serial output.

Beginner25 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

dht22_read.ino
// Read temperature + humidity from a DHT22 and print to Serial.
// Install "DHT sensor library" by Adafruit (Library Manager) first.

#include <DHT.h>

#define DHTPIN  2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
  delay(2000);                        // DHT22 refreshes every ~2 seconds
  float h = dht.readHumidity();
  float t = dht.readTemperature();    // Celsius (use readTemperature(true) for F)

  if (isnan(h) || isnan(t)) {
    Serial.println("Sensor read failed");
    return;
  }
  Serial.print("Temp: ");     Serial.print(t); Serial.print(" C   ");
  Serial.print("Humidity: "); Serial.print(h); Serial.println(" %");
}

Steps

  1. In the Arduino IDE: Tools → Manage Libraries → install "DHT sensor library" by Adafruit.
  2. Wire the DHT22: VDD to 5V, GND to GND, DATA to D2.
  3. Add a 10kΩ resistor between DATA and VDD (pull-up). Many 3-pin DHT modules already include it.
  4. Upload, then open the Serial Monitor at 9600 baud to see readings.