You can use a three-axis magnetometer just like a three-axis accelerometer to determine the local magnetic vector, like the local gravity vector. Then you can use changes in the vector to determine your new orientation. Note that the Earth's magnetic field is almost never vertical or horizontal so you can only measure differences between a known orientation an a new orientation in the same area.
Most people just use both a magnetometer and accelerometer to get direction and orientation.
arusr:
2) the angle (like the HMC5883L is not parallel to ground, but at 30 degrees). So if a car is standing on a steep hill, for example).
So, for the same reason than an accelerometer cannot sense changes in yaw in reference to gravity, I don't think a magnetometer can sense changes in tilt along the axis of the magnetic field. I.e., I think you will be able to sense tilt, but I think only in respect to (magnetic) north/south? That is, if you're pointing (magnetic) north or south and pitch up or down, you will be able to sense it, but not if you roll left or right. Alternatively, if you're pointing (magnetic) east or west, you'll be able to sense roll left or right, but not pitch up or down.
Since this is not a common task, you will need to write your own algorithm to integrate the data from the three axes and compute a solution for bearing and tilt.
Although if johnwasser is correct, the fact that the earth's magnetic field is not horizontal would mean that you can sense both kinds of tilt, but with an ambiguity and/or error. The ambiguity can probably be eliminated if you know your approximate latitude and longitude, but I'm not sure how to deal with error. Probably easiest to just use an accelerometer for tilt and the magnetometer for bearing.
My second problem: the raw values on the magnetometer do not seem consistent...when I return to a position, I don't get similar x,y,z raw values...
Can you give us an example of the values you are seeing, such as from some Serial.print()s?
EDIT: In another thread, jremington suggested calibrating your magnetometer:
With an accurately calibrated 3D magnetometer, it is possible to get magnetic bearing in most places on the Earth, as well as some limited tilt from horizontal. I have experimented with the idea and found that it doesn't always work very well (at least in my location).
For example, in my location the Earth's magnetic field is inclined about 66 degrees from the horizontal, so if you know that value you can calculate the tilt from horizontal using the Z component of the measured magnetic field. You get the bearing from the X and Y components. However, if you tilt the compass so that the magnetic field is roughly along the Z axis, the bearing cannot be determined. That is not a large tilt!
Be SURE to calibrate you magnetometer. Accelerometers should be calibrated in the same way, although they rarely need as much correction as magnetometers.
Be SURE to calibrate you magnetometer. Accelerometers should be calibrated in the same way, although they rarely need as much correction as magnetometers.
Thanks for that input. Is the magnetometer on this different from HMC5883L? Is the library more capable - does it calculate tilt for you? My problem right now is that 1)my HMC5883L is not giving consistent heading 2) even if it were, I don't know of a way of calculating tilt, so I was hoping for a magical library function.
The product page for the linked sensor has a great deal of information. Please check it out thoroughly.
Calculating a tilt angle is done with very simple trigonometry. The Pololu engineers provided a simple program that implements a tilt-compensated compass, which would be a great starting point.