Projects, sensors

SHT35 sensor project with STM32F303 microcontroller series

SHT35 gebrabit project

What’s the purpose of this project?

In this section, we are going to launch the SHT35 sensor using ARM microcontroller, STM32F series. In order to use more conveniently and optimally in this project, we use two ready modules GB624EN and GebraBit STM32F303. These two modules contain the minimum necessary elements of the SHT35 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 SHT35 sensor, you will get to know all the SHT35 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 GB624EN 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

GebraBit SHT35 module
Keil compiler

GebraBit STM32F303 module
 STM32CubeMX program
 ST-LINK/V2 programmer

First as shown in the image below, we connect the GebraBit SHT35 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 SHT35 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 SHT35 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 SHT35 library and driver (provided by GebraBit).

You can download the “STM32Cube MX”, “library”, “driver” and KEIL project at the end of this tutorial.

SHT35 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_SHT35.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 SHT35 sensor and the configurations related to each of the SHT35 sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_SHT35. 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:


					
				

SHT35_Ability Enum

The ability to activate or deactivate different parts of the sensor is defined in this enum:


					
				

SHT35_Single_Shot_Mode Enum

The values of this enum are used to select different sensor measurement modes in Single Shot mode:


					
				

SHT35_Periodic_Data_Acquisition_Mode Enum

The values of this enum are used to select different sensor data acquisition modes in periodic mode:


					
				

SHT35_ Command_Status Enum

To know the status of commands sent to the sensor, the values of this enum are used:


					
				

SHT35_ Checksum_Status Enum

This enum is used to know the checksum status:


					
				

SHT35_Measurement_Time Enum

This enum is used to choose when to convert data values:


					
				

SHT35_ Heater Enum

Using this enum, the sensor internal heater is turned off or on:


					
				

SHT35_Alert_Pending_Status Enum

Using this enum, the status of sensor notifications and warnings is checked:


					
				

SHT35_CRC_Status Enum

Using this enum, the status of the CRC check is specified:


					
				

SHT35_Alert Enum

For sensor temperature and humidity alerts, this enum is used:


					
				

SHT35_Reset_Status Enum

By using this enum, the sensor reset status is specified:


					
				

SHT35 struct

All sensor properties, calibration coefficients 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.


					
				

Declaration of functions

At the end of this file, all the functions for reading and writing in SHT35 registers, sensor configuration and receiving data from the sensor are declared:


					
				

GebraBit_SHT35.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_SHT35.c” library provided by GebraBit, we will examine the “main .c” file of the sample tutorial and view the output of the GebraBit_SHT35 module in the “watch” part in the Keil compiler “Debugging” environment.

Description of “main.c” file

Enums and functions required by GebraBit SHT35 module have been added to the structures. In the next part, a variable named SHT35_Module of the GebraBit_SHT35 structure type (this structure is in the GebraBit_SHT35 header and is explained in the GebraBit_SHT35 library description section) is defined for the configuration of the GebraBit SHT35 module:


					
				

In the next part of the written code, using the GB_SHT35_initialize (&SHT35_Module) and GB_SHT35_Configuration (&SHT35_Module) functions, we set the GebraBit SHT35 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:


					
				

The “main.c” file code text:


					
				

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 SHT35 modules, because they receive their supply voltage directly from the STLINK V2 programmer.

Finally, enter the “Debug” mode and by adding the “SHT35_Module” to the “watch” window and running the program, we can see the changes in the humidity and temperature of the GebraBit SHT35 module:

In the following, you can download the “GebraBit SHT35 module setup project” using the GebraBit STM32F303 module in the Keil environment, the “STM32CubeMX file”, the schematic of the modules and the “SHT35 datasheet”.

Program output video

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *