Arduino Code and Video for DHT22 Temperature and Humidity Sensor
In this tutorial, we will learn how to use the DHT22 temperature and humidity sensor with an Arduino. The DHT22, also known as AM2302, is capable of measuring temperatures from -40 to 80 degrees Celsius and humidity from 0 to 99 percent. By following this guide, you will be able to display the temperature in Celsius, Fahrenheit, or Kelvin, as well as the humidity level.

We will be utilizing the DHT sensor library to easily read data from the sensor. This library simplifies the process of communicating with the DHT22 and allows us to access temperature and humidity values with just a few lines of code. For a more in-depth explanation, I encourage you to watch the associated video (in video at 00:00).
Hardware Explained
The primary component in this project is the DHT22 sensor module, which is a digital temperature and humidity sensor. It uses a capacitive humidity sensing element and a thermistor to measure the surrounding air. The output is a digital signal that can be read by an Arduino.
The DHT22 operates on a voltage range of 3.3 to 5 volts and communicates via a single-wire interface, making it straightforward to integrate into your projects. It also features a long transmission distance of up to 20 meters, allowing flexibility in sensor placement.
Datasheet Details
| Manufacturer | Aosong |
|---|---|
| Part number | DHT22 (AM2302) |
| Logic/IO voltage | 3.3 - 5 V |
| Supply voltage | 3.3 - 5 V |
| Measuring range (Temperature) | -40 to +80 °C |
| Measuring range (Humidity) | 0 to 99 % |
| Accuracy (Temperature) | ±0.5 °C |
| Accuracy (Humidity) | ±2 % at 25 °C |
| Resolution | 0.1 °C / 0.1 % |
| Transmission distance | up to 20 m |
| Package | 4-pin module |
- Ensure proper power supply between 3.3V and 5V.
- Use a 10K pull-up resistor between the data pin and power.
- Keep sensor wires short for accurate readings.
- Avoid rapid polling; allow for delays between readings.
- Be cautious of environmental factors affecting readings.
Wiring Instructions

To wire the DHT22 sensor, start by connecting the power pin (pin 1) of the sensor to the 5V output on the Arduino. Next, connect the ground pin (pin 4) to one of the GND pins on the Arduino. The data pin (pin 2) should be connected to digital pin 2 on the Arduino for communication. Additionally, place a 10K resistor between the data pin and the power pin to ensure stable readings.
In case you are using a different pin for data, remember to update the code accordingly by changing the #define DHTPIN value to match the chosen pin. The setup should be straightforward, following these connections will ensure that your sensor operates correctly.
Code Examples & Walkthrough
The following code initializes the DHT22 sensor and reads temperature and humidity values. First, we include the DHT library and define the pin to which the sensor is connected:
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
Here, the variable DHTPIN is set to 2, which indicates that the data pin of the sensor is connected to digital pin 2 of the Arduino. The DHTTYPE defines the type of sensor used, which in this case is DHT22.
Next, in the setup() function, we initialize the sensor and the serial monitor:
void setup() {
Serial.begin(9600);
dht.begin();
}
This code snippet initializes the serial communication and prepares the DHT sensor for reading. The Serial.begin(9600) sets the baud rate for the serial communication.
In the loop() function, we can read and display the temperature and humidity values:
Serial.print("Temperature: ");
Serial.print(getTemp("c")); // Celsius
Serial.print(" *C ");
Serial.print(getTemp("h")); // Humidity
Serial.println(" % ");
This part of the code prints the temperature in Celsius and the humidity percentage to the serial monitor. The function getTemp() is used to retrieve the requested data based on the parameter passed.
Demonstration / What to Expect
Once everything is set up and the code is uploaded, you should see the temperature and humidity readings displayed on the serial monitor. The DHT22 may take a moment to stabilize, so ensure you allow a few seconds between readings (in video at 15:00). If there are any issues with the readings, check your wiring and ensure the connections are secure. Common pitfalls include incorrect pin assignments or power supply issues.
Video Timestamps
- 00:00 - Introduction to the DHT22 Sensor
- 01:30 - Wiring the Sensor
- 03:00 - Code Explanation
- 04:30 - Code Demonstration
- 06:00 - Conclusion
++
/*
* This is the Arduino code for the DHT22 module to read temperature and humidity.
* This code can display temperature in:
* getTemp("c") is used to get Celsius.
* getTemp("f") is used to get Fahrenheit.
* getTemp("k") is used for Kelvin.
* getTemp("hif") is used to get Fahrenheit.
* getTemp("hic") is used to get Celsius.
* getTemp("h") is used to get humidity.
* Watch the video https://youtu.be/oi_GPSLjgBY
* Other Arduino library and videos https://robojax.com
*
* Written/updated by Ahmad Shamshiri for Robojax.com Video
* Date: Jan 08, 2018, in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: This code is "AS IS" and for educational purposes only.
*
* original source code : https://github.com/adafruit/DHT-sensor-library
*/
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster processors.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx Robojax test!");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Robojax.com test video
Serial.print("Temperature: ");
Serial.print(getTemp("c"));
Serial.print(" *C ");
Serial.print(getTemp("f"));
Serial.println (" *F");
Serial.println("-----------------");
Serial.print("Heat index: ");
Serial.print(getTemp("hic"));
Serial.print(" *C ");
Serial.print(getTemp("hif"));
Serial.println(" *F");
Serial.print(getTemp("k"));
Serial.println(" *K");
Serial.println("-----------------");
Serial.print("Humidity: ");
Serial.print(getTemp("h"));
Serial.println(" % ");
Serial.println("===========================");
}
/*
* getTemp(String req)
* returns the temperature related parameters
* req is string request
* This code can display temperature in:
* getTemp("c") is used to get Celsius.
* getTemp("f") is used to get Fahrenheit.
* getTemp("k") is used for Kelvin.
* getTemp("hif") is used to get Fahrenheit.
* getTemp("hic") is used to get Celsius.
* getTemp("h") is used to get humidity.
*/
float getTemp(String req)
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor).
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return 0.0;
}
// Compute heat index in Kelvin
float k = t + 273.15;
if(req =="c"){
return t;//return Celsius
}else if(req =="f"){
return f;// return Fahrenheit
}else if(req =="h"){
return h;// return humidity
}else if(req =="hif"){
return hif;// return heat index in Fahrenheit
}else if(req =="hic"){
return hic;// return heat index in Celsius
}else if(req =="k"){
return k;// return temperature in Kelvin
}else{
return 0.000;// if no request found, return 0.000
}
}
资源与参考
文件📁
Arduino 库(zip 格式)
-
DHT22 PCB module red
DHT22-module-red.fzpz0.01 MB
Fritzing 文件
-
DHT22 Humidity and Temperature Sensor
DHT22 Humidity and Temperature Sensor.fzpz0.01 MB -
DHT22 PCB module red
DHT22-module-red.fzpz0.01 MB
用户手册
-
DHT22 Temperature and Humidity sensor user's manual
robojax-DHT22_manual.pdf0.36 MB