Question about the compass_tutorial01.c

Home Forums Forums Technical Support for BerryGPS and BerryGPS-IMU Question about the compass_tutorial01.c


Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
  • #17338


    I have a couple questions about the compass_tutorial01.c:

    1/ If I mount the BerryGPS-IMU board on top of the Pi4 board as in the attached photo, which way is “North” for the compass? Is it the red arrow direction?

    2/ I have compiled and run the code but the result heading is only moved somewhere between 300 to 360 degree. Is there anything else should I check? I know this code is just a basic without calibration or compensation for tilt but I would expect it to be somewhat close to 0 to 360 when I move the unit full circle.

    Thanks for any advises.

    Mark Williams

    Hi, rotate the red arrow 90 degrees anti-clockwise, this is north.

    When the BerryGPS-IMU is sitting on the Pi, you need to calibrate as the Pi interferes with compass.

    If you had the BerryGPS-IMU connected via a cable and some distance from the Pi, you may not need to calibrate the compass.

    Mark --



    I have followed the instruction how to calibrate the compass here

    After that I recompile the test 03 program with the result of Min/Max values. However when I did a test with my unit positioned in one direction and then 180 turn around, I got these heading result:

    First test results:

    heading 354.670          magRaw X 589   magRaw Y 571    MagRaw Z -220

    heading 356.553          magRaw X 506   magRaw Y 527    MagRaw Z -268

    heading 354.433          magRaw X 577   magRaw Y 538    MagRaw Z -238

    heading 355.347          magRaw X 504   magRaw Y 528    MagRaw Z -218

    heading 354.452          magRaw X 563   magRaw Y 521    MagRaw Z -213


    Second test result:


    heading 303.325          magRaw X -685  magRaw Y -543   MagRaw Z -282

    heading 302.978          magRaw X -554  magRaw Y -514   MagRaw Z -304

    heading 306.539          magRaw X -697  magRaw Y -540   MagRaw Z -285

    heading 302.722          magRaw X -633  magRaw Y -504   MagRaw Z -295

    heading 304.783          magRaw X -621  magRaw Y -472   MagRaw Z -321

    So I expected the result to be around 180 degrees apart but it was not. Please help me figure out what did I do wrong?


    Mark Williams

    when you did the calibration, did you spin the unit around a few times in all directions?

    Mark --


    Yes and I did until the max/min value no longer changing before I hit Ctrl-C

    Mark Williams

    can you please share the entire code in compass_tutorial03.c

    Mark --


    Here is the code exactly from your github, all I did was to change the “defines” and recompile.

    For some reason, this forum won’t let me upload C code or txt.


    • This reply was modified 4 months ago by thn_tpl.
    • This reply was modified 4 months ago by thn_tpl.


    We ordered two GPS-IMU boards and after seeing the strange compass readout with the first one, I took out the second one and plugged it in. Ran the same compass03 calibrate and then compass03 with new max/min value. Then the second board compass readout is very much as expected.

    Is there a possibility that the first unit has a bad compass?


    Mark Williams

    Hi…. I am so sorry you are having a trouble.  this would be the first I have heard of a compass not working.  Ill have to ask my colleagues to see if they have seen this before.


    In the mean time, can you do a test?
    Can you remove the current print statement from compass_tutorial03.c
    and put in the below print statement.

                    //Apply hard iron calibration
                    magRaw[0] -= (magXmin + magXmax) /2 ;
                    magRaw[1] -= (magYmin + magYmax) /2 ;
                    magRaw[2] -= (magZmin + magZmax) /2 ;
                    //Apply soft iron calibration
                    scaledMag[0]  = (float)(magRaw[0] - magXmin) / (magXmax - magXmin) * 2 - 1;
                    scaledMag[1]  = (float)(magRaw[1] - magYmin) / (magYmax - magYmin) * 2 - 1;
                    scaledMag[2]  = (float)(magRaw[2] - magZmin) / (magZmax - magZmin) * 2 - 1;

    recompile and then start the program again, rotate the compass 180 degrees and send the output to please also include you calibration values



    Mark --


    Here is the test results I sent to sales address

    ====Result from calibration
    #define magXmax -147
    #define magYmax 2372
    #define magZmax 1055
    #define magXmin -2983
    #define magYmin -553
    #define magZmin -2709

    Result after compiling with the above

    heading 356.282 Raw,-880,1210,-1297,Cal,685,301,-470,Rscaled,1.586742,-0.416068,0.189692
    heading 356.440 Raw,-876,1223,-1334,Cal,689,314,-507,Rscaled,1.589563,-0.407179,0.170032
    heading 356.765 Raw,-874,1229,-1188,Cal,691,320,-361,Rscaled,1.590973,-0.403077,0.247609
    heading 356.916 Raw,-876,1205,-1287,Cal,689,296,-460,Rscaled,1.589563,-0.419487,0.195005
    heading 356.349 Raw,-878,1197,-1300,Cal,687,288,-473,Rscaled,1.588152,-0.424957,0.188098
    heading 356.152 Raw,-866,1215,-1295,Cal,699,306,-468,Rscaled,1.596615,-0.412650,0.190755
    heading 356.641 Raw,-887,1204,-1266,Cal,678,295,-439,Rscaled,1.581805,-0.420171,0.206164
    heading 356.257 Raw,-888,1203,-1315,Cal,677,294,-488,Rscaled,1.581100,-0.420855,0.180128
    heading 356.227 Raw,-870,1208,-1280,Cal,695,299,-453,Rscaled,1.593794,-0.417436,0.198725
    heading 356.456 Raw,-868,1210,-1256,Cal,697,301,-429,Rscaled,1.595205,-0.416068,0.211477

    Turning the unit 180 degree ================

    heading 312.402 Raw,-2210,283,-1261,Cal,-645,-626,-434,Rscaled,0.648801,-1.049914,0.208820
    heading 312.847 Raw,-2231,288,-1288,Cal,-666,-621,-461,Rscaled,0.633991,-1.046496,0.194474
    heading 312.341 Raw,-2224,287,-1225,Cal,-659,-622,-398,Rscaled,0.638928,-1.047179,0.227949
    heading 312.522 Raw,-2226,281,-1288,Cal,-661,-628,-461,Rscaled,0.637518,-1.051282,0.194474
    heading 312.366 Raw,-2224,301,-1293,Cal,-659,-608,-466,Rscaled,0.638928,-1.037607,0.191817
    heading 312.756 Raw,-2210,296,-1262,Cal,-645,-613,-435,Rscaled,0.648801,-1.041026,0.208289
    heading 313.065 Raw,-2225,303,-1239,Cal,-660,-606,-412,Rscaled,0.638223,-1.036239,0.220510
    heading 312.762 Raw,-2217,287,-1270,Cal,-652,-622,-443,Rscaled,0.643865,-1.047179,0.204038
    heading 312.718 Raw,-2220,308,-1258,Cal,-655,-601,-431,Rscaled,0.641749,-1.032820,0.210414
    heading 312.988 Raw,-2224,299,-1257,Cal,-659,-610,-430,Rscaled,0.638928,-1.038974,0.210946



    It’s been a week and I haven’t heard anything from regarding the compass performance.

    Any helps to guide me through with getting a good compass reading would be very much appreciated.



    You may have missed Peter’s email. I just forwarded it again.

    We created the test program.
    Please download this file into the same directory as the current code you are working with.
    You can use a wget

    Compile with
    gcc -o selfTest-LIS3MDL selfTest-LIS3MDL.c

    Can you please run it and share the output


    Sorry for the delay, I was on travel.

    Here is the test result after compiled the code and ran:

    | Axis | ST min [gauss] | ST max [gauss] | Self test results |
    | X | 1.0 | 3.0 | 2.976940 |
    | Y | 1.0 | 3.0 | 3.130469 |
    | Z | 0.1 | 1.0 | 0.526260 |
    The test results in the last column need to be between the min and max values for the that axis


    And here is the test result for second board.

    | Axis | ST min [gauss] | ST max [gauss] | Self test results |
    | X | 1.0 | 3.0 | 3.191758 |
    | Y | 1.0 | 3.0 | 4.354581 |
    | Z | 0.1 | 1.0 | 0.570539 |
    The test results in the last column need to be between the min and max values for the that axis

    Mark Williams

    is the second board above the one with the problem?

    Mark --

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.

Blip, blop, bloop…