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
- TM DELAY
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:
1 2 3 |
/* Use custom data pin */ #define AM2301_PORT GPIOD #define AM2301_PIN GPIO_PIN_1 |
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
1 2 |
/* Initialize sensor */ TM_AM2301_Init(); |
You are now able to read data. First make a new instance of data typedef.
1 |
TM_AM2301_Data_t data; |
Then check if data is valid with
1 2 3 4 |
/* Data valid */ if (TM_AM2301_Read(&data) == TM_AM2301_TM_AM2301_OK) { /* Data is valid */ } |
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/** * Enumerations * * There are several different possible results. * If TM_AM2301_OK is returned from read function then you have valid data. */ typedef enum { TM_AM2301_OK, /* Data OK */ TM_AM2301_ERROR, /* An error */ TM_AM2301_CONNECTION_ERROR, /* Device is not connected */ TM_AM2301_WAITHIGH_ERROR, TM_AM2301_WAITLOW_ERROR, TM_AM2301_WAITHIGH_LOOP_ERROR, TM_AM2301_WAITLOW_LOOP_ERROR, TM_AM2301_PARITY_ERROR /* Data read fail */ } TM_AM2301_t; /** * Data structure * * Parameters: * - int16_t Temp: * Temperature in tenths of degrees. * If real temperature is 27.3°C, this variable's value is 273 * - uint16_t Hum: * Humidity in tenths of percent * If real humidity is 55.5%, this variable's value is 555 */ typedef struct { int16_t Temp; uint16_t Hum; } TM_AM2301_Data_t; /** * Initialize AM2301 sensor * * TM_AM2301_OK is returned */ extern TM_AM2301_t TM_AM2301_Init(void); /** * Read data from sensor * * Parameters: * - TM_AM2301_Data_t* data: * Pointer to data structure * * TM_AM2301_OK returned in case data is valid, otherwise anything from TM_AM2301_t typedef */ extern TM_AM2301_t TM_AM2301_Read(TM_AM2301_Data_t* data); |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/** * Keil project for AM2301 sensor * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_am2301.h" #include "tm_stm32f4_ili9341.h" #include <stdio.h> int main(void) { TM_AM2301_Data_t data; char str[50]; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize LCD on F429-Discovery board */ TM_ILI9341_Init(); TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_2); TM_ILI9341_Fill(ILI9341_COLOR_ORANGE); TM_ILI9341_Puts(10, 10, "AM2301 (DHT21)\nsensor", &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); TM_ILI9341_Puts(90, 310, "stm32f4-discovery.net", &TM_Font_7x10, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); /* Initialize sensor */ TM_AM2301_Init(); /* Reset time */ TM_DELAY_SetTime(0); while (1) { /* Every 1 second */ if (TM_DELAY_Time() > 1000) { /* Reset time */ TM_DELAY_SetTime(0); /* Data valid */ if (TM_AM2301_Read(&data) == TM_AM2301_OK) { /* Show on LCD */ sprintf(str, "Humidity: %2.1f %%\nTemperature: %2.1f C", (float)data.Hum / 10, (float)data.Temp / 10); /* Put to LCD */ TM_ILI9341_Puts(10, 100, str, &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); } } /* Do other stuff constantly */ } } |
Project available on Github, download library below.
Recent comments