DHT22 Temperature-Humidity Sensor
| ||
Overview
This is a calibrated digital temperature and humidity module with onboard sensor DHT22 (AM2302), which features higher accuracy and a wider measuring range than DHT11.
It can be used for detecting ambient temperature and humidity, through the standard single-wire interface.
Specification
- Sensor: DHT22
- Temperature
- Resolution: 0.1°C
- Accuracy: ±0.5℃
- Measuring range : -40°C ~ 80°C
- Humidity
- Resolution: 0.1%RH
- Accuracy : ±2%RH (25°C)
- Measuring range : 0%RH ~ 99.9%RH
- Operating voltage : 3.3V ~ 5.5 V
- Recommended storage condition
- Temperature : 10°C ~40°C
- Humidity : 60%RH or below
Working Principle
The schematic diagram is shown below.
Here are the pin definitions.
Pin | Symbol | Descriptions |
1 | VCC | Power supply (3.3V-5.5V) |
2 | GND | Ground |
3 | DOUT | Data output, connected to the pin SDA of AM2302 |
The AM2302 uses the simplified single-bus technology for communication, in which only one data line is applied for data exchange and data control in the system. In applications, an external pull-up resistor, about 5.1kΩ, is usually required. When the bus is idle, its status will switch to HIGH. The SDA is used for data communication and synchronization between the microprocessor and the AM2302. It adopts a single-bus data format, 40 bits of data in one transmission, high bit first out. The corresponding timing diagram is shown below.
The AM2302 data and signal format definition are listed below.
Name | Single-bus data and signal format |
Start signal | The microprocessor sets the SDA to LOW for a period of time (at least 800μs) [1] to inform the sensor to prepare the data. |
Response signal | The sensor sets the SDA to LOW for 80μs, and then HIGH for 80μs, to respond to the start signal. |
Data format | After receiving the start signal, the sensor reads out a string of data (40 bits) through SDA, High bit first out. |
Humidity | The humidity resolution is 16 Bits, high bit first out; The value read out by the sensor is 10 times higher than the actual humidity. |
Temp. | The temperature resolution is 16 Bits, high bit first out; The value read out by the sensor is 10 times higher than the actual temperature.
When the MSB(Bit15) is "1", it indicates a negative temperature; When the MSB (Bit15) is "0", it indicates a positive temperature; The other bits (Bit14 ~ bit 0) indicate the detected temperature value. |
Parity bit | Parity bit = humidity high + humidity low + temperature high + temperature low |
- Single-bus communication timing
When the host (MCU) sends out a start signal (the SDA is set to LOW for at least 800μs), AM2302 will switch from Sleep mode to High-speed mode. After the signal is ended, the AM2302 sends a response signal, and then outputs a string of 40 bits of data via the SDA, high bit first; the outputted data is in the format of Humidity high, Humidity low, Temperature high, Temperature low, and a Parity bit. Information collection starts once the data sending ends. After the collection is finished, the sensor will switch to Sleep mode automatically, waiting for the next communication. (Notes: The data format of DHT22 (AM2302) is different from that of DHT11.)
- Example of Peripherals reading
We will present the steps for data reading in the communication between the host and the sensor.
Step 1
After the AM2302 is powered up (please wait for 2s for AM2302 to become stable. In this period, no command will be sent out on device reading.), the sensor tests the environment temperature and humidity and records relative data. When finished, the sensor enters the Sleep mode automatically. And the SDA data line of AM2302 is pulled up and remains HIGH as the effect of the pull-up resistor. At this moment, the pin SDA of AM2302 is in the INPUT state, detecting any possible external signal.
Step 2
The Microprocessor I/O is set to OUTPUT and outputs a LOW level for more than 800us (The typical hold time is 1ms). Then, the microprocessor I/O is set to INPUT and the bus will be released. At this moment, the microprocessor I/O (the SDA data line of AM2302) goes HIGH as the effect of the pull-up resistor. After the host released the bus, AM2302 sends out a response, a LOW level of 80ms, and then outputs a HIGH level of 80ms to inform the peripheral to receive data. The signal transmission is shown below:
Step 3
After the AM2302 sends the response, the SDA outputs a string of 40 bits of serial data continuously and the microprocessor receives the data according to the changes in the I/O level.
Bit data "0" signal: the level is LOW for 50ms and HIGH for 26-28ms;
Bit data "1" signal: the level is LOW for 50ms and HIGH for 70ms;
The relative signal diagram is shown below:
After the SDA of AM2302 outputted the 40 bits of data, it remains at LOW level for 50ms, and then switches to INPUT state and goes HIGH as the effect of the pull-up resistor. At the same time, the AM2302 internally re-tests the environmental temperature and humidity and records the relative data. When finished, the MCU will enter the Sleep mode automatically. Only when the MCU receives the new start signal from the host, the sensor will wake up and enter the working state.
Video
Pico Quick Start
Firmware Download
Firmware Download
Text Tutorial
Introduction
MicroPython Series
- 【MicroPython】 machine.Pin Function
- 【MicroPython】 machine.PWM Function
- 【MicroPython】 machine.ADC Function
- 【MicroPython】 machine.UART Function
- 【MicroPython】 machine.I2C Function
- 【MicroPython】 machine.SPI Function
- 【MicroPython】 rp2.StateMachine
C/C++ Series
For C/C++, it is recommended to use Pico VS Code for development. This is a Microsoft Visual Studio Code extension designed to make it easier for you to create, develop, and debug projects for the Raspberry Pi Pico series development board. Whether you are a beginner or an experienced professional, this tool can help you confidently and easily develop Pico. Below we will introduce how to install and use the extension.
- Official website tutorial: https://www.raspberrypi.com/news/pico-vscode-extension/.
- This tutorial is applicable to Raspberry Pi Pico, Pico2, and our company's RP2040 and RP2350 series development boards.
- The development environment defaults to Windows as an example. For other environments, please refer to the official website tutorial for installation.
Arduino IDE Series
Install Arduino IDE
-
Download the Arduino IDE installation package from Arduino website.
-
Just click on "JUST DOWNLOAD".
-
Click to install after downloading.
-
Note: You will be prompted to install the driver during the installation process, we can click Install.
Install Arduino-Pico Core on Arduino IDE
-
Open Arduino IDE, click the File on the left corner and choose "Preferences".
-
Add the following link in "Additional boards manager URLs", then click OK.
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
Note: If you already have the ESP32 board URL, you can separate the URLs with commas like this:https://dl.espressif.com/dl/package_esp32_index.json,https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
-
Click on Tools -> Board -> Board Manager -> Search for pico, it shows installed since my computer has already installed it.
Upload Demo At the First Time
-
Press and hold the BOOTSET button on the Pico board, connect the Pico to the USB port of the computer via the Micro USB cable, and release the button when the computer recognizes a removable hard drive (RPI-RP2).
- Download the demo from #Resource, open the D1-LED.ino under arduino\PWM\D1-LED path.
-
Click Tools -> Port, remember the existing COM, do not need to click this COM (different computers show different COM, remember the existing COM on your computer).
-
Connect the driver board to the computer with a USB cable, then click Tools -> Ports, select uf2 Board for the first connection, and after the upload is complete, connecting again will result in an additional COM port.
-
Click Tools -> Board -> Raspberry Pi Pico/RP2040 -> Raspberry Pi Pico.
-
After setting, click the right arrow to upload.
- If you encounter problems during the period, you need to reinstall or replace the Arduino IDE version, uninstall the Arduino IDE clean, after uninstalling the software you need to manually delete all the contents of the folder C:\Users\[name]\AppData\Local\Arduino15 (you need to show the hidden files in order to see it) and then reinstall.
Open Source Demo
- MicroPython Demo (GitHub)
- MicroPython Firmware/Blink Demo (C)
- Official Raspberry Pi C/C++ Demo
- Official Raspberry Pi MicroPython Demo
- Arduino Official C/C++ Demo
Use with Raspberry Pico
Hardware Connection
Sensor | Pico | Description |
---|---|---|
VCC | 3.3V | Power input |
GMD | GND | Power ground |
DOUT | GP15 | Digital data output |
Download examples
Use Raspberry Pi as the host device. Open a terminal and run the following commands to download the example.
sudo apt-get install p7zip-full cd ~ sudo wget https://files.waveshare.com/upload/5/57/DHT22-Temperature-Humidity-Sensor-code.7z 7z x DHT22-Temperature-Humidity-Sensor-code.7z -o./DHT22-Temperature-Humidity-Sensor-code cd ~/DHT22-Temperature-Humidity-Sensor-code cd Pico/c/build/
Examples
C codes
The following tutorial is for operating on a Raspberry Pi. However, due to CMake's multi-platform and portable nature, it can also be successfully compiled on a PC. The steps may differ slightly, so users will need to make adjustments accordingly.
- Go into the c directory.
cd ~/DHT22-Temperature-Humidity-Sensor-code/Pico/c/
- Create and enter the build directory within the folder, then add the SDK. Here, ../../pico-sdk refers to the directory of your SDK. In our example program, the build directory already exists, so you can enter it directly.
cd build export PICO_SDK_PATH=../../pico-sdk (Note: Make sure to correctly specify the path to your own SDK.)
- Generate Makefile.
cmake ..
Run it to generate the executable file. The first compilation may take a while.
make -j9
- After building, a uf2 file is generated.
- Press and hold the button of Pico, connect it to Raspberry Pi then release the button.
- Copy/Drag the uf2 file to the portable disk (RPI-RP2) recognized.
cp main.uf2 /media/pi/RPI-RP2/
Micropython codes
Use in Windows
- 1. Press and hold the BOOTSET button on the Pico board, connect the Pico to the USB port of the computer through the Micro USB cable, and release the button after the computer recognizes a removable hard disk (RPI-RP2).
- 2. Copy the Rp2-pico-20210418-v1.15 file in the python directory to the recognized removable disk (RPI-RP2).
- 3. Open Thonny IDE (Note: Use the latest version of Thonny, otherwise there is no Pico support package, the latest version under Windows is v3.3.3).
- 4. Click Tools -> Settings -> Interpreter, and select Pico and the corresponding port as shown in the figure.
- 5. File -> Open -> the corresponding .py file, click to run, as shown in the following figure:
This demo provides a simple program...
Use in Raspberry Pi
- Flash the Micropython firmware first
- Open the Thonny IDE (Menu -> Programming -> Thonny Python IDE).
- 【Optional】If the Thonny IDE in the Raspberry Pi is not the new version that supports Pico, please upgrade it first.
sudo apt upgrade thonny
- Configure Interpreter, choose Tools -> Options... -> Interpreter, choose MicroPython (Raspberry Pi Pico) and the ttyACM0 port.
- Click File -> Open.. and browser the Micropython codes (Photo Interrupter Sensor.py) to run the codes.
Expected result
- The humidity and temperature data are printed to the serial port:
The STM32 examples are based on the STM32F103RBT6 and the STM32H743. The connection provided below is based on the STM32F103RB. If you need to use other STM32 boards, you may need to change the hardware connection and port the code yourself.
Use with STM32
Hardware connection
Sensor | STM32 | Description |
---|---|---|
VCC | 3.3V | Power input |
GMD | GND | Power ground |
DOUT | PA0 | Digitial data output |
Examples
The examples are developed based on the HAL libraries. Download the Demo codes archive to your PC. Unzip and find the STM32 project from DHT22-Temperature-Humidity-Sensor-code\STM32\STM32F103RB\MDK-ARM.
- Open the DHT22 Sensor.uvprojx file by Keil.
- Build and the project.
- Program the project to your STM32 board.
- Connect the UART1 of your STM32 board to the PC and check the serial data by SSCOM software.
The Arduino example is written for the Arduino UNO. If you want to connect it to other Arduino boards, you may need to change the connection.
Use with Arduino
Hardware connection
Sensor | Arduino | Description |
---|---|---|
VCC | 5V | Power input |
GMD | GND | Power ground |
DOUT | D2 | Digital data output |
Examples
- Download the demo codes to your PC and unzip them.
- Install the Arduino IDE on your PC.
- Go into DHT22-Temperature-Humidity-Sensor-cod/Arduino/.
- Run the .ino file.
- Select the correct Board and the Port.
- Build the project and upload it to the board.
- Open the serial monitor of the Arduino IDE or the SSCOM software and check the serial data.
Resources
FAQ
Support
Technical Support
If you need technical support or have any feedback/review, please click the Submit Now button to submit a ticket, Our support team will check and reply to you within 1 to 2 working days. Please be patient as we make every effort to help you to resolve the issue.
Working Time: 9 AM - 6 PM GMT+8 (Monday to Friday)