Arduino Nano DHT22 Simulator: Wiring, Code and a Live Circuit
This is a DHT22 temperature and humidity sensor wired to an Arduino Nano, 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 Nano 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 Nano
Every connection in the circuit above, with the board’s own pin names.
| DHT22 pin | Arduino Nano 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 Nano
A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.
On a real bench
The Nano plugs into the breadboard itself, straddling the center channel, so the DHT22 goes in a few columns along and every connection is a short jumper. The silkscreen labels its pins D2 to D13 and A0 to A7; in code D2 is simply 2. A Nano clone with a CH340 USB chip needs that driver on a real computer.
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 Nano");
}
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 Nano 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 is the USB port, as on the Uno; a clone with a CH340 chip needs that driver on a real computer, which the simulator does not.
Common mistakes with the DHT22 on the Arduino Nano
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.
Picking A6 or A7
A6 and A7 look like the other analog pins but are inputs to the ADC and nothing else. the DHT22’s DATA line on either of them never sees a digital level. Any of D2 to D13 or A0 to A5 works.
Questions and answers
Can I simulate a DHT22 with an Arduino Nano online?
Yes. The circuit at the top of this page is an Arduino Nano 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 Nano 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 Nano 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.