Electronic – arduino – Some basic questions about an accelerometer’s unexpected behavior while tilted

accelerometeradafruitarduino

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);
    yaccl[a] = float(analogRead(ypin) - 346);
    zaccl[a] = float(analogRead(zpin) - 416);

    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);
    yaccl[a] = float(analogRead(ypin) - 346);
    zaccl[a] = float(analogRead(zpin) - 416);

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

Best Answer

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.

Related Topic