Skip to main content
ATavo.1
Associate III
April 18, 2022
Solved

Why HAL_I2C_Mem_Read returns only 0?

  • April 18, 2022
  • 12 replies
  • 4854 views

Hi,

I am using stm32f1 and HUT21D sensor (Temperature and humidity sensor). The sensor use i2c for communication.

I am using IAR and cubeMX.

For cube: I used default setting for i2c.

For IAR: I shared the main.c below.

Can anyone help on why only get 0.

#I2C​ #[STM32 MCUs]​ 

This topic has been closed for replies.
Best answer by MFran.4

It could be the wires but 10cm is still a human distance. Did you measure with an oscilloscope SDA and SCL? Are the waveforms clean?

12 replies

Tesla DeLorean
Guru
April 18, 2022

Does the function return an error or success code?

If HTU21D_RX_DATA is filled with a different data pattern, does it return that?

That would be indicative that the device is not responding.

Check wiring, pins used, pull-ups, and slave address

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
April 18, 2022

Slave address, and register address look correct, is it timing out, or not responding at all?

Have you scoped the pins?

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
ATavo.1
ATavo.1Author
Associate III
April 20, 2022

It works fine. No timing out or any error in debug but the return of the HAL_I2C_Mem_Read is HAL_Error.

ATavo.1
ATavo.1Author
Associate III
April 18, 2022

I used the debug and I didn't get any errors and I only get zero.0693W00000LyDsPQAV.jpgI checked the pull-ups and the slave address is 0x40.

It seems okay.

The data sheet for the sensor is attached below.

Tesla DeLorean
Guru
April 18, 2022

The array defaults would be zero.

Any other registers in the part readable?

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
ATavo.1
ATavo.1Author
Associate III
April 20, 2022

sadly no.

MFran.4
Senior
April 20, 2022

Scoping the pins is a good idea.

What does HAL_I2C_MemRead return as status? (stat = HAL_I2C_Memread(...) )

ATavo.1
ATavo.1Author
Associate III
April 20, 2022

It show HAL_ERROR

ATavo.1
ATavo.1Author
Associate III
April 20, 2022

I

ATavo.1
ATavo.1Author
Associate III
April 20, 2022

It seems that one the capacitors was not correctly soldered and it change the ground. it works for now.

Thnx everyone.