2023-09-18 12:17 AM - edited 2023-09-18 01:36 AM
Hello. I have STM32L031 nucleo board: https://www.st.com/resource/en/user_manual/um1956-stm32-nucleo32-boards-mb1180-stmicroelectronics.pdf
And I am trying to write SHT40 driver for it. I have Adafruit SHT40 I2C temperature/humidity sensor:
https://www.adafruit.com/product/4885
I follow simple steps to setup my STM32 board for I2C:
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");
}
else{
//read bytes
HAL_Delay(10);
ret = HAL_I2C_Master_Receive(&hi2c1, 0x44, (uint8_t*)&data_rx, 6,1000);
if ( ret != HAL_OK ) {
printf("Error Rx\r\n");
}
else{
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
UPDATE
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.
Solved! Go to Solution.
2023-09-18 02:39 AM
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.
2023-09-18 12:40 AM
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.
II
2023-09-18 12:46 AM - edited 2023-09-18 01:19 AM
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.
2023-09-18 01:23 AM
and what this will do ??
(uint8_t*)&data_rx
should be : data_rx .
2023-09-18 01:27 AM
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
https://cdn-learn.adafruit.com/downloads/pdf/adafruit-sht40-temperature-humidity-sensor.pdf
Best Regards
Ankit Bansal
2023-09-18 01:29 AM
2023-09-18 01:32 AM
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.
2023-09-18 01:33 AM
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.
2023-09-18 01:34 AM
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.
2023-09-18 01:49 AM - edited 2023-09-18 01:49 AM
Are you using pull up resistors.
Else, can you try using 2 other I2C1 pins (if you have in your pinout).
Best regards.
II