Skip to main content
Visitor II
November 1, 2022
Question

To read two sensor values with i2c

  • November 1, 2022
  • 4 replies
  • 1723 views

I'm trying to get the sensor value with stm32

Maybe because I connected the board to one i2c and attached two sensors, the value didn't change

Most people use cereal, but I only use C, so I have to use HAL

The sensor that allows you to read only one is HTU21

I'm doing this and that, but I only got 01 00 00 00 00

I think I didn't code properly, but I don't know where to fix it

I want to get the sensor value and use it as a hex value and float value, so please tell me how

uint8_t hum_addr = 0x40;
 uint8_t hum_sec_addr = 0xE5; 
 uint8_t buf[5] = {0x01, 0x00 }; 
 uint8_t R_buffer1[5] = {0x01, 0x00 }; 
 uint8_t R_buffer2[5] = {0x00, 0x00 }; 
 HAL_StatusTypeDef ret;
 
//ret = HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t)hum_addr, (uint8_t*)R_buffer1, 2); 
 //ret = HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)hum_addr, (uint8_t*)R_buffer2, 2);
 ret = HAL_I2C_Mem_Read_IT(&hi2c1, (uint16_t)(hum_addr << 1), hum_sec_addr, 1, R_buffer2, 5);
 for(int i = 0; i < 5; i++){
 printf("%02X ", R_buffer2[i]);
 }
 printf("\n\r");

Humidity sensor datasheet

http://dlnmh9ip6v2uc.cloudfront.net/datasheets/BreakoutBoards/HTU21D.pdf

Humidity sensors are connected to SD0, SC1 via i2c

0693W00000VOZPSQA5.png

    This topic has been closed for replies.

    4 replies

    ST Employee
    November 1, 2022

    Hello @ark​,

    I suggest you try to use the I2C sensor address without shift:

    HAL_I2C_Mem_Read_IT(&hi2c1, (uint16_t) hum_addr , hum_sec_addr, 1, R_buffer2, 5);

    Hope my answer helped you!

    When your question is answered, please close this topic by choosing Select as Best.

    This will help other users find that answer faster.

    Foued

    Graduate II
    November 1, 2022

    0xE5 doesn't seem like it can be a valid address.

    ST Employee
    November 1, 2022

    It's a measurement command (Temperature) for the read.

    0693W00000VObgGQAT.pngFoued

    Visitor II
    November 1, 2022

    Take an oscilloscope (not a logic analyser guys) and observe the i2c bus. Monitor the slave address ack bit.

    Graduate
    November 1, 2022

    The code doesn't appear to provide the 7 to 50 msec Hold during measurement. See page 5 of the data sheet.

    (Edit) You might try asking for example code as stated on data sheet page 10. The data sheet date is 9 years ago, so there may be no response.