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
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | DHT22 temperature & humidity sensor | SDA (data) to a pin, VCC to 5V, GND to GND. Read it with the DHT library (type DHT22). |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno 5V | DHT22 temperature & humidity sensor VCC |
| 2 | Arduino Uno pin 2 | DHT22 temperature & humidity sensor SDA |
| 3 | Arduino Uno GND | DHT22 temperature & humidity sensor GND |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a DHT22 temperature & humidity sensor from the parts palette.
- Wire 1. Connect Arduino Uno 5V to DHT22 temperature & humidity sensor VCC.
- Wire 2. Connect Arduino Uno pin 2 to DHT22 temperature & humidity sensor SDA.
- Wire 3. Connect Arduino Uno GND to DHT22 temperature & humidity sensor GND.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- 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
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator