cancel
Showing results for 
Search instead for 
Did you mean: 

How to get x, y, and z axis values at single command in LIS2DH12 through I2C?

YFuku.3
Associate III

I've attempted to get values at x, y, and z axes at once, but it doesn't work well.

What I want to do is to check if a device where the LIS2DH12 is populated stays still or moves. So I need to get x, y, and z values.

So far, I read each value by each in a bypass mode, like first read x value, then y value, and finally z value. It's redundant and I want to read it at one-shot command.

I read 6 bytes from OUT_X_L (28h) and stored them to rx_buf[6]. The result should be

OUT_X_L(28h) = rx_buf[0]

OUT_X_H(29h) = rx_buf[1]

OUT_Y_L(2Ah) = rx_buf[2]

OUT_Y_H(2Bh)= rx_buf[3]

OUT_Z_L(2Ch) = rx_buf[4]

OUT_Z_H(2Dh) = rx_buf[5]

However, all the values in rx_buf are 0.

Which mode should I use. How can I read x, y, and z values at one time?

3 REPLIES 3
Eleon BORLINI
ST Employee

Hi @YFuku.3​ , a couple of question: is the I2C working (i.e. are you for example able to read the WHO_AM_I register)? And did you properly initialized the LIS2DH12 sensor (please check the AN5005 app note, p. 13).

You can follow the Github drivers guidelines to check the effectiveness of your code --> https://github.com/STMicroelectronics/STMems_Standard_C_drivers/blob/master/lis2dh12_STdC/example/read_data_simple.c

Regards

YFuku.3
Associate III

Hi Eleon!

> is the I2C working (i.e. are you for example able to read the WHO_AM_I register)?

Yes, it works fine. I can read WHO_AM_I register.

>did you properly initialized the LIS2DH12 sensor (please check the AN5005 app note, p. 13).

I think I configure LIS2DH12 properly because at least I can read each value of x, y, and z axes.

One more question: Can I detect if a device where the LIS2DH12 is populated stays still or moves by Click Interrupt function?

I appreciate your help!

Winfred LU
ST Employee

Hi Yusukenchief,

> I read 6 bytes from OUT_X_L (28h) and stored them to rx_buf[6]. The result should be

> However, all the values in rx_buf are 0.

As Eleon has pointed out, please initialize the sensor properly.

Please check CTRL_REG1(20h) firstly.

> Which mode should I use. How can I read x, y, and z values at one time?

Please read 6 bytes from offset A8h, which is the equivalence of 28h with MSB bit set.

After the start condition (ST) a slave address is sent, once a slave acknowledge (SAK) has been returned, an 8-bit sub-address (SUB) is transmitted: the 7 LSb represent the actual register address while the MSB enables address auto increment. If the MSb of the SUB field is ‘1’, the SUB (register address) is automatically increased to allow multiple data read/writes.

> One more question: Can I detect if a device where the LIS2DH12 is populated stays still or moves by Click Interrupt function?

Yes. Please refer to AN5005 for section 8.1 Single click.

Best Regards,

Winfred