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.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

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!