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
Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.
All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.
If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.
Need help or have questions? Leave a comment below — the author is always happy to assist!
Table of Contents
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:
- SHT31 temperature and humidity sensor
- STM32 microcontroller
- Breadboard
- Jumper wires
- 10kΩ pull-up resistors (2 nos.)
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:
- How to interface DHT11 with STM32
- How to interface the EM18 RFID module with STM32
- How to interface EEPROM with STM32
- Learn how to use the Real-time Clock (RTC) of STM32
- I2C communication with STM32
Liked this article? Subscribe to our newsletter:
or,
Visit LabProjectsBD.com for more inspiring projects and tutorials.
Thank you!
0 Comments