Package Tracker – Accelerometer
Understanding the accelerometer proved to be tricky. Finding answers required much searching and reading. The raw output collected in the CSV file may seem perplexing if you are new to microcontrollers. Upon first examination, I thought that my device was broken: several axes seemed to vacillate wildly between 0 and 255 with even the slightest movement.
Since the accelerometer measures to +/- 2g, and it senses the acceleration of gravity, I expected, with the device at rest to measure the X and Y axis at 128 and the Z axis at near 196. Section 2.2.2 of the LIS302DL datasheet, explains several important points:
1. Output from the device is “expressed as a 2′s complement number“.
2. Zero g output “can slightly change after mounting the sensor onto a printed circuit board”.
Zero gravity then is measured as 0 binary, ignoring any offset. You may scour the remaining 41 pages of the document and you will not find the scaling factor. In other words, when the output changes by one bit, how much acceleration does this bit represent? To find out how acceleration is measured you’ll need ST’s application note AN2335. In section 9.3, it states that “each LSB corresponds to 18mg”. Thus 1.0 g = approximately 55 decimal (110111 binary). To scale the data in the CSV file you’ll need to use the handy basic linear equitation for each axis:
Y = mx +b
m = 0.018g
x = the normalized output of the accelerator (output in +127 to -128 form)
b = the offset from zero measured on your individual sensor
What about this two’s complement stuff? In Ms Excel, it is not pretty. Excel doesn’t seem to have many binary manipulation functions. After finding DEC2BIN I came up short and hacked a formula. Read numbers from zero to 127 as is and for larger numbers subtract 256. I may be off a digit. Calculations that are off a digit are a common mistake in binary math, and I didn’t check this work closely yet.
What remains for me to discover is: how can I separate the acceleration of gravity (g) from acceleration (a), the change in velocity / change in time? My recollection of the old electromechanical accelerometers is that they only measure change in motion, but I may be mistaken.