2
\$\begingroup\$

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?

\$\endgroup\$
6
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Cb32019 Aug 9 at 19:47
  • \$\begingroup\$ The vector magnitude is constant regardless of orientation, but you are applying offsets in one direction which throws off the symmetry. \$\endgroup\$ – DKNguyen Aug 9 at 19:48
  • \$\begingroup\$ 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. \$\endgroup\$ – Cb32019 Aug 9 at 19:50
  • \$\begingroup\$ 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. \$\endgroup\$ – DKNguyen Aug 9 at 19:51
  • \$\begingroup\$ @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. \$\endgroup\$ – user253751 Aug 10 at 13:00
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Cb32019 Aug 9 at 19:46
  • \$\begingroup\$ -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. \$\endgroup\$ – user253751 Aug 10 at 13:00
  • \$\begingroup\$ This was already solved on august 9th \$\endgroup\$ – Cb32019 Aug 15 at 0:24
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

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.