Showing results for 
Search instead for 
Did you mean: 

STM32L031 I2C (SHT40) driver help


Hello. I have STM32L031 nucleo board:

And I am trying to write SHT40 driver for it. I have Adafruit SHT40 I2C temperature/humidity sensor:


I follow simple steps to setup my STM32 board for I2C:

  1. Enable I2C peripheral on STM32CubeMX:
  2. Connect the Adafruit board to the STM32L031 Nucleo board:


  3. Write function to read temperature/humidity data from the SHT40 sensor:







static void SHT40_measure(){
	HAL_StatusTypeDef ret;
	uint8_t data_tx[1] = {0xFD};
	uint8_t data_rx[6];
	ret = HAL_I2C_Master_Transmit(&hi2c1, 0x44, data_tx, 1, 1000);
	if ( ret != HAL_OK ) {
	  printf("Error Tx\r\n");
		//read bytes
		 ret =  HAL_I2C_Master_Receive(&hi2c1, 0x44, (uint8_t*)&data_rx, 6,1000);
		 if ( ret != HAL_OK ) {
			 printf("Error Rx\r\n");
			 for(int i = 0; i < 6 ; i++){
				 printf("data_rx[%i] = %u \n",i,data_rx[i]);
			 float t_ticks = data_rx[0] * 256 + data_rx[1];
			 float rh_ticks = data_rx[3] * 256 + data_rx[4];

			 float t_degC = -45 + 175 * t_ticks/65535;
			 float rh_pRH = -6 + 125 * rh_ticks/65535;

			 printf("t_degC = %.2d \n",t_degC);
			 printf("rh_pRH = %.2d \n",rh_pRH);







The results are as following (Serial console):







Trying to read SHT40 sensor 
Error Tx







And the logic analyzer shows the following:



I would appreciate any help. I have double checked everything and cannot spot a mistake.. What could be an issue with my I2C and why would it write such a garbage (when monitoring through logic analyzer?). It should write data 0xFD to the address 0x44



What I am confused about is why the I2C does not work as it supposed to. I have even disconnected SH40 sensor from my board and trying to send a dummy I2C command such as:



  uint8_t test_data = 0x88;
  HAL_I2C_Master_Transmit(&hi2c1, 0xf0, &test_data, 1, 0xff);



And from what I understand, it should send 2 bytes of data via I2C bus. First byte contains the device address, in this case (0xF0) and then 2nd byte the actual data, in this case (0x88).

Monitoring through logic analyzer:


This looks like a total garbage. The I2C line does not even have 16 clock cycles. What could cause this? It feels like I2C clock is cutting off early and not able to send all the required data.



Accepted Solutions

I have found the issues behind why I could not communicate with the SH40:

Since the I2C adresses are 7-bit, I need to do the following:

#define SHT40_ADDRESS (0x44 << 1)

and then I can use this in I2C write/read functions:

ret = HAL_I2C_Master_Transmit(&hi2c1, SHT40_ADDRESS, &data_tx, 1, 1);

 If I do not shift the address byte to the left by one, it will not be correct.

View solution in original post

Lead II

Hello @LPetr.1 

I suggest you to carefully debug your code. That's how you will be able to identify the error location.

Best regards.


I have added some additional information to my initial post. The issue is definately not related to the SHT40. I am not able to send a proper dummy command using I2C. For some reason not enough I2C cycles are generated hence complete data cannot be transfered.

Principal III

and what this will do ??


should be : data_rx .

If you feel a post has answered your question, please click "Accept as Solution".
Associate II

I think you are powering the board wrongly. The as per the doument below vin in should be used for powering the board, not the Vo which is meant to be voltage output. Thanks



Best Regards

Ankit  Bansal

That's exactly what I suspected @LPetr.1 .

Best regards.


I have updated my initial post. There seems to be issue with the I2C itself and it seems to be missing clock cycles. I dont think data_rx  or &data_rx  could cause this.

Sure this could have been an issue but for now I have removed the SHT40 from the equation as there seems to be issue with the i2c itself. Please look at my initial post update. The i2c seems to be missing clock cycles.

I have removed the SHT40 from the equation for now. There seems to be even stranger issue with the I2C on the STM32L031 (missing clock cycles). Please could you look at my initial post update. 

Are you using pull up resistors.

Else, can you try using 2 other I2C1 pins (if you have in your pinout).

Best regards.