HMC5883L Lagging Problem!!

I’m working with this magnetometer but when I upload the code it works for a little then lag and when I puss the reset button it works for a little and then lag again…This is the code

// 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(57600);

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()
{
delay(80);
// 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 > 2PI)
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");
}

Please edit your post and surround the code with code tags ("</>" button).

The code tags are explained at number 7 here : http://forum.arduino.cc/index.php/topic,148850.0.html

Which library do you use (please give a link to that library) ?
There are probably example sketches with that library. Did you try an example sketch ? That should work without lagging.

How did you connect the sensor ? A 3.3V sensor may not be connected to the I2C of the Arduino Mega board.

I use arduino uno and this code is the library example

Hi,
Have you got pullup resistors on the I2C lines?

Tom.... :slight_smile:

I found libraries on Github that are like your example. But I don't know which one you have.

Get rid of that code, remove that library.

Use the newest Arduino 1.6.5, and go to the Library Manager : sketch / include library / manage libraries.
Install the "Adafruit HMC5883L Unified" by Adafruit.
Install the "Adafruit Unified Sensor".
In the menu there are new examples, open the HMC5883L "magsensor" sketch.

This is the section in the tutorial about the software : Wiring and Test | Adafruit HMC5883L Breakout - Triple-Axis Magnetometer Compass Sensor | Adafruit Learning System
You don't have to download it, since it is now in the Library Manager.

Adafruit maintains the code, and they have a forum, and they use github and they publish schematics and code and they make great tutorials and so on.
Others just show some code on the internet, and you never know if it is working or not.

no I dont use ressistors on I2C interface

Hi,

Tom.... :slight_smile:

I used adafruit library but against after a while it lags

That is almost impossible :confused:
Could it be a driver problem on the computer ? Could you test it on another computer with another usb cable ?

I use windows xp on my computer maybe this is the problem?

That is possible. I don't know what else it could be.

Instead of another computer, there are Android apps that can read the serial port of a Arduino Uno. You could try that.

Or make a test sketch, that does every things as you have now, but without writing to the serial port. Make it blink a led (every second or so). Check with a (stop)watch the led blinking, it should be very stable without lagging.

Thanks all of you for your replies.
I fixed this code control a servo.if I connect the arduino with a power simply it works excellent so windows xp may cause this problem.
Thanks for your help!!!