Arduino DHT22 Temperature & Humidity Sensor: Wiring, Code and Simulator

To build this Arduino DHT22 Temperature & Humidity Sensor project you need an Arduino Uno and a DHT22 temperature & humidity sensor. Temperature and humidity from a DHT22 on pin 2, read with the DHT library every two seconds. Make the 3 connections in the wiring table, upload the Arduino C++ code, and change the temperature and humidity sliders; the next reading shows the new values.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1DHT22 temperature & humidity sensorSDA (data) to a pin, VCC to 5V, GND to GND. Read it with the DHT library (type DHT22).

Wiring

#FromTo
1Arduino Uno 5VDHT22 temperature & humidity sensor VCC
2Arduino Uno pin 2DHT22 temperature & humidity sensor SDA
3Arduino Uno GNDDHT22 temperature & humidity sensor GND

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a DHT22 temperature & humidity sensor from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to DHT22 temperature & humidity sensor VCC.
  4. Wire 2. Connect Arduino Uno pin 2 to DHT22 temperature & humidity sensor SDA.
  5. Wire 3. Connect Arduino Uno GND to DHT22 temperature & humidity sensor GND.
  6. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  7. Run it. Press Run. Change the temperature and humidity sliders; the next reading shows the new values.

Arduino C++ code

// Weather station: read a DHT22 on pin 2 every two seconds.
#include <DHT.h>

DHT dht(2, DHT22);

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

void loop() {
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("DHT22 did not answer. Check the wiring.");
    return;
  }
  Serial.print("Temperature: ");
  Serial.print(t, 1);
  Serial.print(" C  Humidity: ");
  Serial.print(h, 1);
  Serial.println(" %");
}

What to try

Change the temperature and humidity sliders; the next reading shows the new values.

Frequently asked questions

How do I wire the DHT22 Temperature & Humidity Sensor project on an Arduino Uno?

Make 3 connections: Arduino Uno 5V to DHT22 temperature & humidity sensor VCC; Arduino Uno pin 2 to DHT22 temperature & humidity sensor SDA; Arduino Uno GND to DHT22 temperature & humidity sensor GND.

What parts do I need for this Arduino project?

An Arduino Uno and a DHT22 temperature & humidity sensor.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

Temperature and humidity from a DHT22 on pin 2, read with the DHT library every two seconds.

More Arduino Uno projects