Interfacing DHT22 sensor

The DHT22 (also known as AM2302) is a cheap temperature and humidity sensor with an impressive performance! It uses a capacitive humidity sensor and a thermistor to measure the air, and send out a digital signal on the data pin (no analog input pins needed). Its fairly simple to use, but requires careful timing to grab data. The only real CON of this sensor is that you can only get new data from it once every 2 seconds. It is provided with a single wire digital interface. The sensor is calibrated and only requires a single pull-up resistor (any value between 3.6k and 10k should work fine) on the dataline to communicate correctly. Compared to the cheaper (and wider diffused) DHT11, this module is much more precise, more accurate and works in a bigger range of temperature/humidity.

dht22_pinout

Technical data

  • 3.3 to 5.5V power and I/O
  • 2.5mA max current use during conversion (while requesting data)
  • 0-100% humidity readings with 2-5% accuracy
  • -40 to 80°C temperature readings ±0.5°C accuracy
  • No more than 0.5 Hz sampling rate (once every 2 seconds)
  • Body size 27mm x 59mm x 13.5mm (1.05″ x 2.32″ x 0.53″)
  • 4 pins, 0.1″ spacing
  • for full datasheet click here

Performance Tables

dht22_table_perf

How to interface

Interfacing the DHT22 sensor is pretty straightforward. The single line interface, requires only one GPIO of the microcontroller but has the CON to be more complex to cope with. The same GPIO must be configured has an output to initialize the communication and then switched to an input to receive data, and then again for the each other reading. In this project I’ll use the internal pull-up resistor built-in in Cortex-M0 cores, thus one less external required component.

To debug the whole system and read out the sensor readings, I’ve used an STM32F051’s USART module and an Arduino Mini has “USB-to-Serial Adapter” (i.e. burned with “Blink” example only).

A serial 470ohm resistor is used to avoid faults due to misconfiguration (for example configuring a GPIO as output that should be an input) and to protect in case of different voltage. This resistor is used between:

  • DHT22 DATA pin and STM32F0-Discovery board
  • Arduino RX pin and STM32F0-Discovery boa
  • Arduino TX pin and STM32F0-Discovery board

dht22_whole_bb

Connections are simple:

  • Arduino GND to STM32F0-Discovery GND
  • Arduino TX (pin 0) to resistor to STM32F0-Discovery pin PA9
  • Arduino RX (pin 1) to resistor to STM32F0-Discovery pin PA10
  • DHT22 GND to STM32F0-Discovery GND
  • DHT22 VCC to STM32F0-Discovery 5V
  • DHT22 DATA to resistor to STM32F0-Discovery pin PC10

The debug is made via serial interface (115200 bps, 8N1) (thanks to Arduino mini board) and will look like this

dht22_terminal

Source Code

Starting from a Standard Peripheral Library template (or, better, the example provided in my other post), only three files are of interest:

  1. main.c
  2. dht22.h
  3. dht22.c

You can download directly from here: dht22_code

I hope you have enjoyed!

6 thoughts on “Interfacing DHT22 sensor

  1. I am new to programming microcontroller.
    When I tried to compile your code for STM32F100RB board with Keil, I cannot find
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    Should I comment them out or .. ?

    • GPIO_InitStructure is a structure imported from standard peripheral libraries and is used to initialize the corresponding GPIO. Without initialization, the GPIO simply doesn’t work. You need to find the appropriate library for your microcontroller and import it, probably is named stm32f10xx_gpio.h and .c or something similar

  2. I want to use this code for the kit stm32f4 discovery, I find the functions TIM_GetCounter(DHT22_TIM) and TIM_SetCounter(DHT22_TIM, 0)

    • i’m not sure to understand what do you need. DHT22_TIM is a placeholder (#define DHT22_TIM …) for the STM32’s hardware timer used for the DHT22’s communication

  3. Hi Fabio, can I ask you something ? I try to convert your code to use for STM32F4 Discovery board but It ‘s not work, Can you help me and do I need config any thing more?
    Thank you

    • Hi Anh! Can you be more specific? Are you using HAL libraries or the old standard peripheral libraries?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.