Problems with HMC5883L Magnetometer/Compass

Hello Arduino community, I am currently working on an Arduino robotic car as part of a project for the County Science Fair. My partner and I in this project have already advanced to the State Science Fair. It consists of Line Following, Obstacle Avoiding, Bluetooth Controlled. We have planned on adding a GPS guided aspect to our project. We have successfully found our coordinates with the GPS module but also need to calibrate our HMC5883L magnetometer/compass. We hope maybe you have some experience with this compass. Our current issue is that when we are calibrating the compass on the Arduino, we get no data coming from the Serial Monitor. The baud rate is at 9600 on the Monitor, and the return is Newline. The calibration sketch is from the HMC5883L library --> Examples. The VCC and GND pins of the compass are connected to VCC and GND of the Arduino, and the SCL and SDA pins are connected to 20 and 21 (on the Arduino Mega) or connected to pins A4 and A5 (on the Arduino Uno). I have even reversed the SCL and SDA pins to no avail. It would be wonderful if you could maybe give me and my partner some tips if you have any about the HMC5883L compass which would help us achieve success. Our code is the calibration sketch for the HMC5883L.

Daniel and Diana

HMC5883L_calibrate.ino (1.5 KB)

You probably have the QMC5883 semi-clone, often incorrectly labeled by sellers. You need a different library for that, which google will find.

If not, follow the "How to use this forum" guidelines and post the code.

Thank you for the explanation, and I apologize for my shaky forum skills, I have just started using this forum. How can I tell what the difference between a QMC5883 and HMC5883L?

This is the code I'm using for whom it concerns:

/*
  Calibrate HMC5883L. Output for HMC5883L_calibrate_processing.pde
  Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-magnetometr-hmc5883l.html
  GIT: https://github.com/jarzebski/Arduino-HMC5883L
  Web: http://www.jarzebski.pl
  (c) 2014 by Korneliusz Jarzebski
*/

#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;

int minX = 0;
int maxX = 0;
int minY = 0;
int maxY = 0;
int offX = 0;
int offY = 0;

void setup()
{
  Serial.begin(9600);

  // Initialize Initialize HMC5883L
  while (!compass.begin())
  {
    delay(500);
  }

  // Set measurement range
  compass.setRange(HMC5883L_RANGE_1_3GA);

  // Set measurement mode
  compass.setMeasurementMode(HMC5883L_CONTINOUS);

  // Set data rate
  compass.setDataRate(HMC5883L_DATARATE_30HZ);

  // Set number of samples averaged
  compass.setSamples(HMC5883L_SAMPLES_8);
}

void loop()
{
  Vector mag = compass.readRaw();

  // Determine Min / Max values
  if (mag.XAxis < minX) minX = mag.XAxis;
  if (mag.XAxis > maxX) maxX = mag.XAxis;
  if (mag.YAxis < minY) minY = mag.YAxis;
  if (mag.YAxis > maxY) maxY = mag.YAxis;

  // Calculate offsets
  offX = (maxX + minX)/2;
  offY = (maxY + minY)/2;

  Serial.print(mag.XAxis);
  Serial.print(":");
  Serial.print(mag.YAxis);
  Serial.print(":");
  Serial.print(minX);
  Serial.print(":");
  Serial.print(maxX);
  Serial.print(":");
  Serial.print(minY);
  Serial.print(":");
  Serial.print(maxY);
  Serial.print(":");
  Serial.print(offX);
  Serial.print(":");
  Serial.print(offY);
  Serial.print("\n");
}

The markings on the chips are different, the I2C addresses are different, etc.

Don't be afraid to use your favorite search engine, with maybe "fake HMC5883" as a search phrase.

Thank you for your help, I have figured out that my compass is indeed a QMC5883, and I am receiving these readings:

x: -928 y: 312 z: -1542 a: 161
x: -928 y: 307 z: -1542 a: 161
x: -926 y: 295 z: -1547 a: 162
x: -921 y: 307 z: -1542 a: 161
x: -935 y: 307 z: -1547 a: 161
x: -907 y: 312 z: -1561 a: 161
x: -918 y: 307 z: -1542 a: 161
x: -907 y: 305 z: -1540 a: 161
x: -923 y: 327 z: -1556 a: 160
x: -916 y: 295 z: -1561 a: 162
x: -907 y: 307 z: -1545 a: 161
x: -886 y: 317 z: -1563 a: 160
x: -913 y: 317 z: -1535 a: 160
x: -916 y: 307 z: -1547 a: 161

Great!

To calibrate it, follow this excellent tutorial: Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

Thank you for all of your help, I greatly appreciate it. I will check out the link you sent me.

Thanks, Daniel and Diana

More step by step info here, for a different magnetometer (not that the type matters much).