# Finding angle in cell phone using accelerometer

1. Aug 28, 2009

### deck123

I am using a cellphone with 2 accelerometers in it. So, in all I have 2*3 readings from accelerometer ( 3--> x,y, z axis). I am trying to find the orientation (up and down) and the tilt/angle of the cell phone. The cell phone won't be kept in the horizontal surface, instead it would be rotated in 360 degrees.
Any help would be appreciated.

2. Sep 1, 2009

### InTuoVultu

If the phone is not rotating, both accelerometers are going to be pointing in the same direction. So really, you have one vector you can work with, and that's the gravity vector. That means that if you tilt the phone vertically in x/y/z (like the difference between you holding the phone to your face and the phone lying on the desk). But if you put the phone on your desk, and then keeping it flat, spin it 180 degrees, you can measure the acceleration of the spin, but once that stops there's no way to know for certain which way you're pointed. You can try to be fancy and say, "well I was accelerating in this direction, for this long, so I was probably going this speed for this long, so I am probably here" But this means that any error will just keep adding and adding with no way to know for sure where you're pointing.

I think this is what you're asking.

3. Sep 5, 2009

### MurrayMD

If I understand your problem correctly you want your phone to know its 3D orientation. You need at least two vectors to do that. The xyz accelerometer produces a gravity vector that goes straight down. An xyz magnetometer can provide a second vector that is horizontally towards magnetic north and vertically downward in the northen hemisphere. The cross-product of these two vectors will be horizontal the magnetic east-west directions and the cross-product between the east-west vector and the gravity vector will be horizontal in the magnetic north-south directions. Formulas exist for converting magnetic to geographical north although I don't know them offhand. An xyz accelerometer by itself produces just the gravity vector, which could allow you to use your cell phone as an electronic bi-directional level (lateral and axial to the orientation of the accelerometer).

Last edited: Sep 5, 2009
4. Sep 8, 2009

### deck123

Actually measuring acceleration in not my main issue. I am using accelerometer for my convenience. I need to find the orientation in the 3d sphere of the phone, i.e. angles in the 3d space. I need the angle information to convert xyz values of accelerometer data to a particular direction from the orientation the cellphone is currently placed.

5. Sep 8, 2009

### MurrayMD

If you want 3D orientation you need two vectors. Gravity gives you one. A magnetometer can give you the other.

6. Sep 13, 2009

### deck123

THanks for the reply.. I looked around and couldn't find the formula that u mentioned in the previous post. Can you give me a link to it.

7. Sep 14, 2009

### MurrayMD

Wikipedia's article on magnetic declination has a link to the National Geophysical Data Center (NGDC) where there's an online geomagnetic declination calculator and other useful links: http://www.ngdc.noaa.gov/geomagmodels/Declination.jsp [Broken]

Geomagnetic north changes over both both time and location (due to factors like hematite and magnetite in the earth's crust) so maps can become outdated quickly. If you just want to find your way out of the woods or get a Boy Scout badge the maps will work fine but if you're drilling for oil and want to hit your target within 5 feet at five miles down you might be inclined more towards the calculator!

At least I think that's what you were asking. A cross-product (e.g. a x b) is a geometric operation between two vectors "a" and "b" whose magnitude is the product of a,b and the sine of the angle between them, i.e. ab sin(theta) and whose direction is perpendicular to the plane defined by the first two vectors. (It's in the Wikipedia as well; just Google "cross-product wiki") The direction can be one of two (opposite to each other) and follows the "right hand rule" where if you were to physically grab the cross-product vector with your right hand with your thumb out and pointing in the same direction, the vector closest to your knuckles would be the "a" vector and the vector closest to your fingertips would be the "b" vector. If you reverse the order of the multiplication to "b x a" the cross-product will be in the opposite direction.

The reason for the cross-products is to take your two available vectors, magnetic north and gravity and build them into a 3D Cartesian coordinate system whose three principal axes are 90 degrees apart. The magnetic declension will give you the angle from magnetic north you need to rotate your coordinate system to align it to geographical north.

Last edited by a moderator: May 4, 2017