Go Down

Topic: Why HMC5883L error? (Read 1 time) previous topic - next topic

warren631

I use the standard "HMC5883L_Example.INO" and it seems to work but I still get the error:
Quote
Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1.2

I have tried all those values but I always get this error and changing these numbers doesn't seem to make any difference to the readings.
Why do I get this error?

MarkT

Where did you get the code from - the one I found looks OK, perhaps you have a different version?

setScale() seems to do the obvious thing - look at what your version is doing?
[ I won't respond to messages, use the forum please ]

warren631

I downloaded the code and library from Loveelectronics (https://www.loveelectronics.co.uk/Tutorials/8/hmc5883l-tutorial-and-arduino-library). I just changed from .pde to .ino. And I also deleted the first character because it was giving a compile error.  Here it is:
Code: [Select]
/*
HMC5883L_Example.pde - Example sketch for integration with an HMC5883L triple axis magnetomerwe.
Copyright (C) 2011 Love Electronics (loveelectronics.co.uk)

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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
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.begin(9600);

 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(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' 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("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");
}

MarkT

Incidentally why are you allocating an instance dynamically rather than statically?

You can just do:

Code: [Select]

HMC5883L compass () ;


and lose the line:
Code: [Select]
  compass = HMC5883L(); // Construct a new HMC5883 compass.

from setup()

As for curing the problem you are probably going to debug the library to see what's happening.
[ I won't respond to messages, use the forum please ]

What Module are you using ?

Parallax,  Sparkfun, Love,  or other ?

Using the Parallax with the MEGA 256 mine did not like the 3.3vdc supply,  but worked fine with the 5V supply. 

Make sure your device has a 5v > 3.3 V regulator if you try 5VDC as the chip it's self can only handle 3.3 V

Hexadec

#5
Nov 20, 2012, 09:51 pm Last Edit: Nov 20, 2012, 09:54 pm by Hexadec Reason: 1
For anyone having problems compiling the example:


Open the HMC5883L.cpp - Class file and change :

Code: [Select]
void HMC5883L::Write(int address, int data)
{
 Wire.beginTransmission(HMC5883L_Address);
 Wire.send(address);
 Wire.send(data);
 Wire.endTransmission();

To

void HMC5883L::Write(int address, int data)
{
 Wire.beginTransmission(HMC5883L_Address);
 Wire.write(address);
 Wire.write(data);
 Wire.endTransmission();
}
}



Then change:

Code: [Select]
if(Wire.available() == length)
 {
 for(uint8_t i = 0; i < length; i++)
 {
 buffer[i] = Wire.receive();
 }
 }
 Wire.endTransmission();

To

if(Wire.available() == length)
 {
 for(uint8_t i = 0; i < length; i++)
 {
 buffer[i] = Wire.read();
 }
 }
 Wire.endTransmission();



It will compile then... :smiley-mr-green:

I don't know if it works...but it compiles.... :P
Sponge.
I'm allowed to have it 'cos it's not sharp.

That change is already incorporated into the newest version of the library.

The problem is not that he cannot compile,  the problem that  the error message regarding "Scale" claims that the entered Gauss value is  invalid,  even though it is one of the listed values.

I also have this problem,  but just  ignore it,   


Hexadec

That's why I headed my post with:

Quote
For anyone having problems compiling the example:


I downloaded the example from the link above and it isn't updated.  ;)

I'll have a look at the library when I get some time this weekend.
Sponge.
I'm allowed to have it 'cos it's not sharp.

Go Up