# Some basic questions about an accelerometer's unexpected behavior while tilted

I have an ADXL335 accelerometer connected to my Arduino. Here is some code that I am running based off a tutorial:

    xaccl[a] = float(analogRead(xpin) - 345);

float length = sqrt((xaccl[a] * xaccl[a]) + (yaccl[a] * yaccl[a]) + (zval[a] * zval[a]));

Serial.println(length);


When the accelerometer is flat on my desk, the vector length is around 128. When the accelerometer is tilted, the length of the vector increases even if it is not moving. The 3d vector length should stay constant regardless of the angle.

1. Could someone explain what is happening?
2. Also, what unit of measurement does the accelerometer output? I assume m/s^2.
3. Lastly, some sample code I found for reading values:
    xaccl[a] = float(analogRead(xpin) - 345);


Why is the code subtracting 345, 345, and 416?

The vector magnitude is constant regardless of orientation, but you are applying offsets in one direction which throws off the symmetry.

You can't calibrate away gravity in only one orientation and expect it to work for all orientations. Thinking about what you did and if you flipped it upside down instead of a slight tilt. Would you expect it to be zero? It's adding all your offsets in the wrong direction.

Those gravity offsets are only useful if the accelerometer moves but orientation doesn't change. In that scenario, gravity is calibrated out because its direction and magnitude are always known.

• Gravity is not the issue nor is calibration. I updated my original post to give a simpler explanation of the situation. In short, gravity like you said is constant so the length of the 3D vector should also be constant. Please take a look – Cb32019 Aug 9 at 19:47
• The vector magnitude is constant regardless of orientation, but you are applying offsets in one direction which throws off the symmetry. – DKNguyen Aug 9 at 19:48
• You mean the -345, 346, and 416 offsets? That was in some sample code, I don't know why it was there. I got it off some pedometer tutorial and I could tell the guy didn't know how to program. Anyways, YES that was it. Thank you. – Cb32019 Aug 9 at 19:50
• Those subtractions are only useful if the accelerometer moves but orientation doesn't change. In that scenario, gravity is calibrated out because its direction and magnitude are always known. So if you were doing cartwheels, your pedometer would not work. – DKNguyen Aug 9 at 19:51
• @Cb32019 Find out why your code subtracts 345, 346 and 416. DKNguyen guesses that these are to calibrate for gravity. If his guess is correct, then yes, the problem has everything to do with gravity and calibrating. – user253751 Aug 10 at 13:00

You probably haven't allowed for gravity. An accelerometer can't tell the difference between acceleration and gravity. So there always appears to be an acceleration of 1g (9.81 m/s²) downwards.

You probably calibrated that away when the accelerometer was sitting on your desk. But the moment you tilt the accelerometer, you change the axis in which it sees gravity, and you start getting non-zero readings, even when you stop moving it.

I rather suspect that the values of 345, 345, and 416 are the ones that worked for the person who write the tutorial, on their accelerometer. Don't assume that they will work for you. A stationary accelerometer should always give an acceleration of 1g downwards, whatever orientation it is in.

• Gravity is not the issue nor is calibration. I updated my original post to give a simpler explanation of the situation. In short, gravity like you said is constant so the length of the 3D vector should also be constant. Please take a look. – Cb32019 Aug 9 at 19:46
• -345 -346 -416 is probably calibrating for gravity. But you don't know why your code does that, so you don't even know you're doing it! Don't write code if you don't know what it does. – user253751 Aug 10 at 13:00
• This was already solved on august 9th – Cb32019 Aug 15 at 0:24

first of - you might want to share with us the tutorial so maybe someone can follow the code better...

2nd - the acc output units is mv . in the DS of adxl you can see that the sensitivity is 300mv/g .

but the measurement range is 3.6g which is translated to maximum of 1.08v.

you also have 0g offset from the IC as mentioned in the DS :

for x,y : max offset is 1.65v

for z : max offset is 1.8v

Arduino A/D converter is 10bit with 5v voltage therefore for output of 345 the voltage in the input to the converter will be 5v/1024 * 345 = 1.686 , and for 416 it will be 2.03(seems a bit out of spec ). so regarding your question why subtracting - my guess is its because of the zero g offset cancellation.

also I have no idea why would you build a 3d vector and that's not the issue , but before looking at 3d vector I would recommended you will look at 3x1-dimension vectors . it might make more sense to you.