I am reading LIS3DSH accelerometer datasheet and I have some questions regarding following text:

2.2 Understanding acceleration data

The measured acceleration data are sent to OUT_X_H, OUT_X_L, OUT_Y_H, OUT_Y_L, OUT_Z_H, and OUT_Z_L registers. These registers contain, respectively, the most significant part and the least significant part of the acceleration signals acting on the X, Y, and Z axes. IEA = 0 IEA = 1 Startup sequence AN3393 14/100 Doc ID 018750 Rev 2 The complete acceleration data for the X (Y, Z) channel is given by the concatenation OUT_X_H & OUT_X_L (OUT_Y_H & OUT_Y_L, OUT_Z_H & OUT_Z_L) and it is expressed in 2’s complement number.

Now, since accelerometer readings are 16bits in size, I know I have to merge the values with AND function, like it states in quote from datasheet?


1 Answer 1


No, not AND, you want to use OR. You need something like this:

int x = (((unsigned int) OUT_X_H) << 8) | ((unsigned int) OUT_X_L)
int y = (((unsigned int) OUT_Y_H) << 8) | ((unsigned int) OUT_Y_L)
int z = (((unsigned int) OUT_Z_H) << 8) | ((unsigned int) OUT_Z_L)

This will shift over the high byte and then OR the two bytes together for the 16 bit result. Even though the 16 bit number is signed, you need to do the combination on unsigned numbers so that you don't accidentally sign-extend something. Make sure you don't get the MSB and LSB mixed up, either! Just a few days ago I helped someone debug this exact problem - reading from an accelerometer and accidentally reassembling the data incorrectly.

  • \$\begingroup\$ Ok, but If I debug my code and place breakpoint on read values, the values are very different every time, but the board itself is not moving, it is on the table? Why is data at every read so different? I would like to turn on one of "direction" leds on STM32F4 Discovery Board. How do I "interpret" in which direction board is moved? And why the data merging is then described in quote with AND function? \$\endgroup\$ Commented Nov 5, 2013 at 4:19
  • 2
    \$\begingroup\$ Well, there is going to quite a bit of noise. Also, there is going to be a 1g acceleration in one direction, unless you happen to be on a satellite or something. I would imagine you could get quite a bit of variance in the LSB, but the MSB should be relatively stable. \$\endgroup\$ Commented Nov 5, 2013 at 4:25
  • \$\begingroup\$ You are right, MSB is quite stable. Now, if I lean board to left side, that means, with right hand I fix board and with left hand i move it away from my body, I should get negative g (negative value) on z axis? Is my assumption right? \$\endgroup\$ Commented Nov 5, 2013 at 4:32
  • \$\begingroup\$ No idea. I don't know what orientation the chip is mounted in. I would suggest orienting the board in different directions and not wiggling it for starters. This way you can see when each axis is pointing up or down. \$\endgroup\$ Commented Nov 5, 2013 at 5:18
  • 1
    \$\begingroup\$ Yes, the noise is expected, you must apply averaging. I'm familiar with programming the LIS3 series, we use them on the OLPC XO-1.75 and XO-4 laptops. Another thing you should watch out for is reading the sample as a result of an interrupt, within the time before the next sample is latched into the registers. If you simply read in a loop without any synchronisation with the device, you will sometimes catch it in between updates. But if you really can't synchronise, set the frequency low and ignore duplicate samples. wiki.laptop.org/go/Accelerometer \$\endgroup\$ Commented Nov 5, 2013 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.