What's the purpose of this project?
In this section, we are going to launch the Dissolved Oxygen Module using an ARM microcontroller, STM32F series. To use more conveniently and optimally in this project, we use two ready modules GB627EN and GebraBit STM32F303. These two modules contain the minimum necessary elements of the Capacitive Soil Moisture 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 Dissolved Oxygen module, you will get to know how to set the various parts of the STM32 microcontroller to set up this sensor using the ADC, how to use the GB627EN 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?
s 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 them 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 Dissolved Oxygen module to the GebraBit STM32F303 module as follows:
Note: Set the VCC SEL jumper to 3v3 otherwise the module will not work properly .
Finally, we will see the value of the Dissolved Oxygen in Real Time in the “Watch1” window of the Keil compiler in the “Debug Session” mode.
Note: To have more accurate data calibrate the module as mentioned below.
STM32CubeMX settings
In the following, we review the settings related to each of the “ADC”, “RCC”, “Debug”, ”GPIO” and “Clock” sections in the STM32F303 microcontroller to develop the GebraBit Dissolved Oxygen 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:
ADC settings
To get the Dissolved Oxygen module output voltage with the GebraBit STM32F303 module enable the ADC in single-ended mode and select PA0 as ADC1_IN1:
GPIO settings
The Gebrabit STM32F303 module has built-in LED and pushbutton in PB6 and PA3 pins so we make the PB6 pin as GPIO_OUTPUT and PA3 pin as GPIO_INPUT to make the cube setting more functional and let you make your ideas into reality with these two modules.
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, first go to the Code Generator tab, and then in the Generated files section select the highlighted setting and active it
here we have used the “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 Dissolved Oxygen library and driver (provided by GebraBit).
You can download the “STM32Cube MX”, “library”, “driver” and KEIL project at the end of this tutorial.
Dissolved Oxygen library and driver
In addition to the modular design of various sensors and ICs, GebraBit tries to provide a 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” files (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-independent, the user can easily add the library in any of their favorite compilers and develop it by the desired microcontroller and development board.
GebraBit_Dissolved_Oxygen.h header file
In this file the casing of the module and the configurations related to each of the module’s internal blocks are defined in the form of a “STRUCT” with the name GebraBit_Dissolved_Oxygen Finally, in the Debug Session environment, all the configurations related to each block can be seen in real time.
Dissolved Oxygen struct
All sensor properties and sensor data are defined in this “struct” and all the information and configuration implemented on the sensor are stored in this “structure” and you can see the changes in each part of the sensor in the “Debug Session” environment.
typedef struct Dissolved_Oxygen
{
uint8_t CAL_MODE;
uint8_t TEMP;
uint8_t CAL1_TEMP;
uint8_t CAL2_TEMP;
uint32_t ADC_RAW_VALUE;
float CAL1_VOLTAGE;
float CAL2_VOLTAGE;
float Dissolved_Oxygen_VALUE;
float ADC_INPUT_VOLTAGE_VALUE;
ADC_HandleTypeDef ADC_HANDELER;
}GebraBit_Dissolved_Oxygen;
Declaration of functions
At the end of this file, all the functions for receiving data from the module and calibration are declared:
void GB_Dissolved_Oxygen_Configuration(GebraBit_Dissolved_Oxygen * Dissolved_Oxygen);
void GB_Dissolved_Oxygen_Single_Point_Calibration(GebraBit_Dissolved_Oxygen * Dissolved_Oxygen, float Cal1_Voltage, uint8_t Cal1_Temp);
void GB_Dissolved_Oxygen_Two_Point_Calibration(GebraBit_Dissolved_Oxygen * Dissolved_Oxygen, float Cal1_Voltage, uint8_t Cal1_Temp, float Cal2_Voltage, uint8_t Cal2_Temp);
void GB_Dissolved_Oxygen_Update_Slution_Temp(GebraBit_Dissolved_Oxygen * Dissolved_Oxygen, float Temp);
void GB_Dissolved_Oxygen_Calculate_Dissolved_Oxygen(GebraBit_Dissolved_Oxygen * Dissolved_Oxygen);
void GB_Dissolved_Oxygen_Get_Data(GebraBit_Dissolved_Oxygen * Dissolved_Oxygen);
GebraBit_Dissolved_Oxygen.c source file
In this file, which is written in C language, all the functions are commented on 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_Dissolved_Oxygen.c” library provided by GebraBit, we will examine the “main .c” file of the sample tutorial and view the output of the GebraBit_Dissolved_Oxygen module in the “watch” part in the Keil compiler “Debugging” environment.
Description of “main.c” file
functions required by the GebraBit Dissolved Oxygen module have been added to the structures. In the next part, a variable named DO_Module of the GebraBit_Dissolved_Oxygen structure type (this structure is in the GebraBit_Dissolved_Oxygen header and is explained in the GebraBit_Dissolved_Oxygen library description section) is defined for the configuration of the GebraBit Dissolved Oxygen module:
/* USER CODE BEGIN PTD */
GebraBit_Dissolved_Oxygen DO_Module;
/* USER CODE END PTD */
In the next part of the written code, using the GB_Dissolved_Oxygen_Configuration (&DO_Module) function and GB_Dissolved_Oxygen_Two_Point_Calibration (&DO_Module, Module Voltage Value in solution 1, Temp solution 1, Module Voltage Value in solution 2, Temp solution 2) , we set the GebraBit Dissolved Oxygen module and finally, in the while part of the program, the data is read from the sensor and Digital Status and ADC values are continuously received:
/* USER CODE BEGIN 2 */
GB_Dissolved_Oxygen_Configuration(&DO_Module);
//GB_Dissolved_Oxygen_Single_Point_Calibration(&DO_Module,1.6,25);
GB_Dissolved_Oxygen_Two_Point_Calibration(&DO_Module,1.6,25,1.3,15);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//GB_Dissolved_Oxygen_Update_Slution_Temp(&DO_Module,TEMP);
GB_Dissolved_Oxygen_Get_Data(&DO_Module);
HAL_Delay(500);
}
Note: If this is the first time you use the probe or the probe has been used for some time, the probe needs to be calibrated for accuracy.
• Single-point calibration: only calibrate the saturated dissolved oxygen at a fixed temperature, suitable for use when the temperature is stable
• Two-point calibration: calibrate the saturated dissolved oxygen at different temperatures, you can calculate the temperature compensation, used when the temperature changes
Single-point calibration:
Only calibrate the saturated dissolved oxygen at a fixed temperature, suitable for use when the temperature is stable. There are 2 ways to get saturated dissolved oxygen voltage:
1. Expose the wet probe to the air. A easy way.
2. immerse the probe in saturated dissolved oxygen water, troublesome but precise.
Expose the wet probe to the air
1. Prepare the probe
2. Wet the probe in pure water and shake off excess water drops
3. Expose the probe to the air and maintain proper air flow (do not use a fan to blow)
4. After the output voltage is stable, record the voltage, which is the saturated dissolved oxygen voltage at the current temperature
Immerse the probe in saturated dissolved oxygen water
1. Probe ready
2. Prepare a cup of purified water and use one of the following methods to make saturated oxygen water.
o A: Use a stirrer or an eggbeater to continuously stir for 10 minutes to saturate the dissolved oxygen
o B: Use an air pump to continuously inflate the water for 10 minutes to saturate the dissolved oxygen
3. Stop stirring or pumping, and put the probe after the bubbles disappear
4. After placing the probe, keep stirring slowly while avoiding any bubbles.
5. After the output voltage stable, record the temperature and voltage
Two-point calibration
1 .Prepare two cups of purified water, put one cup in the refrigerator, and one cup to warm up (Do not exceed 40°C, otherwise the probe may be damaged.)
2. Use one of the following methods to make saturated oxygen water.
o A: Use a stirrer or an eggbeater to continuously stir for 10 minutes to saturate the dissolved oxygen.
o B: Use an air pump to continuously inflate the water for 10 minutes to saturate the dissolved oxygen.
3. Stop stirring or pumping, and put the probe after the bubbles disappear.
4. After placing the probe, keep stirring slowly while avoiding any bubbles.
5. After the output voltage stable, record the temperature and voltage.
6. Perform the same operation on another glass of water. Measure and record the temperature and voltage.
Caution: the module is not waterproof and avoid getting the electrical components wet.
The “main.c” file code text:
/* USER CODE BEGIN Header */
/*
* ________________________________________________________________________________________________________
* Copyright (c) 2020 GebraBit Inc. All rights reserved.
*
* This software, related documentation and any modifications thereto (collectively ?Software?) is subject
* to GebraBit and its licensors' intellectual property rights under U.S. and international copyright
* and other intellectual property rights laws.
*
* GebraBit and its licensors retain all intellectual property and proprietary rights in and to the Software
* and any use, reproduction, disclosure or distribution of the Software without an express license agreement
* from GebraBit is strictly prohibited.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT IN
* NO EVENT SHALL GebraBit BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THE SOFTWARE.
* ________________________________________________________________________________________________________
*/
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
* @Author : Sepehr Azimi
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "GebraBit_Dissolved_Oxygen.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_Dissolved_Oxygen DO_Module;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
GB_Dissolved_Oxygen_Configuration(&DO_Module);
//GB_Dissolved_Oxygen_Single_Point_Calibration(&DO_Module,1.6,25);
GB_Dissolved_Oxygen_Two_Point_Calibration(&DO_Module,1.6,25,1.3,15);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//GB_Dissolved_Oxygen_Update_Slution_Temp(&DO_Module,TEMP);
GB_Dissolved_Oxygen_Get_Data(&DO_Module);
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
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 Dissolved Oxygen modules, because they receive their supply voltage directly from the STLINK V2 programmer.
Finally, enter the “Debug” mode and by adding the “DO_Module” to the “watch” window and running the program, we can see the changes in the Dissolved Oxygen:
In the following, you can download the “GebraBit Dissolved Oxygen module setup project” using the GebraBit STM32F303 module in the Keil environment, the “STM32CubeMX file”, the schematic of the modules, and the “Dissolved Oxygen datasheet”.
Program output video
The video of the module operation will be uploaded soon
DOCUMENTS
- Dissolved Oxygen Datasheet
- Dissolved Oxygen Schematic
- STM32F303 Datasheet
- STM32F303 Schematic
- Keil Project
- STM32CUBEMX