# Calculating a compass angle?

I am getting data from compass( hmC5883L) and trying to detect angle.

Well, i dont really care about offsets, i just want to check that if its flat on the table and i make a 90 degrees round, i will get numbers x-> 90+x

Instead for 90 degrees turn i get values like that : (starting from 13 degrees)

13->180, jump right to 0, 0->180, 180->13


code to manipulate data :

                float gScale = .92;  // Scale factor for +1.3Ga setting

float adjx = x - xOffset; //set to 0 for testing
float adjy = y - yOffset;//set to 0 for testing

zs = z * gScale;

heading += declination / 1000; // Declination for geo area

float angle = heading * 180/M_PI;


What am i doing wrong ?

• you have to remember that the compass is possibly drifting constantly, and you haven't shown us where you get the data in - which could be wrong too Commented Dec 1, 2015 at 18:15
• thanks, what do you mean where did i get the data in ? i am using Wire library with arduino and read the data as usual, i think the technical details are not relevant because i do get everything to work, only calculate wrong. if you look at it, it seems that my angles are going 0-180 always, and not 0-360, although i do zeroing it every 2pai Commented Dec 1, 2015 at 18:17
• Look for atan2() it gives [-pi,pi] Commented Dec 1, 2015 at 19:35
• PI and M_PI why are you using both? Commented Dec 1, 2015 at 19:56
• @MarkoBuršič he is using Atan2 Commented Dec 1, 2015 at 20:13

            float gScale = .92;  // Scale factor for +1.3Ga setting

float adjx = x - xOffset; //set to 0 for testing
float adjy = y - yOffset;//set to 0 for testing

zs = z * gScale;


• Unless you actually need the magnetic field strength in mG, you can omit gScale and the multiplication. Mathematically, the heading will be the same. Commented Dec 4, 2015 at 14:28