QMC5883L compass module not working very well..

1,108 views
Skip to first unread message

Des Quilty

unread,
Oct 22, 2018, 2:30:57 PM10/22/18
to London Hackspace
Hi 

I have got one of these compass chips off ebay and tried this code on my arduino. 

GY-273 QMC5883L (HMC5883L Variant) 3-Axis Compass Magnetometer

I get some results but the heading seems to flip around quite a bit even when the chip is stationary, and if you rotate it it doesn't seem to reflect the actual direction. Certainly not good enough to help autosteer my remote control golf trolley project, which otherwise is coming along nicely.

Has anybody else had good experiences with these chip modules?

#include <Wire.h> //I2C Arduino Library

#define address 0x0D

  

void setup()

{

    //Initialize Serial and I2C communications

    Serial.begin(9600);

    Wire.begin();

    //Put the QMC5883L IC into the correct operating mode:

    Wire.beginTransmission(address); //Open communication with QMC5883L.

    Wire.write(0x09); //Select mode register.

    Wire.write(0x1D); //Continuous measurement mode.

    Wire.endTransmission();

}

 

void loop()

{

    int x,y,z;//Triple axis data.

    Wire.beginTransmission(address); //Tell the QMC5883L where to begin reading data.

    Wire.write(0x00); //Select X MSB register.

    Wire.endTransmission();

    //Read data from each axis, 2 registers per axis:

    Wire.requestFrom(address, 6);

    if(6<=Wire.available())

    {

        x = Wire.read()<<8;//X msb.

        x |= Wire.read();//X lsb.

        z = Wire.read()<<8;//Z msb.

        z |= Wire.read();//Z lsb.

        y = Wire.read()<<8;//Y msb.

        y |= Wire.read();//Y lsb.

    }

    float heading = atan2(y, x); //Calculate Heading in Radians.

    //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;

    float headingDegrees = heading * 180/M_PI; //Convert radians to degrees.

    Serial.print("Heading: ");

    Serial.println(headingDegrees);

    delay(1000);

} 


Alexander Baxevanis

unread,
Oct 22, 2018, 3:22:37 PM10/22/18
to London Hackspace
I'm not familiar with these particular compass modules but I know ones used in quadcopters and even in some mobile phones require calibration from time to time.

During calibration, you usually need to rotate the module by 360 degrees or on a specific pattern.

This blog shows the basic principles behind calibration: http://blog.bitify.co.uk/2013/11/connecting-and-calibrating-hmc5883l.html

Bear in mind that compass readings will also be affected by the presence of other metals around the sensor, so worth doing the calibration when the sensor is installed in its final place.

Hope this helps!

Alex

Nick Leaton

unread,
Oct 22, 2018, 3:36:35 PM10/22/18
to london-h...@googlegroups.com
Last time I played with a 3 axis compass I did this. 

Start recording the outputs and plot them on a 3d graph. You will find each axis is scaled differently and there is a different offset for zero. There is also no guarantee that each axis is orthogonal. On top of that the sensor itself can be tilted as can the actual magnetometers in the device. 

The shape then looks like a rugby ball tilted and shifted off to one side. 

To make a good compass you need to transform this shape into a sphere centred at the origin. 

https://paperless.bheeb.ch/download/Calibration.pdf is a paper that is comprehensive on the maths. 


Nick

--
You received this message because you are subscribed to the Google Groups "London Hackspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to london-hack-sp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Nick

Des Quilty

unread,
Dec 30, 2018, 6:10:48 AM12/30/18
to London Hackspace
Just reading around the whole subject I stumbled upon a youtube video somebody warning that the pins you solder onto the compass board can sometime become magnitised. I wonder if that was what was throwing off readings. I haven't had a chance to desolder the pins and solder wires straight to the board.

While thinking along those lines I also noted that quite a few people found this chip to be more reliable and also has inbuilt voltage regulation. 

LSM303D 3D Compass and Accelerometer Carrier with Voltage Regulator



I purchased one while I was in the US and have just wired it up, soldering the wires directly to the board without pins. Using the sample "Heading" code supplied with the library linked in the website the code is returning a simple heading in 360 degrees very nicely, which is just what I am looking for. Hopefully I can now integrate this into my code for my remote control golf trolley to self correct its course. 

مرتضی تمام

unread,
Aug 17, 2020, 12:19:09 PM8/17/20
to London Hackspace

        x = Wire.read()<<8;//X msb.

        x |= Wire.read();//X lsb.

        z = Wire.read()<<8;//Z msb.

        z |= Wire.read();//Z lsb.

        y = Wire.read()<<8;//Y msb.

        y |= Wire.read();//Y lsb.

 

 I think this code works for HMC5883l, not QMC5883l, and base on the order of registers in QMC5883l datasheet, maybe you should change the code like this:

        xlsb = Wire.read();

        xmsb= Wire.read();

        ylsb = Wire.read();

        ymsb = Wire.read();

        zlsb = Wire.read();

        zmsb = Wire.read();

        x = ((xMSB<<8) | xLSB); 

        y = ((yMSB<<8) | yLSB);

        z = ((zMSB<<8) | zLSB);

 
Reply all
Reply to author
Forward
0 new messages