Beginner user compass question

Hi guys,

I'm new to Arduino and was interested in the compass sensor, which I have attached to my Arduino and am attempting a basic tutorial to get readings from. I started using the tutorial at Creative Electronic!: Arduino: simple compass with HMC5883L + Library to try and get some basic data for the compass read back through the virtual com port. I noticed that no matter how I moved the compass, it would always output the following line back to me:

Raw: 513 3598 27136 Scaled: 471.96 3310.16 24965.12 Heading: 49.51 Radians 2836.48 Degrees

I then unplugged the compass leads from the Arduino and it kept sending the same result. Restarting the Arduino without a compass also leads to the same line being repeatedly output.

I have tried a number of google searches around this but can't seem to find a similar issue, so I feel like I must be doing something really silly. Does anyone here have any advice on what I could do to get the compass read working?

cheers
Thomas

Post your code, wiring and maybe picture of the wiring. See the first message in the forum for directions on using code tags, links and images.

I don't know anything about that library, but it looks like Adafruit maintains a library and example for that sensor. You can generally count on their libraries and tutorials.

post your code, schmatics etc

Hi,

Here is the code I'm using (library is in the instructions link).

// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;

// Out setup routine, here we will configure the microcontroller and compass.
void setup()
{
  // Initialize the serial port.
  Serial.begin(9600);

  Serial.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
    
  Serial.println("Setting scale to +/- 1.3 Ga");
  error = compass.setScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.getErrorText(error));
  
  Serial.println("Setting measurement mode to continous.");
  error = compass.setMeasurementMode(MEASUREMENT_CONTINUOUS); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.getErrorText(error));
}

// Our main program loop.
void loop()
{
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.readRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.readScaledAxis();
  
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: -2��37' which is -2.617 Degrees, or (which we need) -0.0456752665 radians, I will use -0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = +54.36;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  Output(raw, scaled, heading, headingDegrees);

  // Normally we would delay the application by 66ms to allow the loop
  // to run at 15Hz (default bandwidth for the HMC5883L).
  // However since we have a long serial out (104ms at 9600) we will let
  // it run at its natural speed.
  delay(1000);//of course it can be delayed longer.
}

// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
{
   Serial.print("Raw:\t");
   Serial.print(raw.XAxis);
   Serial.print("   ");   
   Serial.print(raw.YAxis);
   Serial.print("   ");   
   Serial.print(raw.ZAxis);
   Serial.print("   \tScaled:\t");
   
   Serial.print(scaled.XAxis);
   Serial.print("   ");   
   Serial.print(scaled.YAxis);
   Serial.print("   ");   
   Serial.print(scaled.ZAxis);

   Serial.print("   \tHeading:\t");
   Serial.print(heading);
   Serial.print(" Radians   \t");
   Serial.print(headingDegrees);
   Serial.println(" Degrees   \t");
}

I have connected the compass' GND to GND, VCC to 5v, SDA to A4 and SCA to A5 - not entirely sure how to do a diagram as I am new to this type of thing.

Thanks again for your help

Thomas

Please post a link to the compass module you are using.

Note that the HMC5883L is a 3.3V device and unless the module has a 5V regulator and level shifting circuitry, it will be destroyed by connection to 5V.

When making connections to a new I2C device, it is always a good idea to run the I2CScanner program before any other, to see if the device is recognized and the I2C address is correct.