Pages: [1]   Go Down
Author Topic: Problem with HMC5883L  (Read 4660 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi, last week i received my new HCM5883L magnetometer breakout board , i connected it to arduino according to pin configuration that is SCL to pin 5, SDA to pin 4 with 5.6 k pullup resistor but i received just a raw data between 0 and 5 of x and y and the value of z is constant i-e -4096 ,and when i brought a magnet near to the sensor and move around, the value get high and then start fluctuate btw 0 and 5 .  i m not getting what happened to this , whether the board is faulty or there is some other reason.i attached the screen shot of the serial monitor with code. plz if some one knows then help me .thanks in advance   


Code:
/*
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");
}
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1018
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have also problems with the returned raw data. But all raw x,y,z values change with a magnet, or with the earth magnetic file (if I rotate the breakout board).

Is only your raw z-axis changing with a magnet ?
Can you add a link to the library and the breakout board ?
Can you paste the output of the serial monitor in your message ?
« Last Edit: July 22, 2012, 03:48:08 am by Krodal » Logged

0
Online Online
Shannon Member
****
Karma: 206
Posts: 12052
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Those pullups are going to +3.3V I hope....
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3007
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you solve this problem ?

I have ordered one of these gadgets and I am worried about ruining it.   

Do you need to have a resistor pair as a "voltage divider" when connecting a 3.3V  I2C device to a 5V Arduino,    or do you just use one pullup resistor connected to 3.3V,   which would make it a pulldown resistor, I guess...
Logged

Offline Offline
Edison Member
*
Karma: 8
Posts: 1341
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mine was difficult to calibrate.  I couldn't figure it out, so I just used X,Z instead of X,Y for 2D.  Seems Y was bad somehow, off by 50-100 degrees at times, combining X,Y.  Rotate the unit 90 degrees to use X,Z.
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I have a problem with HMC5883L magnetometer:
card: Arduino 2009
Soft: HMC5883L_Example.pde

The sketch is loaded into the card (arduino) but in the serial monitor I have this:

Constructing new HMC5883L
Setting scale to +/- 1.3 Ga
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
Setting measurement mode to continous.
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
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   -18176   -15872   322      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      

and so on.

What's happened ?

Thanx
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I have a problem with HMC5883L magnetometer:
card: Arduino 2009
Soft: HMC5883L_Example.pde

The sketch is loaded into the card (arduino) but in the serial monitor I have this:

Constructing new HMC5883L
Setting scale to +/- 1.3 Ga
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
Setting measurement mode to continous.
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
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   -18176   -15872   322      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      
Raw:   875   1795   883      Scaled:   805.00   1651.40   812.36      Heading:   1.16 Radians      66.63 Degrees      

and so on.

What's happened ?

Thanx


I get the same errors. Any ideas to get this fixed?
Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2071
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

..in HMC5883L.cpp change to:
Code:
char* HMC5883L::GetErrorText(int errorCode)
{
if(errorCode == ErrorCode_1_Num)
return ErrorCode_1;

return "Error not defined."; //or "No error" ??
}

Also such stuff like:
Code:
int HMC5883L::SetScale(float gauss)
{
uint8_t regValue = 0x00;
if(gauss == 0.88)
{
regValue = 0x00;
m_Scale = 0.73;
}
else if(gauss == 1.3)
{
regValue = 0x01;
m_Scale = 0.92;
}
else if(gauss == 1.9)
{
regValue = 0x02;
..
is of course not to recommend, I would rather use int "gauss" values instead of floats there..

This library does not calibrate the sensor, so when you move a magnet around the values changes and stay off (and you may get wrong heading). Google for HMC58X3 v04 - that driver includes calibration..
https://launchpad.net/hmc58x3
« Last Edit: January 28, 2013, 05:17:57 pm by pito » Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3007
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found three lots of code on the net for that device,   and they all had obvious mistakes.

One was so bad, it had obviously never been attempted to actually use.

In the end, I wrote my own.

As for the magnet-waving around, these devices are intended to detect the very weak magnetic
field of the earth.  Waving a magnet around near them is going to impose a much larger magnetic field,
possibly overload them,  and cause problems with overflows in the readings.   If you want to detect magnets,
you need to turn the sensitivity of the device down.

You need to calibrate the device  by setting the offset and scaling factor for each axial direction,  so that
the magnitude of the apparent field vector remains approximately constant no matter which way you
orient the device. 


Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2071
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got the sensor today  smiley and I am quite surprised that moving a magnet (ie in 3-5cm distance) around does change the values PERMANENTLY  smiley-confuse (when the sensor stays in a fixed position). Currently looking how to degauss the sensors (it shall include "automatic degaussing")..
« Last Edit: January 28, 2013, 05:27:28 pm by pito » Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3007
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to detect a magnet,  you are probably better off using a "hall effect" sensor.
Logged

Pages: [1]   Go Up
Jump to: