Arduino Code and Video for DHT22 Temperature and Humidity Sensor

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.

DHT22 sensor with PCB-1

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

ManufacturerAosong
Part numberDHT22 (AM2302)
Logic/IO voltage3.3 - 5 V
Supply voltage3.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
Resolution0.1 °C / 0.1 %
Transmission distanceup to 20 m
Package4-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

Arduino wiring for DHT22 sensor
Arduino wiring for DHT22 sensor

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

图像

Arduino wiring for DHT22 sensor
Arduino wiring for DHT22 sensor
DHT22 sensor with PCB-1
DHT22 sensor with PCB-1
DHT22 with PCB red
DHT22 with PCB red
DHT22 sensor no PCB
DHT22 sensor no PCB
54-Arduion code DHT22 Temperature and Humidity Sensor Module
语言: C++
已复制!

文件📁

Arduino 库(zip 格式)

Fritzing 文件

用户手册