Sunday, January 20, 2013

Simple Accelerometer Data Conversion to Degrees

What I though would be a very simple search turned into hours of research and reading. My task is simple. Read an accelerometer, in this case an ADXL345, and determine what its orientation is in degrees. Geeez, you would have thought I was the first person to do this, so now I'll be the first person to just give you the simple lines of code to do it.

   double xyz[3];
   double ax, ay, az;

   ax = xyz[0];
   ay = xyz[1];
   az = xyz[2];
   double xAngle = atan( ax / (sqrt(square(ay) + square(az))));
   double yAngle = atan( ay / (sqrt(square(ax) + square(az))));
   double zAngle = atan( sqrt(square(ax) + square(ay)) / az);

   xAngle *= 180.00;   yAngle *= 180.00;   zAngle *= 180.00;
   xAngle /= 3.141592; yAngle /= 3.141592; zAngle /= 3.141592;

Yea I know this could have been better written, but it works. There. Done! My 3 hours of research resulted in 11 or so lines of code. There are lots of blogs and DIY posts that go into the physics of accelerometers and go on at length about vector addition, angles, radians etc. But if you're like me all I want to know is if my toy robot fell down and can't get up.

But credits are due. I did find something I could read and understand without a masters in math at My code is a distillation of code found on page 5.

Note the get.Gxyz method returns (what I understand) are voltage levels from 0 to about 1.1 volts. My ADXL345 is running on IIC, but I can only assume if you are reading analog voltages through analog input pins you would get something similar.

Now, if you use this code you might notice some slight problems. You see the accelerometer should be calibrated, and/or the readings should be normalized. But this code will get you going if your task is rather simple. If you're building a fleet of quadcopters this might not be enough code.

I'm reading angles from 0 to 89.8 degrees in both the positive and negative direction. Now I don't think I'm finished with my angle calculations because I expect my toy to get bounced around a bit and I'll have to sort that out, but this is a simple first start.

I wish I could have found a post like this last night when I started my research, so hopefully you will find this of use, which is the purpose of this blog. Adruinos for Gumps.


  1. Thanks for your code, I was looking for the same conversion. Did you add any more code for calibration?

  2. Thanks for sharing, This was useful in my Corona project.

    Here is the Lua code if anyone needs it

  3. This comment has been removed by the author.

    1. This comment has been removed by the author.

  4. I wish I could have found a post like this a little before and I found. Thank you!!

  5. Excellent! Thanks very much. Anyone got a nice low pass filter snippet?

  6. Excellent! Thanks very much. Anyone got a nice low pass filter snippet?

  7. tht really helped bro. thnxx

  8. Thanks and Impressive Article. Outsourcing Data Conversion requirements of many a business has proven to be vital and wise decision. It helps them to function effectively and save a considerable amount of time and money. IT Outsourcing Services offer to convert your data into a powerful management resource. However difficult, complex or voluminous be your data conversion project, we will get it completed quickly and cost effectively. With the support of experienced team of Data Conversion professionals and state-of-the-art technology, we can obtain data from virtually any media source or file and convert it into usable formats. Our experience and capability to provide the complete Data Conversion solutions make us leaders in the industry.

  9. Best Web Development Services are Available at reduced cost.