Arduino Mega - DHT22

This guide shows you how to use Arduino Mega with the DHT22 temperature and humidity sensor. We will learn in detail:

Arduino Mega and DHT22 sensor module

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×DHT22 Temperature Humidity Sensor Module
1×10 kΩ Resistor
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of DHT22 Temperature and Humidity Sensor

DHT22
Temperature Range -40°C to 80°CW
Temperature Accuracy ± 0.5°C
Humidity Range 0% to 100%
Humidity Accuracy ± 2 to 5%
Reading Rate 0.5Hz (once every 2 seconds)
Operating Voltage3 to 5V

Pinout

DHT22 has two types: the sensor and the module.

DHT22 temperature and humidity sensor Pinout

The DHT22 sensor has four pins:

  • GND pin: connect to ground (0V).
  • VCC pin: connect to the supply voltage (5V or 3.3V).
  • DATA pin: this pin lets the sensor talk to the Arduino Mega.
  • NC pin: not connected; ignore it.

The DHT22 sensor has three pins:

  • GND pin: connect to ground (0V)
  • VCC pin: connect to power (5V or 3.3V)
  • DATA pin: used to send and receive data between the sensor and the Arduino Mega

Some makers sell the DHT22 sensor as a small module with three pins: GND, VCC, and DATA (or -, +, and OUT).

Wiring Diagram

Use a pull-up resistor of about 5k–10k ohms on the data line between the DHT22 sensor and the Arduino Mega to keep the line high and allow communication.

Arduino Mega - DHT22 Sensor Wiring

The wiring diagram between Arduino Mega DHT22 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Mega - DHT22 Module Wiring

Most DHT22 sensor modules already have a resistor built in, so you don’t need to add one. This saves you time and effort in wiring or soldering.

The wiring diagram between Arduino Mega DHT22 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

How To Program For DHT22 Temperature Sensor

Both the DHT22 sensor and its module are programmed in the same way.

  • Install the library
#include <DHT.h>
  • Choose the Arduino Mega pin that connects to the DHT sensor:
#define DHT22_PIN 2
  • Make a DHT22 sensor object
DHT dht22(DHT22_PIN, DHT22);
  • Put the sensor in place
dht22.begin();
  • Check how humid it is.
float humi = dht22.readHumidity();
  • Check the temperature in degrees Celsius.
float tempC = dht22.readTemperature();
  • Check the temperature in Fahrenheit
float tempF = dht22.readTemperature(true);

Arduino Mega Code - DHT22

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-dht22 */ #include <DHT.h> #define DHT22_PIN 2 // The Arduino Mega pin connected to DHT22 DHT dht22(DHT22_PIN, DHT22); void setup() { Serial.begin(9600); dht22.begin(); // initialize the DHT22 sensor } void loop() { // wait a few seconds between measurements. delay(3000); // read humidity float humidity = dht22.readHumidity(); // read temperature as Celsius float tempC = dht22.readTemperature(); // read temperature as Fahrenheit float tempF = dht22.readTemperature(true); // check if any reads failed if (isnan(humidity) || isnan(tempC) || isnan(tempF)) { Serial.println("Failed to read from DHT22 sensor!"); } else { Serial.print("DHT22# Humidity: "); Serial.print(humidity); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); } }

Detailed Instructions

Follow these steps one by one:

  • Connect the parts as shown in the diagram.
  • Connect the Arduino Mega board to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the COM port.
  • Click the Libraries icon on the left side of the Arduino IDE.
  • Type DHT in the search box and find the Adafruit DHT sensor library.
  • Click Install to add the library.
Arduino Mega DHT sensor library
  • You need to install extra libraries.
  • Click the Install All button to install all needed libraries.
Arduino Mega Adafruit Unified sensor library
  • Pick and copy the code for your sensor, then open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Mega.
  • Change the temperature near the sensor by making it hotter or cooler.
  • See the results in the Serial Monitor.
COM6
Send
DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F DHT22# Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F DHT22# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!