Wiring guide

How to wire a DHT22 to an Arduino Mega

To wire a DHT22 to an Arduino Mega, connect VCC to 5V, DATA to pin 2 and GND to GND. The Arduino Mega is a 5 V board, so the sensor runs from 5 V and DATA goes straight to the pin with no level shifter. The sketch below uses the Adafruit DHT sensor library (DHT.h) library. It prints the temperature and the humidity on the serial monitor every two seconds.

  • 5 V logic
  • Library: DHT.h
  • 3 wires
Arduino Mega · DHT22live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

DHT22 to Arduino Mega wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

DHT22 pinArduino Mega pinWire colorWhy
VCC (+)5VredThe DHT22 is happy anywhere from 3.3 to 6 V; 5 V gives the longest cable run.
DATA (out)pin 2amberOne wire, both directions: the board pulls it low to ask, the sensor answers with 40 bits.
GND (-)GNDblueA shared ground, or the board cannot read the sensor’s levels.
Voltage

Power and logic levels for DHT22 and Arduino Mega

Logic level

The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The DHT22 takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.

This pair

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

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.

The sketch

The working code for the Arduino Mega

Library: Adafruit DHT sensor library (DHT.h). This is the sketch the circuit above runs.

DHT22 on Arduino Mega pin 2
#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(" %");
}
Output

What you should see

Open the serial monitor at 9600 baud. The sketch waits two seconds, which is how often a DHT22 can measure, and then prints one line per reading. The sensor starts at 22 degrees C and 50 percent humidity, and the line follows its two sliders when you drag them.

If a read fails the sketch prints No reading from the DHT22 instead of a number. That is what a wrong DATA pin or a missing pull-up looks like.

The serial monitor prints this, recorded from the circuit above left at its defaults:

DHT22 on the Arduino Mega
Temperature: 22.0 C  Humidity: 50.0 %
Pitfalls

Common mistakes wiring a DHT22 to an 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. The Adafruit library turns on the board’s internal pull-up, which carries a short wire, so in the editor a sensor with its pullup property set to false still reads; but that pull-up is weak, tens of kilohms, so on the bench add a 10 k resistor from DATA to 5V for any real cable run.

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.

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.