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