Library 25- AM2301 (DHT21) sensor for STM32F4

AM2301 or DHT21 is a digital sensor for measure temperature and humidity. It has temperature resolusion up to .1 degree and accuracy to .5 degree celcius. Humidity has .1% resolution and 3% accuracy. This is quite good.



  • Read temperature from sensor
  • Read humidity from sensor


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
  • TM
    • TM DELAY
    • TM GPIO
    • defines.h

VCC 3V3-5V Positive supply
GND GND Ground
DATA PD1 Data line

You can select your own data pin for STM32F4xx, add lines below in your defines.h file and edit as you want:

On pin, internal pull-up resistor is enabled, but if you have a larger distance, add external resistor about 4k7 Ohm.

Read data

First you have to initialize sensor. Do this with

You are now able to read data. First make a new instance of data typedef.

Then check if data is valid with

Functions and enumerations


AM2301 Temperature and Humidity sensor

AM2301 Temperature and Humidity sensor

Project available on Github, download library below.

TM STM32F4 AM2301 Library


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Comrada

    Hello! I’m trying to use it for DHT22, but getting an error TM_AM2301_WAITHIGH_LOOP_ERROR
    It seems it has the same parameters. Have you tried to measure with this sensor?

    • Hi,

      I didnt. But if it has the same parameters, then make sure that your clock is set correct. Check PLL parameters.
      Also, use timer for delay functions if you use GCC. How to use it, look at my delay library.

      • Comrada

        I have this picture(1). First, there is a 250us high pulse, but in datasheet there is something like on the second picture.
        And in your library:
        /* Pin output */
        /* Set pin low for ~800-1000 us */
        /* Set pin high to ~30 us */
        /* Read mode */

        How did this impulse(250us)?

  • Lucas

    Hello Tilen,

    If I want read two sensors DHT22 in the same time, how I do?


    • Hi,

      this lib supports only one sensor right now.

      • Lucas

        Hi Tilen,

        Is working. I duplicated the library.


        • This is one way, another is to create a structure for each sensor where is located. I will add this into hal library for this.

  • yousra

    hello , its the same with dht11 ??

  • David Chau

    Hi Tilen,

    I am trying to use the HAL version of the AM2301 example you provided for the Nucleo F401RE (posting here as I could not find the HAL story).
    I have implemented the SPL version of your libraries and obtained the correct temp/hum readings.
    However, when using the HAL version, the data read is incorrect and does not match the SPL version.
    I am using the predefined clock/PLL configurations you provided. However, I suspect that it is a timing issue.
    I am not sure where to start debugging, any pointer or suggestions?


    • David Chau

      I seem to have fixed the issue, the bits read were reversed.
      This could have been due to how the HAL functions read the sensor.