Go Down

Topic: Arduino Duemilanove and Hmc5883l (Read 7223 times) previous topic - next topic


So you do get values from the board but they are not what you expect them to be. And you do not remove the board but just move or better turn it. Is that correct?

If yes, please post your sketch (you just attached the used library yet, not your sketch), as we have just seen pictures of a part of it. Don't forget to put code tags around it (selecting the code and hitting the # button above the smileys).


? cant take true values , when ? turn sensor . image values1 ; values changed when ? turn sensor . image values  ; radian and degrees dont changed with other values .they are 272 degrees and 90 degrees , 4.76 radian and 1.72 radian . image calculate radian degrees show us how calculate radian and degrees . what is my wrong ?


when ? move up or down sensor degrees changed . ? cant understand , ? use hmc for digital compass so which axis ? use ?


Please post the code in your IDE (that's what's on your pictures).

The code you have there is called a sketch in Arduino world. So go into the IDE, hit Ctrl-A, then Ctrl-C, then come to this web page and hit the reply button. In the form there you have a "#" button. Hit that first and then press Ctrl-V (for pasting). This way you post your sketch to the forum, that's what I asked for in my last post. This way we can see what your code (not the library you're using) does.


Code: [Select]
HMC5883L_Example.ino - Example sketch for integration with an HMC5883L Triple Axis Magnetometer.
Copyright (C) 2012 Love Electronics (loveelectronics.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

// 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.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
  compass = HMC5883L(); // Construct a new HMC5883 compass.
  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("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.

// 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_YAxis = 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' W, 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 = 0.0457;
  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(66);

// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
   Serial.print("   ");   
   Serial.print("   ");   
   Serial.print("   Scaled:");
   Serial.print("   ");   
   Serial.print("   ");   

   Serial.print("   Heading:");
   Serial.print(" Radians   ");
   Serial.println(" Degrees   ");
sorry for my bad english


The problem is, your x axis is always 0, so atan2(0, y) will always return PI/2 or -PI/2. The rest is correction function. Is it possible that you mounted your sensor so that the x axis is always 0 and the movement axis are y and z? If so, you have to change your code:

Code: [Select]
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2( scaled.ZAxis,scaled.YAxis);

It's possible that you have to swap the two axis again depending on your mount position.


already ? use z and y axis thank you so much . but ? wonder why x axis usually 0 ?


As far as I know you're using the sensor on a robot. Robots usually are rolling around on a flat surface (horizontally), which means in one plane. One axis isn't used and stays on the same value. A value of zero shows that your mount seems to be quite exact (assuming the sensor is working correctly).

Go Up