Important: Please read the Qt Code of Conduct -

QT Compass/Magnetometer/Accelerometer

  • Ok... LG V50 running Android 9.

    Been working on an app that needs use of the compass.

    First issue was that the compass was always returning bad readings at a very high rate (around 100hz). I set up a timer to grab the readings every 300-500ms.. which helped stabilize the readings somewhat... and then added in code to force the returned values to a 360 degree range... no problems... -ish.

    Now... Why does the compass readings float so much. I'm getting values ranging +/- 30 degrees randomly when the phone is sitting still on my desk (away from other magnetic devices).

    I then moved to try using the magnetometer and accelerometer to get a good bearing... again, that works OK, except for the same issue... Still getting +/- 30 degrees, randomly.


  • Just one idea: Qt seems to have broken sensor related things in 5.12.4, and I wouldn't be surprised if they had broken the compass too. So if you use later Qt, I'd suggest trying version 5.12.3.

    (Showing code is also always a good idea, as everybody makes silly mistakes they don't see themselves.)

  • Compiling with 5.12.3 instead of 5.12.4 helps...
    Marking this as Solved.

  • Reopened this...
    My Compass code:

    mapCompass = new QCompass(this);
    connect(mapCompass,SIGNAL(readingChanged()), this, SLOT(compassReadingChanged()));

    And the slot that processes the output:

    void Functionsqt::compassReadingChanged() {
        QCompassReading *myReading = mapCompass->reading();
        double myAngle = myReading->azimuth();
        if( myAngle < 0.0 ) {
           myAngle = myAngle + 360.0;
        if( azArray.count() == 100 ) {
            float tAz = 0;
            for( int i = 0; i< 100; i++ ) {
                tAz = tAz+azArray[i];
            tAz = tAz / 100.0;
            myCurrentAz = QString::number( tAz, 'f', 1).toFloat();
            qDebug() << "new AZ: " << tAz;
        myCurrentCal = QString::number( myReading->calibrationLevel(), 'f', 1).toFloat();

    And the output from the qDebug():

    new AZ:  221.512
    new AZ:  220.078
    new AZ:  221.048
    new AZ:  227.811
    new AZ:  223.733
    new AZ:  196.193
    new AZ:  192.071
    new AZ:  213.953
    new AZ:  208.428
    new AZ:  208.155
    new AZ:  208.688
    new AZ:  208.456
    new AZ:  209.089
    new AZ:  210.445
    new AZ:  210.164
    new AZ:  210.134
    new AZ:  209.201
    new AZ:  208.077
    new AZ:  208.99
    new AZ:  209.358
    new AZ:  210.152
    new AZ:  209.229
    new AZ:  209.664
    new AZ:  221.767
    new AZ:  221.634

    Trying to 'smooth' the data via averaging... Problem is, with or without the averaging, the 'floating' of the data happens... and this is with the phone laying down, not moving. As you can see, this dataset ranges from 221 to 192...

    I've also tried various data rates (1hz, 10hz, 50hz... ) with the same results.

    I get the same results anywhere: plugged into my laptop, out in the yard, in the car, etc. Always 'floating' 30+ degrees.

    Any ideas why?


  • @BTSTOnline said in QT Compass/Magnetometer/Accelerometer:

    Any ideas why?

    Could it be the sensor sensitivity or precision and not Qt framework?
    Is it possible you run another application to validate you don't have such "floating" range, in order to rule out hardware or software?

  • Yes, I have tried other compass apps on my 'Droid and none exhibit this behavior.

Log in to reply