# How to calibrate an accelerometer?

I'm confused about a few points on how to best calibrate an accelerometer, whose data will be used in an orientation sensor fusion algorithm.

As a summary, the most common approaches I've seen take measurements in 6 different orientations (1G in +x, -x, +y, -y, +z, -z), to then arrive at a max, min measured value. Offsets are then calculated as averages: `(max - min)/2`. And then a scaling factor is calculated. There's a few aspects of this that I'm unclear about, and was hoping it's okay to ask in a single question, since they are related.

1) the examples calculate the average as written above, rather than `sum all vals / N vals`. Isn't that approach more error-prone?

2) should the offset per axis be calculated once for each orientation, and then summed?

3) what's the best approach to calculate scale bias?

Example 1 (pseudo-code):

``````chordlengthX = (max - min)/2
chordlengthY = (max - min)/2
chordlengthZ = (max - min)/2

avg_rad = (chordlengthX + chordlengthY + chordlengthZ)/3

calibratedX = (x - offset)*scaleX
``````
``````rawrange = max - min
refrange = refmax - refmin // 1G??

calibratedX = (((x - min)*refrange)/rawrange) +refmin
``````

Something else altogether (as you can probably tell I need to revise my maths)?

4) if my surface is not completely level, I cannot trust my measurements since my 1G reference is not accurate?

Most accelerometers are usable out of the box. (It is the magnetometers that are difficult to use w/o calibration.)

That said, example 1 looks good until the last line. You need an x-offset which is simply the difference between the x-maximum and x-minimum.

Consider how the coordinates of the X, Y and Z values of an accelerometer would appear if plotted in 3D space. One that is perfectly aligned would appear spherical. But if one of the sensors is more sensitive than the others, the 3D plot might take on an egg shape. You have corrected for this in the first 5 lines of example 1.

Now, if one of the sensors is not centered the 3D plot would be shifted off the 0,0,0 point of the 3D plot. You can correct for that by finding the x-offset, y-offset and z-offset.

You should not need to find the value for 1G. You should not need to find a perfectly flat surface. To calibrate you need to rotate the accelerometer smoothly about its center. Take care not to expose the accelerometer to greater than 1G during calibration. Rotate the accelerometer in a random fashion until you are satisfied that all 3 sensors have experienced their most negative and most positive values.

• Thank you for taking the time to answer. Apologies for being dumb, but still confused about a couple of points. What's the error in the last line of example 1? From my understanding of your explanation, it seems it should be correct. We subtract bias, and then multiply by the sensitivity? I'm probably missing something. Secondly, if the sensor is moving about the wouldn't the maximum and minimum just be the range from the datasheet, for example if I'm yanking it about quite quickly? Feb 20, 2022 at 12:29
• It would be good if you carefully selected your variable names. Even in fake code. It was easy to assume the 6 "max" & "min" values were actually "x-maximum", "x-minimum", "y-maximum", "y-minimum", ect. But I did not know what "x" was in the last line of example 1. If I needed to guess, I think the last line is an example of how to use the 2 calibration values you seek. And might be written: "calibratedX = (x-raw - x-offset) * scaleX". Take care to smoothly rotate the accelerometer during calibration. Do not expose it to > 1G by jerking it around. Again, calibration may not be necessary. Feb 20, 2022 at 14:52
• Thank you again for taking the time to help out. I really appreciate it. I'll test this tomorrow and see how it goes. :) Feb 20, 2022 at 16:04