Learn how to interface the SHT31 sensor with the STM32 microcontroller in this informative article. Discover the necessary code to integrate the SHT31 sensor and STM32, and create innovative solutions for a variety of applications using the accurate and reliable data provided by the SHT31 sensor.

Disclaimer: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act of copying and claiming it as yours. Most of the articles published here are kept open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guidance feel free to comment below, the author will try to help you. Also, there can be affiliation links in the article. Which will not affect you anyway, but allows the author with some commission. So please don’t take it otherwise. Thanks.

Table of Contents

Advertisements

About SHT31 sensor:

There are various humidity & temperature sensors like DHT11 & HTU21D. But while talking about the accuracy none of them are suitable for industrial-level temperature humidity monitoring due to the accuracy and precision. They have poor accuracy as well as sensitivity. So here we will use the SHT31 Temperature/Humidity sensor. They are the finest & highest-accuracy devices you can get. It is a digital sensor with an I2C interface which makes for easy reading of humidity and temperature. The SHT31 sensor has an excellent ±2% relative humidity and ±0.3°C temperature accuracy for most uses.

Datasheet of: SHT31 Temperature/Humidity sensor.

How to interface with STM32:

The SHT31 is a high-precision temperature and humidity sensor that utilizes an I2C communication protocol for interfacing with a microcontroller. The STM32 microcontroller family, on the other hand, is a widely used 32-bit ARM-based microcontroller platform that comes with built-in I2C interface support. In this article, we will discuss how to interface SHT31 with the STM32 microcontroller.

Hardware Requirements:

Step 1:

Wiring the SHT31 Sensor to STM32 The SHT31 sensor can be easily interfaced with the STM32 microcontroller by connecting the SDA and SCL pins of the sensor to the corresponding pins of the STM32 microcontroller. Connect a 10kΩ pull-up resistor between the SDA and VCC pin and another 10kΩ pull-up resistor between the SCL and VCC pin. Finally, connect the VCC pin of the sensor to the 3.3V pin of the STM32 microcontroller and the GND pin to the ground.

Step 2:

Setting up the I2C Peripheral Before we can communicate with the SHT31 sensor, we need to configure the I2C peripheral of the STM32 microcontroller. This can be done using the STM32CubeIDE software. Open a new project in the STM32CubeIDE software and select the appropriate STM32 microcontroller.

Step 3:

Configuring the I2C Communication Next, we need to configure the I2C communication parameters. In the STM32CubeIDE software, open the Project Manager view and select “Pinout & Configuration” from the drop-down menu. In the “Pinout & Configuration” view, click on the “I2C1” peripheral and select the appropriate GPIO pins for SDA and SCL.

Next, we need to configure the I2C communication parameters. In the “Configuration” tab of the I2C1 peripheral settings, set the I2C clock speed to 100 kHz. Also, enable the “General Call” and “Software Reset” options.

Example code:

Here is an example code interfacing SHT31 with STM32.

#include "stm32f4xx.h"
#include "stm32f4xx_i2c.h"

#define SHT31_ADDR 0x44 << 1 // SHT31 I2C address shifted left by 1 bit
#define CMD_MEASURE_TEMP 0x2C06 // Command to measure temperature
#define CMD_MEASURE_HUMIDITY 0x2C10 // Command to measure humidity

I2C_HandleTypeDef hi2c1;

void I2C_Init(void)
{
    hi2c1.Instance = I2C1;
    hi2c1.Init.ClockSpeed = 100000;
    hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c1.Init.OwnAddress1 = 0;
    hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c1.Init.OwnAddress2 = 0;
    hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    HAL_I2C_Init(&hi2c1);
}

void SHT31_ReadTempHumidity(float* temp, float* humidity)
{
    uint8_t data[6];
    uint16_t temp_raw, humidity_raw;

    // Send command to measure temperature
    HAL_I2C_Master_Transmit(&hi2c1, SHT31_ADDR, &CMD_MEASURE_TEMP, 2, 1000);
    HAL_Delay(50);

    // Read temperature data
    HAL_I2C_Master_Receive(&hi2c1, SHT31_ADDR, data, 3, 1000);
    temp_raw = data[0] << 8 | data[1];
    *temp = ((float)temp_raw * 175.0f / 65535.0f) - 45.0f;

    // Send command to measure humidity
    HAL_I2C_Master_Transmit(&hi2c1, SHT31_ADDR, &CMD_MEASURE_HUMIDITY, 2, 1000);
    HAL_Delay(50);

    // Read humidity data
    HAL_I2C_Master_Receive(&hi2c1, SHT31_ADDR, data, 3, 1000);
    humidity_raw = data[0] << 8 | data[1];
    *humidity = ((float)humidity_raw * 100.0f / 65535.0f);
}

int main(void)
{
    float temperature, humidity;

    // Initialize the I2C peripheral
    I2C_Init();

    while (1)
    {
        // Read temperature and humidity data from SHT31
        SHT31_ReadTempHumidity(&temperature, &humidity);

        // Print the temperature and humidity data
        printf("Temperature: %.2f°C\n", temperature);
        printf("Humidity: %.2f%%\n", humidity);

        // Delay for some time
        HAL_Delay(1000);
    }
}

Note:

In this example code, we first initialize the I2C peripheral of the STM32 microcontroller with a clock speed of 100 kHz. We then define the SHT31 I2C address and the commands to measure temperature and humidity.

Code Points:

The SHT31_ReadTempHumidity() the function sends the commands to measure temperature and humidity to the SHT31 sensor, waits for a brief period, and then reads the

temperature and humidity data from the sensor. The raw data is then converted to Celsius degrees and percentage relative humidity, respectively.

Finally, in the main loop of the program, we call the SHT31_ReadTempHumidity() function to read temperature and humidity data from the SHT31 sensor and print the data to the console. We also add a delay of 1 second between each measurement.

Step 4:

Reading Data from the SHT31 Sensor With the I2C peripheral and communication parameters configured, we can now read temperature and humidity data from the SHT31 sensor. We can do this by sending commands to the sensor over the I2C bus.

To read temperature data, send the command 0x2C06 to the sensor over the I2C bus. This command instructs the sensor to measure temperature and return the data. To read humidity data, send the command 0x2C10 to the sensor over the I2C bus.

Once the commands are sent, the sensor will return the data in two bytes. We need to combine these two bytes to get the actual temperature or humidity value. The formula for converting the raw data to actual temperature or humidity value is given in the SHT31 datasheet.

Step 5:

Testing the Interface We can test the interface by writing a simple program that reads temperature and humidity data from the SHT31 sensor and displays the data on a serial terminal. In the main loop of the program, we can first send the command to measure temperature and humidity, wait for a brief period, and then read the data from the sensor. Finally, we can convert the raw data to actual temperature and humidity values and display them on the serial terminal.

Conclusion:

Note that in order to use the printf() function to print data to the console, you will need to configure the UART peripheral of the STM32 microcontroller and include the appropriate headers in the code.

Overall, this example code provides a basic framework for interfacing the SHT31 sensor with the STM32 microcontroller. You can modify the code to suit your specific project requirements, such as adding error handling or implementing a different communication protocol.

If everything is ok, you’ll get the reading. Now you can integrate this into your other code.

Read more on:

For Professional Designs or Help:

Loading

Also let me know what you want to get as the next article, comment below!


MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

%d bloggers like this: