Arduino Mega DHT22 Simulator: Wiring, Code and a Live Circuit
This is a DHT22 temperature and humidity sensor wired to an Arduino Mega 2560, 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 Mega 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 Mega
Every connection in the circuit above, with the board’s own pin names.
| DHT22 pin | Arduino Mega 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 ATmega2560 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 Mega
Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.
On a real bench
The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The DHT22 uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.
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 Mega");
}
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 Mega 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; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.
Common mistakes with the DHT22 on the Arduino Mega
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.
Taking the serial or I2C pins
Pins 0 and 1 are the USB serial port, 14 to 19 are Serial1 to Serial3 and 20 and 21 are I2C. the DHT22’s DATA line works on any of them until the sketch needs that port, so start on pin 2 and up.
Questions and answers
Can I simulate a DHT22 with an Arduino Mega 2560 online?
Yes. The circuit at the top of this page is an Arduino Mega 2560 with a DHT22, running in your browser on a simulated ATmega2560. 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 Mega 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 Mega 2560 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.