DIY ELECTRONICSTR

← All projects

DHT22 Temperature and Humidity Meter

Beginner20 min

A project that measures humidity and temperature, in both Celsius and Fahrenheit, with a DHT22 sensor and writes them to the Serial Monitor every two seconds. It is more precise than the DHT11 and has a wider measuring range. Before uploading the code, install Adafruit's "DHT sensor library" in the Arduino IDE (and the "Adafruit Unified Sensor" library it asks for).

Get it on Google Play

What you need

Wiring steps

  1. 1

    Power the sensor

    Power the sensor

    Plug a red jumper wire into the 5V pin on the Arduino's POWER header. With the DHT22's front facing you, connect the other end to its top pin, pin 1 (VCC).

    • Arduino 5V → DHT22 VCC (red wire)
  2. 2

    Connect the data line

    Connect the data line

    Plug a blue jumper wire into the Arduino's digital pin 2. Connect its other end to the DHT22's pin 2 (DATA), just below. The code reads the sensor on this pin (DHTPIN 2).

    • Arduino pin 2 → DHT22 DATA (blue wire)
  3. 3

    Ground (GND) connection

    Ground (GND) connection

    Plug a black jumper wire into the GND pin on the Arduino's digital header, next to pin 13. Connect its other end to the DHT22's bottom pin, pin 4 (GND). Pin 3 stays free; do not connect anything to it.

    • Arduino GND → DHT22 GND (black wire)

Arduino sketch

Download the sketch (.ino)

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin(); // initialize the sensor
}

void loop() {
  // wait a few seconds
  // between measurements.
  delay(2000);

  // read humidity
  float humi  = dht.readHumidity();
  // read temperature as Celsius
  float tempC = dht.readTemperature();
  // read temperature as Fahrenheit
  float tempF = dht.readTemperature(true);

  // check if any reads failed
  if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print("%");

    Serial.print("  |  ");

    Serial.print("Temperature: ");
    Serial.print(tempC);
    Serial.print("°C ~ ");
    Serial.print(tempF);
    Serial.println("°F");
  }
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
Humidity: 45.50%  |  Temperature: 23.40°C ~ 74.12°F
Humidity: 45.60%  |  Temperature: 23.40°C ~ 74.12°F

Similar projects

In the app, tick off every wire, match the parts to your inventory and pick up where you left off.

Get it on Google Play