Arduino Uno DHT22 Simulator: Wiring, Code and a Live Circuit
This is a DHT22 temperature and humidity sensor wired to an Arduino Uno, running live: the board reads the sensor every two seconds and prints both numbers on the serial monitor. Open it in the editor and drag the sensor’s sliders: the next reading follows them.
The Arduino Uno is a 5 V board, so the sensor runs from its 5V pin and DATA goes to pin 2. Below are the wiring, the sketch, and the mistakes people make with this pair.
- 5 V logic
- Library: DHT.h (Adafruit)
- Data on pin 2
- Powered from 5V
Wiring the DHT22 to the Arduino Uno
Every connection in the circuit above, with the board’s own pin names.
| DHT22 pin | Arduino Uno pin | Why |
|---|---|---|
| VCC (+) | 5V | The DHT22 is happy anywhere from 3.3 to 6 V; 5 V gives the longest cable run. |
| DATA (out) | pin 2 | One wire, both directions: the board pulls it low to ask, the sensor answers with 40 bits. |
| GND (-) | GND | A shared ground, or the board cannot read the sensor’s levels. |
On a 5 V board the whole circuit is 5 V: the module’s own pull-up holds DATA at 5 V between readings and the ATmega328P reads that directly. A bare four-pin DHT22 has no pull-up, so add a 10 k resistor from DATA to 5V.
Pins to leave alone on the Arduino Uno
Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.
On a real bench
On a real bench the Uno stands beside the breadboard: the DHT22 goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#include "DHT.h"
#define DHT_PIN 2 // DATA, pin 2
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println("DHT22 on the Arduino Uno");
}
void loop() {
delay(2000); // the DHT22 measures every 2 s
float celsius = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(celsius) || isnan(humidity)) { // a failed read is NAN
Serial.println("No reading from the DHT22");
return;
}
Serial.print("Temperature: ");
Serial.print(celsius, 1);
Serial.print(" C Humidity: ");
Serial.print(humidity, 1);
Serial.println(" %");
}
DHT dht(DHT_PIN, DHT_TYPE) names the pin and the sensor type, and dht.begin() releases the line so the module’s pull-up holds it high. The Adafruit DHT library’s calls compile here as written, on the Arduino Uno as on every board in the simulator.
readTemperature() and readHumidity() each return a float, or NAN when the sensor did not answer. Both come from the same 40-bit frame, so a failed read is NAN in both, and the sketch checks isnan() on both before printing anything. A failed reading is normal on a real bench now and then, and the first one after power-up always fails.
The two-second delay is the sensor’s own pace: a DHT22 starts a new measurement at most every two seconds and hands back the last one in between. Serial.begin(9600) opens the serial monitor. Serial goes over the USB cable, and the serial monitor in the editor reads it at whatever baud rate the sketch sets.
Common mistakes with the DHT22 on the Arduino Uno
Reading faster than every two seconds
A DHT22 needs two seconds between measurements. Ask sooner and it returns the previous reading, or nothing at all, so a fast loop looks like a stuck sensor. Keep the delay(2000), or time the reads with millis().
Leaving out the pull-up on a bare sensor
The blue three-pin module has a pull-up built in. The bare four-pin DHT22 does not, and without a 10 k resistor from DATA to 5V every reading fails. In the editor, set the sensor’s pullup property to false to see exactly that.
Using pins 0 and 1
Pins 0 and 1 carry the USB serial port on a real Uno. the DHT22’s DATA line wired there fights every upload and every Serial.print(). Start at pin 2.
Questions and answers
Can I simulate a DHT22 with an Arduino Uno online?
Yes. The circuit at the top of this page is an Arduino Uno with a DHT22, running in your browser on a simulated ATmega328P. The sensor’s sliders set the temperature and humidity it reports, so you can test a thermostat or an alarm threshold without heating anything. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Which Arduino Uno pin should the DHT22 DATA wire use?
Any digital pin that can be both an output and an input. This circuit uses pin 2. Avoid pins 0 and 1, which carry the USB serial port.
How do I use a DHT11 instead?
Change DHT22 to DHT11 in the #define and wire it the same way. The DHT11 reads whole degrees and whole percent, from 0 to 50 C, and it can be asked once a second. In the editor, set the sensor’s model property to dht11.
Why does the serial monitor say there is no reading?
Either the sketch asked within the sensor’s first second after power-up, the DATA wire is on a different pin from the one in the #define, or the pull-up is missing. On an Arduino Uno check that DHT_PIN is 2.
Build your own DHT22 project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.