cancel
Showing results for 
Search instead for 
Did you mean: 

To read two sensor values with i2c

ark2
Associate III

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

5 REPLIES 5
Foued_KH
ST Employee

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

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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

0693W00000VObgGQAT.pngFoued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

S.Ma
Principal

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

raptorhal2
Lead

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.