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.

Library

Features

  • Read temperature from sensor
  • Read humidity from sensor

Dependencies

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

AM2301 STM32F4xx DESCRIPTION
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

Example

AM2301 Temperature and Humidity sensor

AM2301 Temperature and Humidity sensor



Project available on Github, download library below.

Icon
TM STM32F4 AM2301 Library

tilz0R

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 */
        AM2301_PIN_OUT;
        /* Set pin low for ~800-1000 us */
        AM2301_PIN_LOW;
        Delay(1000);
        /* Set pin high to ~30 us */
        AM2301_PIN_HIGH;
        Delay(30);
        /* Read mode */
        AM2301_PIN_IN;

        How did this impulse(250us)?

  • Lucas

    Hello Tilen,

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

    Thanks!

    • Hi,

      this lib supports only one sensor right now.

      • Lucas

        Hi Tilen,

        Is working. I duplicated the library.

        Cheers

        • 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.