# SparkFun Forums

### Magnetometers and Accelerometers and Gyros (oh my?)

Have questions about a SparkFun product or board? This is the place to be.

### Magnetometers and Accelerometers and Gyros (oh my?)#111621

By thebecwar
#111621
Ok, so it seems a lot of people are confused about the differences between magnetometers, accelerometers and gyroscopes. I've answered my share of questions about them, and am making this post so I don't have to keep typing the same things over and over. We'll start by looking at what each of the devices do, and then look at how combining them can give us our position in space.

Magnetometers:
Simply put magnetometers measure the variations in a magnetic field. The earth, being a rock filled with ferro-magnetic minerals, has a strong magnetic field that can be read by a magnetometer to give you an idea where north is. The actual angle to magnetic north won't quite get you to the North Pole, because the alignment of the magnetic field is a little off, and varies from place to place. Ultimately the magnetometer's most basic function is to act as a compass, providing a reference to north that can be used for navigation.

Accelerometers:
An accelerometer measures acceleration. It can be tuned to measure the acceleration due to gravity, but more commonly it is used to measure a device's movements. The frame of reference (meaning what you're measuring) is the device itself. These come in one, two, or three axis. The device measures acceleration, not velocity. Acceleration, for those that slept through physics class, is defined as the change in velocity as a function of time. If you drop a ball off a tower, the ball will gain 9.8 meters/second velocity over the course of every second. This is commonly referred to as m/s/s (meters per second per second) or m/s2 (meters per second squared). (Of course dropping a ball off a tower, the ball will encounter air resistance, and won't accelerate as quickly, but the 9.8m/s2 or 32ft/s2 will get you close. This also ignores terminal velocity, which is again, a topic that's beyond the scope of this introduction.)

Gyros (aka Angular Rate Sensors):
Gyroscopes measure rotational velocity. These are the devices used to measure pitch roll and yaw rates. These measurements combined with time can give you your current attitude (pitch/roll/yaw angle). Like accelerometers they come in one, two or three axis versions. (There are other types of gyros out there, but that's the most common, and the only one sold by SFE.)

How they all can be fused together:

The science of dead reckoning has been known by sailors the world over since at least the 1200s (probably earlier, because the Vikings were raiding villages all over the coast from the 700s on). Dead reckoning is the process of estimating ones current position based on knowing speed and direction. This method of navigation has been used for centuries, and allows you to get close to where you want to go, but it's not horribly accurate over a long period of time. An accelerometer and a timer is all you really need to determine your position (referred to as a fix). By calculating your linear velocity and applying how much time has elapsed, you can find out your distance travelled. This is not very accurate as it provides no reference, and no angle compensation.

Fixing the angle compensation issue is a simple matter of adding a gyroscope into the mix. Since the gyro measures rotation (angular velocity) it complements the features of the accelerometer nicely. Adding the gyro allows you to add pitch, roll and yaw into your calculations, improving accuracy, and allowing control of more complex devices. The problem with this system, is that without a known reference point you can't extract anything more than where you are relative to the starting point. In addition to this the device has no way of compensating for the natural drift in the figures returned from the gyro and accelerometer.

Compensating for drift and adding accuracy with other sensors:

Because gyros can drift over time, and measurement accuracy is not perfect, you need some way of re-calibrating your measurements and sanity checking your output. Using a magnetometer you can obtain a fixed reference point (north) that allows you to check your calculations against known figures. The three devices work in unison to provide you a fairly accurate picture of where you are, and where you're pointed.

Another way to improve accuracy would be the addition of a GPS. This allows you to check your position against a reference, and update your calculations based on a known figure.

Moving On From Here:

Hopefully the information here helps people understand the differences between the types of sensors, and how they can be used together. The next step would be to implement one of the algorithms used to determine position based on the data from sensors. You'll also probably want to filter your incoming input data (like a Kalman Filter). Building an IMU requires patience and some math skills. At a minimum you should know the Pythagorean Theorem, right triangle geometry/trigonometry (including the SIN/COS/TAN ratios), and some basic vector addition and subtraction.

(For those into mathematics check out: http://code.google.com/p/imumargalgorithm30042010sohm/ --Written by forum user sebmadgwick)

theBecwar

(If anyone has any information to update this, or noticed something I screwed up, please let me know so I can fix it.)
Last edited by thebecwar on Mon Nov 08, 2010 2:28 am, edited 1 time in total.

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#112897

By study
#112897
Thanks a lot for this usefull post.
Is there any difference between "gyro" and "angular rate sensor"?!!
what's the gyro output only rotational speed or angle?
best Regards

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#112909

By tecoist
#112909
study wrote:Is there any difference between "gyro" and "angular rate sensor"?!!
what's the gyro output only rotational speed or angle?
Gyroscopes can be used to make an angular rate sensor (and that's the kind of gyroscope you'll find at SFE), but they can be used for many other applications (e.g., gyrocompass or attitude indicator). So it would be more precise to say "angular rate sensor" rather than "gyro."

The angular rate sensors output rotational speed. It is possible to build a gyroscopic device that (with some limitations) directly outputs angle information (e.g., both the gyrocompass and the attitude indicator display angles directly).

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#112911

By study
#112911
I think i understand what you mean but a new question:
Is there any chip or sensor that give us the angle? or all sensors need processor to convert angular rate to angle?
or let me ask my question in another form: ( sorry my english is not so good to say my means i hope you understand what i mean ) :
is there any gyro with angle output or all of them give us angular rate and we should convert it to angle ourselves ?
i'm a little confuse in words, actually gyro = gyroscope right? or gyro = angular rate sensor?
Tanks again

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#112914

By tecoist
#112914
study wrote:is there any gyro with angle output or all of them give us angular rate and we should convert it to angle ourselves
There are gyroscopic devices with angle outputs, but I don't know of any inexpensive ones.

There are no gyroscopic devices that can independently give you heading information (that's one of the traditional "angles" used to describe the orientation of a platform). There are (expensive) gyroscopic devices that can give you pitch and roll (the other two traditional angles), but they essentially cheat and integrate acceleration information over time to establish the vertical direction (when you see references to "self-erecting" gyroscopes, that's what they're talking about).

Magnetometers can give you magnetic heading. Even though this is significantly different from geographical heading in most places, it's adequate for many purposes and preferred or required for some (for instance, air traffic control uses magnetic headings).

There's a minor error in thebecwar's original post: over most of the earth's surface magnetometers do not give you pitch and roll values. It's geometrically impossible, because the reference direction they provide is not aligned with gravity. There are an infinite number of possible pitch and roll values associated with any particular set of magnetometer values. Now if you know that roll, for instance, is constrained, you can measure pitch, or vice versa. But in the general case where your platform is free to rotate, no.

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#112920

By study
#112920
Thanks master
i saw a tilt sensor that is inexpensive and i think output is angle too, maybe i can use it to balance my quadcopter. can you take a look at this sensor and tell me why thats cheap? here is datasheet:

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#112943

By study
#112943
"The ZCT part is a two axis accelerometer, not a gyro. It measures tilt by measuring the change in g load. That will only work in an inertial reference frame."

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#112988

By thebecwar
#112988
tecoist wrote:There's a minor error in thebecwar's original post: over most of the earth's surface magnetometers do not give you pitch and roll values. It's geometrically impossible, because the reference direction they provide is not aligned with gravity. There are an infinite number of possible pitch and roll values associated with any particular set of magnetometer values. Now if you know that roll, for instance, is constrained, you can measure pitch, or vice versa. But in the general case where your platform is free to rotate, no.
Yes, but no. The magnetic field is not aligned with the gravitational field, but the magnetic field has an inclination error, which can be used to calculate a reference angle. It all depends on your location on earth, and the magnetic variations at that location.

I'm going back and removing the reference, because it could be confusing. I'm also adding a change to the gyro section that clarifies that the most common type, and the only type found here is the angular velocity sensor.

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#113017

By tecoist
#113017
Yes, if you know (even approximately) where you are, you know what the local inclination is. However, you can't turn that into pitch and roll, on its own. I know it seems intuitively reasonable that it should be possible ("well, the inclination is 40 degrees, and my platform's saying 50 degrees, so I must be pitched up 10 degrees!")--but in fact, that's not true.

Here's one way to see this: suppose you add an axle to your platform, pointing in exactly the direction the magnetometer is giving you. Now if you spin the platform around that axle, the magnetometer readings won't change at all, but roll and pitch will vary (roll will go all around the clock, pitch is constrained to a range of values). Same thought experiment will tell you the Fun Things that happen at the magnetic poles (inclination 90 degrees: no heading information, and if the signal weren't so weak and noisy, you would get pitch and roll from the magnetometer) and magnetic equator (no roll information, but you can measure pitch).

Isn't geometry fun?

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#113020

By phalanx
#113020
I figured there was enough good information in this post so I made it an announcement so it stands out from the rest.

-Bill

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#113036

By michael.everett
#113036
Hey all, great and informative thread so far. I'm just starting to research this and get into it. I had become aware of the papers by Mahony, but hadn't run into the posted link yet. What I would like to do is to create a 3D motion tracking system that is wireless. Obviously that is a pretty generic and useful device, but my first test case is that I would like to encase the device I create into a special head for poi. For those that don't know poi is the art of fire spinning, so we're basically talking about some kind of orb attached to the end of a chain or string/fabric being spun all around in all directions by the performer. I have many reasons and applications for it, but I won't go into them here. And no I'm not suggesting the thing be on fire while being spun!

From the research so far it definitely seems like I will want a gyro and accelerometer with good bandwidth and precision. The posted link and paper make the case that adding a 3 axis magnetometer to the mix further increases accuracy by allowing for further mitigation of bias and drift error. Does anyone have any experience with this?

I have a few unknowns and I'm wondering if anyone has worked with IMUs and MARG/AHRS systems extensively and might have some experience...

The main deal is, the poi are being spun pretty quickly, I'm not sure of actual force estimates, but the speed and rate of change of forces should be pretty high compared to other applications like robotics. Obviously the higher the sample rate the better (in theory). But any experience with the limits and ability of these devices (let's say the ones sold on Sparkfun) to cope with such fast rates of change and high force? Also there is a ton of centrifugal forces involved. Does that pose any special considerations?

Secondly I want it to be wireless and report to a computer "base station" for processing. The idea currently is to use a Arduino compatible board, mated with an XBee. I'm thinking it probably makes the most sense to just use the Arduino to read the sensors as fast as possible, and stream the raw sensor values as fast as possible to the computer and do the heavy lifting on the computer. Anyone have any experience that would indicate otherwise?

Finally in all of the video demos I see of people playing with IMUs they always have a 3D representation on the computer that is demonstrating realtime simulation of the data. But the simulated object never translates its position, it just rotates. These devices are capable of tracking position in space too right (by calculating velocity + orientation over time)?

So yeah basic 3D motion capture. Accuracy doesn't have to be perfect, but just approximate the movements reasonably. It should be wireless, and be able to deal with the force of being swung around on the end of a rope. For anyone out there who has experience with IMUs/AHRS systems, does this seem feasible or am I way out of line assuming it should be possible?

Thanks!

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#113041

By thebecwar
#113041
phalanx wrote:I figured there was enough good information in this post so I made it an announcement so it stands out from the rest.
-Bill
Gracias.

Anyone can still feel free to let me know about any changes to the text. I'm always willing to modify it to work better as a primer/design guide.
michael.everett wrote:Hey all, great and informative thread so far. I'm just starting to research this and get into it. I had become aware of the papers by Mahony, but hadn't run into the posted link yet. What I would like to do is to create a 3D motion tracking system that is wireless.
[...]
Secondly I want it to be wireless and report to a computer "base station" for processing.
[...]
I take it that traditional motion capture (cameras and pingpong balls) won't work for you. The MARG system may be a little bit bulky for your end use, but a accelerometer/gyro solution may work nicely. It's all about the accuracy of your sensors, and the accuracy of your algorithm. Centrifugal force will act in a known direction and magnitude, so you can work your processing algorithm to filter that information out. Choosing sensors can be difficult, but calculating a ballpark figure using newton's laws, and then giving yourself a buffer will help. Processing on an external computer will help a lot, allowing the microcontrollers involved to concentrate on shoveling the data from point A to point B. The difference in processing speed/ALU abilities gives you a significant calculation advantage on the computer. You should also be able to use a higher sampling rate, since you aren't wasting cycles working with the data before transferring it.

And yes, IMUs can track position as well as orientation. (Apollo used IMUs to land on the moon.)

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#113224

By thebecwar
#113224
Finally got around to doing something with my webserver... As a result, the Magnetometers Accelerometers and Gyros Tutorial can be edited by anyone. (Yay mediawiki!)

http://www.thebecwar.com/wiki/Magnetome ... _and_Gyros

(I'm also open to a better wiki title.)

### Re: Magnetometers and Accelerometers and Gyros (oh my?)#118865

By allegutta
#118865
Hi thebecwar,

I have a HMC6343 tilt compensated magnetometer (http://www.sparkfun.com/products/8656), and from this module I can get the tilt data. In your first post you wrote that you only can get the tilt data from a gyro, but how can I get the tilt data from this module when there is no gyro in it? I am a little confused.

Kind Regards