I made a board with the Analog Devices ADXL345. I then attached it to my robot and did obstacle avoidance with data logging for about 10s. The robot made 4 turns. Here is a graph of z-axis readings. I set the ADXL345 to +/- 2g. It is 10bit. This data doesn't look right. Is this what I would see from vibration, or is this what I should see?

enter image description here

  • \$\begingroup\$ Did you expect that the reading will be around 1g (equivalent value in A/D counts) all the time? \$\endgroup\$ – Nick Alexeev Oct 30 '13 at 3:44
  • \$\begingroup\$ How about trying to log with the robot still for 10 seconds first? \$\endgroup\$ – darron Oct 30 '13 at 3:51
  • \$\begingroup\$ When I let it sit still for 10s, all the reading were around -14. Still not sure what is wrong. \$\endgroup\$ – BSEE Oct 30 '13 at 5:01

That's a very bimodal distribution - not something you should see from vibration. My guess is you are either not reading the right data from the chip or you are interpreting it incorrectly. This part uses two byte values for the outputs - it looks like you may have the MSB and LSB swapped.

Edit: I'm actually going to say that you almost certainly have the MSB and LSB swapped. The sensitivity on the 2g scale is 256 LSB/g. This means that you should be seeing a raw reading of + or - 256 or so, assuming the chip is mounted with the axis vertical. The LSB will be mostly 'noise' while the MSB will be mostly zero. If the bytes are swapped, then the sign bit becomes what should have been bit 7 and it will flip more or less randomly. If the original value is 255, byte swapping results in -256. If the original value is 256, byte swapping results in value of 1. If the original value is 127, byte swapping results in 32,512. If you are dividing by the sensitivity, you should see a highly bimodal distribution with lot of points with small negative values and a lot of points with large positive values.

  • \$\begingroup\$ I tried simply flipping the bytes but got values around -3232: z = ((int)values[0]<<8)|(int)values[1]; \$\endgroup\$ – BSEE Oct 30 '13 at 5:12
  • \$\begingroup\$ The data is in 2's compliment. I see on Sparkfun's site someone else had a similar issue but no solution was posted. \$\endgroup\$ – BSEE Oct 30 '13 at 5:28
  • \$\begingroup\$ You were right but it is the code. The "z=" code I showed above must have been sign extending the LSB (values[1]), then oring it in. There is another bug where the data logged is not being retained but there is a workaround so it's effectively fixed. \$\endgroup\$ – BSEE Oct 30 '13 at 6:07
  • 1
    \$\begingroup\$ Yeah, if you cast it to int before you shift it, it will be sign extended and it will cause problems. What you want to do is cast it to unsigned int, combine it, then cast it to a signed int. \$\endgroup\$ – alex.forencich Oct 30 '13 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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