23 degrees is a lot, that doesn't seem a calibration error.Sometimes the x,y,z values don't show the exact amount of magnetic field. For better accuracy some calibration can be needed.Can you try another sketch to test it, and to show the x,y,z values. Perhaps the 'z' is zero due to a problem.The I2C-bus should be a 3.3V I2C-bus, did you know that ? You can connect it to the Arduino Uno, but only without extra pullup resistors.Is the something metal next to it ? a screwdriver or a computer ?If you still have problems, tell us which compass module you have, which Arduino board, and is possible a photo of your setup.
You have to calibrate magnetometers frequently, especially if the environment changes. My favorite procedure, which is a bit involved, is described here: http://sailboatinstruments.blogspot.com/2011/08/improved-magnetometer-calibration.htmlThere are many simpler approaches, some just involve subtracting the offset from each axis and estimating the gain errors (which are usually small), e.g. http://www.bot-thoughts.com/2011/04/quick-and-dirty-compass-calibration-in.html
The scaling and offset of the different axes of those HMC5883L's isn't all that good.The ones I have, will happily tell you that the angle between north and east is about 30 degrees. All of mine would have been up to 23 degrees off direction. Although like a broken clock which is right twice a day, there are a couple of directions where it coincidentally happens to be correct.You do need to calibrate them.Now, the question of whether the calibration would change so much in a few weeks, is a different question.I use one which was calibrated about 18 months ago, and it is still right ( after applying the corrections ) today. I have not personally experienced the calibration change which you have described.
jremington, did you hear before of suddenly 23 degrees calibration error ?
Quotejremington, did you hear before of suddenly 23 degrees calibration error ? I have personally experienced much larger errors than that -- actually magnetometers are capable of producing complete nonsense.The problem is that the atan2 function, which gives you the angle, assumes that the X and Y magnetometer axes are orthogonal and that their axes actually intersect at the origin. That is not the case if there is a magnetic disturbance nearby, or the calibration is changed by, for example, dropping the magnetometer on the floor.
his was gave me correct North but it is only when I run that program only.