What's the purpose of this project?
In this section, we are going to launch the HTU31D sensor using ARM microcontroller, STM32F series. In order to use more conveniently and optimally in this project, we use two ready modules GB621EN and GebraBit STM32F303. These two modules contain the minimum necessary elements of the HTU31D sensor and the STM32F microcontroller, which are provided by the GebraBit team to facilitate the work.
What are we going to learn in this tutorial?
In this tutorial, in addition to setting up and using the HTU31D sensor, you will get to know all the HTU31D sensor registers, how to set the various parts of the STM32 microcontroller to set up this sensor using the I2C protocol, how to use the GB621EN module specific library and driver file. You will also learn how to declare functions and finally receive sensor data in the Keil compiler.
What do we need to start this project?
As you probably know, we need some hardware and software to do this project. The titles of these hardware and software are provided to you in the table below and you can prepare/download by clicking on each of them and get ready to start.
Required hardware
|
Required software
|
---|---|
Keil compiler
|
|
STM32CubeMX program
|
|
ST-LINK/V2 programmer
|
First as shown in the image below, we connect the GebraBit HTU31D module to the GebraBit STM32F303 module as follows:
Note: Considering that the PA14 pin of the GebraBit STM32F303 microcontroller module is used to program the microcontroller, the I2C setting on the PA14 and PA15 pins is not possible in this version, so the GebraBit HTU31D module cannot be placed as a pin to pin on the GebraBit STM32F303 microcontroller module.
Finally, we will see the values of temperature and humidity in Real Time in the “Watch1” window of the Keil compiler in the “Debug Session” mode.
STM32CubeMX settings
In the following, we review the settings related to each of the “I2C”, “RCC”, “Debug”, and “Clock” sections in the STM32F303 microcontroller to develop the GebraBit HTU31D module.
RCC settings
Due to the presence of “8Mhz” crystal in the GebraBit STM32F303 module, we select the “external clock” in the “RCC” section:
Debug & Programming settings
Regarding the access to “SWCLK” and “SWDIO” pins in the GebraBit STM32F303 module, to reduce the number of pins during “Debug & Programming”, in the “SYS” block, we select the “Serial Wire” option in the “Debug” section:
I2C settings
To communicate with the GebraBit STM32F303 module through I2C, select the Fast Mode with a speed of 400khz and select PB8 and PB9 pins as SCL and SDA:
According to the sensor data sheet, the settings of the I2C parameters in the “Parameter Settings” section will be set as shown in the above image.
Clock settings
The “clock” settings for each part of the STM32F303 microcontroller in this code, are as follows:
Project Manager settings
“Project Manager” settings are as follows, here we have used “MDK-ARM” version “5.32” compiler:
After completing all the above settings, we can develop our code easily just by one click on “GENERATE CODE” and adding the HTU31D library and driver (provided by GebraBit).
You can download the “STM32Cube MX”, “library”, “driver” and KEIL project at the end of this tutorial.
HTU31D library and driver
In addition to the modular design of various sensors and ICs, GebraBit tries to provide variety of structured and hardware-independent libraries in C language for the ease of users in setting up and developing software.
For this purpose, after preparing each GebraBit module, the users can refer to the “tutorial” section of the desired module and download the dedicated library, which contains the “ .h” and “ .c” file (Header and Source) and a sample training program under “GebraBit STM32F303”, “GebraBit ATMEGA32A” or “Arduino” development boards.
All the defined functions and structures in the library are commented in full detail and all the received parameters in the arguments of the functions and their return values, are briefly explained. Since the libraries are hardware independed, the user can easily add the library in any of their favorite compilers and develop it by desired microcontroller and development board.
GebraBit_HTU31D.h header file
In this file, based on the datasheet of the sensor or IC, all address registers, the values of each register are defined in the form of “Enumeration”. Also, the casing of the HTU31D sensor and the configurations related to each of the HTU31D sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_ HTU31D. Finally, in the Debug Session environment, all the configurations related to each block can be seen in real time.
USER REGISTER MAP
The registry map or sensor commands are defined in this section:
1. #define HTU31D_I2C &hi2c1
2. #define HTU31D_ADDRESS 0x40
3. #define HTU31D_WRITE_ADDRESS ((HTU31D_ADDRESS<<1)|0)
4. #define HTU31D_READ_ADDRESS ((HTU31D_ADDRESS<<1)|1)
5. #define HTU31D_CONVERSION (0x40)
6. #define HTU31D_READ_TEMPERATURE_HUMIDITY (0x00)
7. #define HTU31D_READ_HUMIDITY (0x10)
8. #define HTU31D_RESET (0x1E)
9. #define HTU31D_HEATER_ON (0x04)
10. #define HTU31D_HEATER_OFF (0x02)
11. #define HTU31D_READ_SERIAL_NUMBER (0x0A)
12. #define HTU31D_READ_DIAGNOSTIC (0x08)
13. // Processing constants
14. #define HTU31D_TEMPERATURE_COEFFICIENT (float)(-0.15)
15. #define HTU31D_CONSTANT_A (float)(8.1332)
16. #define HTU31D_CONSTANT_B (float)(1762.39)
17. #define HTU31D_CONSTANT_C (float)(235.66)
18. // Coefficients for temperature computation
19. #define TEMPERATURE_COEFF_MUL (175.72)
20. #define TEMPERATURE_COEFF_ADD (-46.85)
21. // Coefficients for relative humidity computation
22. #define HUMIDITY_COEFF_MUL (125)
23. #define HUMIDITY_COEFF_ADD (-6)
24.
HTU31D_Ability Enum
The ability to activate or deactivate different parts of the sensor is defined in this enum:
typedef enum Ability
{
Disable = 0 ,
Enable
}HTU31D_Ability;
HTU31D_Action Enum
The values of this enum are used to activate and deactivate different parts of the sensor:
1. typedef enum Action
2. {
3. DEACTIVE = 0,
4. ACTIVE = 1
5. }HTU31D_Action;
HTU31D_ Humidity_OSR Enum
The values of this enum are used for the humidity sensor OSR settings:
1. typedef enum Humidity_OSR
2. {
3. HUMIDITY_OSR_0 = 0x00 ,
4. HUMIDITY_OSR_1 = 0x08 ,
5. HUMIDITY_OSR_2 = 0x10 ,
6. HUMIDITY_OSR_3 = 0x18
7. }HTU31D_Humidity_OSR;
8.
HTU31D_ Temperature_OSR Enum
The values of this enum are used for the temperature sensor OSR settings:
1. typedef enum Temperature_OSR
2. {
3. TEMPERATURE_OSR_0 = 0x00 ,
4. TEMPERATURE_OSR_1 = 0x02 ,
5. TEMPERATURE_OSR_2 = 0x04 ,
6. TEMPERATURE_OSR_3 = 0x06
7. }HTU31D_Temperature_OSR;
8.
HTU31D_Humidity_Conversion_Time Enum
The values of this enum are used to select the humidity values conversion time:
1. typedef enum Humidity_Conversion_Time
2. {
3. HUMIDITY_OSR_0_MEASUREMENT_TIME = 1 ,
4. HUMIDITY_OSR_1_MEASUREMENT_TIME = 2 ,
5. HUMIDITY_OSR_2_MEASUREMENT_TIME = 4 ,
6. HUMIDITY_OSR_3_MEASUREMENT_TIME = 8
7. }HTU31D_Humidity_Conversion_Time;
8.
HTU31D_Temperature_Conversion_Time Enum
The values of this enum are used to select the temperature values conversion time:
1. typedef enum Temperature_Conversion_Time
2. {
3. TEMPERATURE_OSR_0_MEASUREMENT_TIME = 2 ,
4. TEMPERATURE_OSR_1_MEASUREMENT_TIME = 4 ,
5. TEMPERATURE_OSR_2_MEASUREMENT_TIME = 7 ,
6. TEMPERATURE_OSR_3_MEASUREMENT_TIME = 13
7. }HTU31D_Temperature_Conversion_Time;
8.
HTU31D_ Heater Enum
Using this enum, the sensor internal heater is turned off or on:
1. typedef enum Heater
2. {
3. HEATER_ON = HTU31D_HEATER_ON ,
4. HEATER_OFF = HTU31D_HEATER_OFF
5. }HTU31D_Heater;
6.
HTU31D_CRC_Status Enum
Using this enum, the status of the CRC check is specified:
1. typedef enum CRC_Status
2. {
3. CRC_ERROR = 0 ,
4. CRC_OK
5. }HTU31D_CRC_Status;
6.
HTU31D_Reset_Status Enum
By using this enum, the reset status of the sensor is specified:
1. typedef enum
2. {
3. FAILED = 0 ,
4. DONE
5. }HTU31D_Reset_Status;
6.
HTU31D struct
1. /*************************************************
2. * Defining HTU31D Register & Data As Struct *
3. **************************************************/
4. typedef struct HTU31D
5. {
6. uint8_t Register_Cache;
7. HTU31D_Reset_Status RESET;
8. uint32_t SERIAL_NUMBER;
9. uint8_t DIAGNOSTIC;
10. HTU31D_Action NVM_CRC_ERROR;
11. HTU31D_Action HUMIDITY_UNDER_OVER_RUN;
12. HTU31D_Action HUMIDITY_ABOVE_120RH_ERROR;
13. HTU31D_Action HUMIDITY_BELOW_10RH_ERROR ;//-10
14. HTU31D_Action TEMPERATURE_UNDER_OVER_RUN;
15. HTU31D_Action TEMPERATURE_ABOVE_150_ERROR;
16. HTU31D_Action TEMPERATURE_BELOW_50_ERROR ;//-50
17. HTU31D_Heater ON_CHIP_HEATER;
18. HTU31D_Humidity_OSR HUMIDITY_OSR;
19. HTU31D_Temperature_OSR TEMPERATURE_OSR;
20. HTU31D_Humidity_Conversion_Time HUMIDITY_CONVERSION_TIME;
21. HTU31D_Temperature_Conversion_Time TEMPERATURE_CONVERSION_TIME;
22. uint8_t ADC_RAW_DATA[ADC_RAW_DATA_BUFFER_SIZE];
23. uint16_t RAW_TEMPERATURE;
24. uint16_t RAW_HUMIDITY;
25. uint8_t HTU31D_CRC;
26. HTU31D_CRC_Status CRC_CHECK;
27. float TEMPERATURE;
28. float HUMIDITY;
29. // double PARTIAL_PRESSURE;
30. // double DEW_POINT;
31. }GebraBit_HTU31D;
32.
Declaration of functions
At the end of this file, all the functions for reading and writing in HTU31D registers, sensor configuration and receiving data from the sensor are declared:
1. /********************************************************
2. * Declare Read&Write HTU31D Register Values Functions *
3. ********************************************************/
4. extern void GB_HTU31D_Write_Command( uint8_t cmd);
5. /********************************************************
6. * Declare HTU31D Configuration Functions *
7. ********************************************************/
8. extern void GB_HTU31D_Soft_Reset ( GebraBit_HTU31D * HTU31D ) ;
9. extern void GB_HTU31D_CRC_Check( GebraBit_HTU31D * HTU31D , uint16_t value, uint8_t crc) ;
10. extern void GB_HTU31D_On_Chip_Heater ( GebraBit_HTU31D * HTU31D , HTU31D_Heater heater ) ;
11. extern void GB_HTU31D_Read_Serial_Number ( GebraBit_HTU31D * HTU31D ) ;
12. extern void GB_HTU31D_Read_Diagnostic ( GebraBit_HTU31D * HTU31D ) ;
13. extern void GB_HTU31D_Humidity_OSR ( GebraBit_HTU31D * HTU31D , HTU31D_Humidity_OSR osr ) ;
14. extern void GB_HTU31D_Temperature_OSR ( GebraBit_HTU31D * HTU31D , HTU31D_Temperature_OSR osr ) ;
15. extern void GB_HTU31D_Configuration(GebraBit_HTU31D * HTU31D) ;
16. extern void GB_HTU31D_Start_Conversion ( GebraBit_HTU31D * HTU31D ) ;
17. extern void GB_HTU31D_Read_Raw_Temperature_Humidity( GebraBit_HTU31D * HTU31D ) ;
18. extern void GB_HTU31D_Temperature ( GebraBit_HTU31D * HTU31D ) ;
19. extern void GB_HTU31D_Humidity ( GebraBit_HTU31D * HTU31D ) ;
20. extern void GB_HTU31D_Dew_Point( GebraBit_HTU31D * HTU31D ) ;
21. extern void GB_HTU31D_initialize( GebraBit_HTU31D * HTU31D ) ;
22. extern void GB_HTU31D_Get_Data(GebraBit_HTU31D * HTU31D);
23.
GebraBit_HTU31D.c source file
In this file, which is written in C language, all the functions are commented in full detail, and all the parameters received in the arguments of the functions and their return values are clearly explained so we confine to these explanations and invite users to check this file directly for more information.
Sample program in Keil
After making the Keil project by STM32CubeMX and adding the “GebraBit_ HTU31D.c” library provided by GebraBit, we will examine the “main .c” file of the sample tutorial and view the output of the GebraBit_HTU31D module in the “watch” part in the Keil compiler “Debugging” environment.
Description of “main.c” file
Enums and functions required by GebraBit HTU31D module have been added to the structures. In the next part, a variable named HTU31D_Module of the GebraBit_HTU31D structure type (this structure is in the GebraBit_ HTU31D header and is explained in the GebraBit_HTU31D library description section) is defined for the configuration of the GebraBit HTU31D module:
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_HTU31D HTU31D_Module;
/* USER CODE END PTD */
In the next part of the written code, using the GB_HTU31D_initialize (&HTU31D_Module) and GB_HTU31D_Configuration (&HTU31D_Module) functions, we set the GebraBit HTU31D module and finally, in the while part of the program, the data is read from the sensor and the humidity and temperature values are continuously received:
1. /* USER CODE BEGIN 2 */
2. GB_HTU31D_initialize(&HTU31D_Module);
3. GB_HTU31D_Configuration(&HTU31D_Module);
4. /* USER CODE END 2 */
5.
6. /* Infinite loop */
7. /* USER CODE BEGIN WHILE */
8. while (1)
9. {
10. /* USER CODE END WHILE */
11.
12. /* USER CODE BEGIN 3 */
13. GB_HTU31D_Get_Data(&HTU31D_Module);
14. }
15. /* USER CODE END 3 */
16. }
17.
1. /* USER CODE BEGIN 2 */
2. GB_HTU31D_initialize(&HTU31D_Module);
3. GB_HTU31D_Configuration(&HTU31D_Module);
4. /* USER CODE END 2 */
5.
6. /* Infinite loop */
7. /* USER CODE BEGIN WHILE */
8. while (1)
9. {
10. /* USER CODE END WHILE */
11.
12. /* USER CODE BEGIN 3 */
13. GB_HTU31D_Get_Data(&HTU31D_Module);
14. }
15. /* USER CODE END 3 */
16. }
17.
The “main.c” file code text:
1. /* USER CODE BEGIN Header */
2. /*
3. * ________________________________________________________________________________________________________
4. * Copyright (c) 2020 GebraBit Inc. All rights reserved.
5. *
6. * This software, related documentation and any modifications thereto (collectively “Software”) is subject
7. * to GebraBit and its licensors' intellectual property rights under U.S. and international copyright
8. * and other intellectual property rights laws.
9. *
10. * GebraBit and its licensors retain all intellectual property and proprietary rights in and to the Software
11. * and any use, reproduction, disclosure or distribution of the Software without an express license agreement
12. * from GebraBit is strictly prohibited.
13.
14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15. * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT IN
16. * NO EVENT SHALL GebraBit BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
17. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
18. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
19. * OF THE SOFTWARE.
20. * ________________________________________________________________________________________________________
21. */
22. /**
23. ******************************************************************************
24. * @file : main.c
25. * @brief : Main program body
26. * @Author : Mehrdad Zeinali
27. ******************************************************************************
28. * @attention
29. *
30. * Copyright (c) 2022 STMicroelectronics.
31. * All rights reserved.
32. *
33. * This software is licensed under terms that can be found in the LICENSE file
34. * in the root directory of this software component.
35. * If no LICENSE file comes with this software, it is provided AS-IS.
36. *
37. ******************************************************************************
38. */
39. /* USER CODE END Header */
40. /* Includes ------------------------------------------------------------------*/
41. #include "main.h"
42. #include "i2c.h"
43. #include "gpio.h"
44.
45. /* Private includes ----------------------------------------------------------*/
46. /* USER CODE BEGIN Includes */
47. #include "GebraBit_HTU31D.h"
48. /* USER CODE END Includes */
49.
50. /* Private typedef -----------------------------------------------------------*/
51. /* USER CODE BEGIN PTD */
52. GebraBit_HTU31D HTU31D_Module;
53. /* USER CODE END PTD */
54.
55. /* Private define ------------------------------------------------------------*/
56. /* USER CODE BEGIN PD */
57. /* USER CODE END PD */
58.
59. /* Private macro -------------------------------------------------------------*/
60. /* USER CODE BEGIN PM */
61.
62. /* USER CODE END PM */
63.
64. /* Private variables ---------------------------------------------------------*/
65.
66. /* USER CODE BEGIN PV */
67.
68. /* USER CODE END PV */
69.
70. /* Private function prototypes -----------------------------------------------*/
71. void SystemClock_Config(void);
72. /* USER CODE BEGIN PFP */
73.
74. /* USER CODE END PFP */
75.
76. /* Private user code ---------------------------------------------------------*/
77. /* USER CODE BEGIN 0 */
78.
79. /* USER CODE END 0 */
80.
81. /**
82. * @brief The application entry point.
83. * @retval int
84. */
85. int main(void)
86. {
87. /* USER CODE BEGIN 1 */
88.
89. /* USER CODE END 1 */
90.
91. /* MCU Configuration--------------------------------------------------------*/
92.
93. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
94. HAL_Init();
95.
96. /* USER CODE BEGIN Init */
97.
98. /* USER CODE END Init */
99.
100. /* Configure the system clock */
101. SystemClock_Config();
102.
103. /* USER CODE BEGIN SysInit */
104.
105. /* USER CODE END SysInit */
106.
107. /* Initialize all configured peripherals */
108. MX_GPIO_Init();
109. MX_I2C1_Init();
110. /* USER CODE BEGIN 2 */
111. GB_HTU31D_initialize(&HTU31D_Module);
112. GB_HTU31D_Configuration(&HTU31D_Module);
113. /* USER CODE END 2 */
114.
115. /* Infinite loop */
116. /* USER CODE BEGIN WHILE */
117. while (1)
118. {
119. /* USER CODE END WHILE */
120.
121. /* USER CODE BEGIN 3 */
122. GB_HTU31D_Get_Data(&HTU31D_Module);
123. }
124. /* USER CODE END 3 */
125. }
126.
127. /**
128. * @brief System Clock Configuration
129. * @retval None
130. */
131. void SystemClock_Config(void)
132. {
133. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
134. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
135. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
136.
137. /** Initializes the RCC Oscillators according to the specified parameters
138. * in the RCC_OscInitTypeDef structure.
139. */
140. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
141. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
142. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
143. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
144. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
145. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
146. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
147. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
148. {
149. Error_Handler();
150. }
151.
152. /** Initializes the CPU, AHB and APB buses clocks
153. */
154. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
155. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
156. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
157. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
158. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
159. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
160.
161. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
162. {
163. Error_Handler();
164. }
165. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
166. PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_SYSCLK;
167. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
168. {
169. Error_Handler();
170. }
171. }
172.
173. /* USER CODE BEGIN 4 */
174.
175. /* USER CODE END 4 */
176.
177. /**
178. * @brief This function is executed in case of error occurrence.
179. * @retval None
180. */
181. void Error_Handler(void)
182. {
183. /* USER CODE BEGIN Error_Handler_Debug */
184. /* User can add his own implementation to report the HAL error return state */
185. __disable_irq();
186. while (1)
187. {
188. }
189. /* USER CODE END Error_Handler_Debug */
190. }
191.
192. #ifdef USE_FULL_ASSERT
193. /**
194. * @brief Reports the name of the source file and the source line number
195. * where the assert_param error has occurred.
196. * @param file: pointer to the source file name
197. * @param line: assert_param error line source number
198. * @retval None
199. */
200. void assert_failed(uint8_t *file, uint32_t line)
201. {
202. /* USER CODE BEGIN 6 */
203. /* User can add his own implementation to report the file name and line number,
204. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
205. /* USER CODE END 6 */
206. }
207. #endif /* USE_FULL_ASSERT */
208.
Program output
After generating the Keil project using STM32CubeMX and adding the library, we connect the STLINK V2 programmer to the GebraBit STM32F303 using the STLINKV2 adapter:
STLINKV2 adapter:
By connecting the STLINK V2 programmer to the GebraBit STM32F303, there is no need to apply power to the GebraBit STM32F303 and GebraBit HTU31D modules, because they receive their supply voltage directly from the STLINK V2 programmer.
Finally, enter the “Debug” mode and by adding the “HTU31D_Module” to the “watch” window and running the program, we can see the changes in the humidity and temperature of the GebraBit HTU31D module:
In the following, you can download the “GebraBit HTU31D module setup project” using the GebraBit STM32F303 module in the Keil environment, the “STM32CubeMX file”, the schematic of the modules and the “HTU31D datasheet”.