ArduMag – MEMS magnetometer calibration made easy

ardumag

We needed a way to calibrate an external 3-axis MEMS magnetometer (HMC5883L) module (connected to an Arduino) with an Android device…and so I did develop ‘ArduMag‘, a portable Android compass calibration app.

ArduMag generally works with any compass/magnetometer module (e.g. HMC5883L), any microcontroller (e.g. Arduino) and any Bluetooth module (e.g. HC-06).

You send the raw magnetometer data via Bluetooth (or in a file) and the app will calculate and send back calibration parameters  (using Elliposid fitting method) that you can apply to correct your raw magnetometer measurements from soft and hard iron errors.

How to use it?

1. Download ‘ArduMag‘ on Google Play and run it.

2. Pair your serial Bluetooth module with your Android device. In ‘ArduMag’, press ‘Menu->Bluetooth‘ to connect to your bluetooth module (once connected, ‘ArduMag’ will send the initial sequence ‘{ardumag}’ to your microcontroller)

3. Turn the compass module around all axis, and send the raw compas data (x y z) to the ArduMag app (via Bluetooth or in a file) – example:
10   -45    94
11   -44    93
11   -33    92
etc.
NOTE: xyz-fields must be separated by TAB-characters (‘\t’)

In ArduMag, you’ll see the number of points received and a 3D visualization of your raw measurements. When sufficient measurements have been transmitted, you’ll see an Ellipsoid.

shot1

4. Press ‘Calibrate‘ to perform the Ellipsoid fitting and compute the calibration parameters. ‘ArduMag’ will show the calibrated measurements – this should be a sphere now:
shot2

Additionally, ‘ArduMag’ will send back the computed calibration parameters (via Bluetooth) – example:
{ardumagB00|287.75}
{ardumagB10|-87.3165}
{ardumagB20|7.465}
{ardumagA00|1.123}
{ardumagA01|0.075}
{ardumagA02|0.075}
{ardumagA10|0.123}
{ardumagA11|1.075}
{ardumagA12|0.075}
{ardumagA20|0.123}
{ardumagA21|0.075}
{ardumagA22|1.075}

5. Use these calibration parameters to correct your future raw magnetometer measurements like this:

double x = comRaw.x – B00;
double y = comRaw.y – B10;
double z = comRaw.z – B20;

comCalibrated.x = (A00 * x + A01 * y + A02 * z) ;
comCalibrated.y = (A10 * x + A11 * y + A12 * z) ;
comCalibrated.z = (A20 * x + A21 * y + A22 * z) ;

3 thoughts on “ArduMag – MEMS magnetometer calibration made easy”

  1. Ich bekomme mit folgenden Daten einer parserfehler : 1

    53.000000 700.000000 -77.000000
    54.000000 699.000000 -77.000000
    55.000000 698.000000 -80.000000
    55.000000 698.000000 -80.000000
    53.000000 696.000000 -77.000000
    55.000000 699.000000 -77.000000
    56.000000 699.000000 -78.000000
    55.000000 700.000000 -80.000000
    54.000000 699.000000 -81.000000
    54.000000 697.000000 -80.000000
    57.000000 698.000000 -79.000000
    56.000000 699.000000 -79.000000
    58.000000 697.000000 -77.000000
    56.000000 696.000000 -80.000000
    54.000000 699.000000 -76.000000
    57.000000 699.000000 -80.000000
    54.000000 698.000000 -82.000000
    53.000000 698.000000 -78.000000
    54.000000 696.000000 -78.000000
    53.000000 697.000000 -78.000000
    55.000000 700.000000 -81.000000
    52.000000 697.000000 -79.000000
    55.000000 699.000000 -78.000000
    56.000000 697.000000 -80.000000
    53.000000 699.000000 -78.000000
    53.000000 700.000000 -80.000000
    55.000000 697.000000 -79.000000
    55.000000 698.000000 -77.000000
    54.000000 699.000000 -79.000000
    55.000000 698.000000 -79.000000
    56.000000 697.000000 -77.000000
    53.000000 698.000000 -79.000000
    53.000000 697.000000 -82.000000
    54.000000 699.000000 -79.000000
    57.000000 695.000000 -78.000000
    55.000000 698.000000 -81.000000
    54.000000 697.000000 -77.000000
    56.000000 699.000000 -79.000000
    55.000000 700.000000 -80.000000
    55.000000 699.000000 -79.000000
    57.000000 697.000000 -78.000000
    53.000000 699.000000 -80.000000
    53.000000 699.000000 -83.000000
    55.000000 696.000000 -77.000000
    55.000000 698.000000 -80.000000
    54.000000 697.000000 -76.000000
    54.000000 697.000000 -79.000000
    54.000000 699.000000 -79.000000
    56.000000 698.000000 -80.000000
    55.000000 699.000000 -80.000000
    56.000000 698.000000 -78.000000
    53.000000 698.000000 -81.000000
    54.000000 697.000000 -80.000000
    57.000000 700.000000 -78.000000
    54.000000 697.000000 -80.000000
    54.000000 698.000000 -80.000000
    56.000000 698.000000 -80.000000
    55.000000 700.000000 -79.000000
    50.000000 697.000000 -79.000000
    53.000000 697.000000 -79.000000
    54.000000 699.000000 -77.000000
    58.000000 699.000000 -80.000000
    56.000000 699.000000 -78.000000
    57.000000 700.000000 -77.000000
    56.000000 699.000000 -79.000000
    55.000000 698.000000 -78.000000
    55.000000 698.000000 -79.000000
    54.000000 695.000000 -79.000000
    58.000000 698.000000 -79.000000
    56.000000 700.000000 -77.000000
    57.000000 699.000000 -80.000000
    56.000000 698.000000 -78.000000
    56.000000 698.000000 -81.000000
    57.000000 698.000000 -81.000000
    61.000000 700.000000 -78.000000
    104.000000 701.000000 -86.000000
    143.000000 697.000000 -94.000000
    194.000000 694.000000 -93.000000
    247.000000 691.000000 -88.000000
    306.000000 675.000000 -85.000000
    345.000000 660.000000 -89.000000
    374.000000 646.000000 -89.000000
    392.000000 629.000000 -92.000000
    423.000000 607.000000 -89.000000
    433.000000 591.000000 -96.000000
    448.000000 571.000000 -98.000000
    468.000000 542.000000 -96.000000
    483.000000 512.000000 -101.000000
    500.000000 463.000000 -91.000000
    510.000000 427.000000 -95.000000
    509.000000 400.000000 -97.000000
    511.000000 366.000000 -97.000000
    503.000000 321.000000 -95.000000
    498.000000 288.000000 -100.000000
    484.000000 241.000000 -98.000000
    469.000000 204.000000 -101.000000
    456.000000 163.000000 -93.000000
    428.000000 102.000000 -88.000000
    405.000000 60.000000 -83.000000
    361.000000 -3.000000 -82.000000
    285.000000 -94.000000 -73.000000
    254.000000 -126.000000 -75.000000
    210.000000 -171.000000 -65.000000
    169.000000 -215.000000 -47.000000
    162.000000 -216.000000 -49.000000
    126.000000 -239.000000 -54.000000
    136.000000 -237.000000 -48.000000
    126.000000 -246.000000 -36.000000
    92.000000 -272.000000 -35.000000
    69.000000 -288.000000 -27.000000
    59.000000 -292.000000 -26.000000
    47.000000 -301.000000 -30.000000
    -4.000000 -325.000000 -40.000000
    -53.000000 -345.000000 -51.000000
    -73.000000 -357.000000 -55.000000
    -113.000000 -368.000000 -63.000000
    -136.000000 -370.000000 -70.000000
    -184.000000 -378.000000 -82.000000
    -194.000000 -383.000000 -83.000000
    -232.000000 -399.000000 -70.000000
    -278.000000 -423.000000 -51.000000
    -334.000000 -437.000000 -25.000000
    -382.000000 -445.000000 -12.000000
    -413.000000 -443.000000 -11.000000
    -461.000000 -448.000000 8.000000
    -499.000000 -443.000000 15.000000
    -522.000000 -440.000000 17.000000
    -564.000000 -427.000000 15.000000
    -588.000000 -421.000000 20.000000
    -633.000000 -402.000000 16.000000
    -679.000000 -369.000000 8.000000
    -700.000000 -349.000000 3.000000
    -716.000000 -333.000000 2.000000
    -744.000000 -294.000000 -4.000000
    -759.000000 -272.000000 0.000000
    -768.000000 -249.000000 8.000000
    -788.000000 -196.000000 24.000000
    -793.000000 -146.000000 14.000000
    -791.000000 -88.000000 6.000000
    -790.000000 -55.000000 8.000000
    -783.000000 -26.000000 2.000000
    -766.000000 47.000000 -5.000000
    -746.000000 95.000000 -9.000000
    -735.000000 123.000000 -5.000000
    -717.000000 165.000000 4.000000
    -700.000000 198.000000 6.000000
    -675.000000 239.000000 2.000000
    -650.000000 275.000000 0.000000
    -627.000000 304.000000 -6.000000
    -605.000000 333.000000 -8.000000
    -583.000000 356.000000 -20.000000
    -554.000000 386.000000 -20.000000
    -544.000000 394.000000 -26.000000
    -529.000000 408.000000 -25.000000
    -516.000000 422.000000 -28.000000
    -510.000000 427.000000 -29.000000
    -513.000000 425.000000 -22.000000
    -517.000000 424.000000 -24.000000
    -505.000000 435.000000 -21.000000
    -502.000000 441.000000 -22.000000
    -498.000000 444.000000 -23.000000
    -491.000000 448.000000 -27.000000
    -484.000000 455.000000 -25.000000
    -471.000000 467.000000 -26.000000
    -446.000000 489.000000 -27.000000
    -438.000000 494.000000 -28.000000
    -410.000000 515.000000 -30.000000
    -393.000000 525.000000 -36.000000
    -375.000000 537.000000 -34.000000
    -331.000000 565.000000 -40.000000
    -297.000000 586.000000 -46.000000
    -264.000000 599.000000 -50.000000
    -256.000000 603.000000 -49.000000
    -263.000000 600.000000 -49.000000
    -238.000000 614.000000 -53.000000
    -241.000000 611.000000 -55.000000
    -222.000000 622.000000 -56.000000
    -154.000000 646.000000 -65.000000
    -131.000000 653.000000 -71.000000
    -109.000000 660.000000 -72.000000
    -52.000000 678.000000 -83.000000
    -5.000000 684.000000 -84.000000
    42.000000 692.000000 -85.000000
    101.000000 695.000000 -92.000000
    92.000000 698.000000 -85.000000
    67.000000 702.000000 -77.000000
    41.000000 699.000000 -75.000000
    31.000000 698.000000 -78.000000
    40.000000 699.000000 -70.000000
    -2.000000 712.000000 -21.000000
    14.000000 709.000000 -35.000000
    -2.000000 705.000000 -35.000000
    -46.000000 691.000000 -41.000000
    -145.000000 657.000000 -50.000000
    -322.000000 574.000000 -25.000000
    -397.000000 526.000000 -11.000000
    -447.000000 486.000000 -2.000000
    -510.000000 433.000000 18.000000
    -560.000000 382.000000 27.000000
    -596.000000 343.000000 25.000000
    -686.000000 216.000000 22.000000
    -724.000000 147.000000 30.000000
    -770.000000 24.000000 41.000000
    -791.000000 -104.000000 36.000000
    -785.000000 -176.000000 35.000000
    -751.000000 -282.000000 42.000000
    -655.000000 -384.000000 11.000000
    -593.000000 -409.000000 -21.000000
    -560.000000 -410.000000 -49.000000
    -551.000000 -412.000000 -57.000000
    -534.000000 -415.000000 -53.000000
    -455.000000 -428.000000 -62.000000
    -414.000000 -430.000000 -66.000000
    -444.000000 -434.000000 -45.000000
    -436.000000 -431.000000 -51.000000
    -393.000000 -435.000000 -47.000000
    -330.000000 -423.000000 -64.000000
    -279.000000 -411.000000 -71.000000
    -124.000000 -364.000000 -73.000000
    -96.000000 -355.000000 -71.000000
    -63.000000 -334.000000 -76.000000
    32.000000 -284.000000 -80.000000
    101.000000 -242.000000 -77.000000
    227.000000 -131.000000 -82.000000
    320.000000 -25.000000 -89.000000
    387.000000 75.000000 -96.000000
    454.000000 255.000000 -109.000000
    471.000000 380.000000 -112.000000
    432.000000 517.000000 -114.000000
    407.000000 557.000000 -115.000000
    385.000000 582.000000 -117.000000
    329.000000 637.000000 -107.000000
    273.000000 670.000000 -90.000000
    220.000000 695.000000 -76.000000
    128.000000 719.000000 -42.000000
    73.000000 727.000000 -3.000000
    74.000000 734.000000 50.000000
    134.000000 733.000000 141.000000
    214.000000 720.000000 172.000000
    262.000000 687.000000 207.000000
    278.000000 636.000000 255.000000
    292.000000 605.000000 278.000000
    300.000000 560.000000 297.000000
    312.000000 520.000000 306.000000
    301.000000 499.000000 319.000000
    294.000000 491.000000 323.000000
    270.000000 465.000000 338.000000
    264.000000 455.000000 339.000000
    273.000000 440.000000 343.000000
    199.000000 395.000000 370.000000
    141.000000 348.000000 386.000000
    147.000000 322.000000 388.000000
    205.000000 353.000000 368.000000
    233.000000 357.000000 360.000000
    200.000000 343.000000 373.000000
    195.000000 342.000000 375.000000
    205.000000 349.000000 369.000000
    223.000000 361.000000 363.000000
    211.000000 364.000000 365.000000
    200.000000 355.000000 373.000000
    165.000000 277.000000 382.000000
    133.000000 202.000000 386.000000
    139.000000 219.000000 385.000000
    151.000000 241.000000 383.000000
    160.000000 237.000000 380.000000
    160.000000 229.000000 379.000000
    122.000000 197.000000 387.000000
    103.000000 188.000000 389.000000
    105.000000 187.000000 391.000000
    144.000000 187.000000 378.000000
    193.000000 203.000000 368.000000
    209.000000 162.000000 354.000000
    183.000000 39.000000 328.000000
    180.000000 10.000000 313.000000
    162.000000 -53.000000 286.000000
    145.000000 -113.000000 252.000000
    149.000000 -136.000000 226.000000
    138.000000 -185.000000 175.000000
    124.000000 -215.000000 140.000000
    137.000000 -220.000000 107.000000
    93.000000 -261.000000 65.000000
    88.000000 -269.000000 42.000000
    60.000000 -285.000000 16.000000
    -21.000000 -330.000000 -15.000000
    -62.000000 -348.000000 -39.000000
    -104.000000 -355.000000 -74.000000
    -113.000000 -360.000000 -79.000000
    -172.000000 -364.000000 -119.000000
    -174.000000 -352.000000 -135.000000
    -162.000000 -339.000000 -147.000000
    -165.000000 -301.000000 -189.000000
    -161.000000 -268.000000 -218.000000
    -151.000000 -198.000000 -257.000000
    -122.000000 -161.000000 -273.000000
    -95.000000 -83.000000 -294.000000
    -78.000000 -26.000000 -311.000000
    -66.000000 34.000000 -316.000000
    -79.000000 78.000000 -327.000000
    -96.000000 117.000000 -328.000000
    -110.000000 172.000000 -331.000000
    -110.000000 225.000000 -325.000000
    -103.000000 267.000000 -321.000000
    -97.000000 336.000000 -306.000000
    -70.000000 393.000000 -288.000000
    -57.000000 484.000000 -249.000000
    -50.000000 521.000000 -228.000000
    -17.000000 578.000000 -193.000000
    -19.000000 602.000000 -174.000000
    1.000000 646.000000 -136.000000
    37.000000 667.000000 -121.000000
    55.000000 681.000000 -100.000000
    67.000000 690.000000 -89.000000
    71.000000 716.000000 -44.000000
    66.000000 721.000000 -37.000000
    63.000000 713.000000 -51.000000
    57.000000 708.000000 -65.000000
    50.000000 691.000000 -89.000000
    34.000000 663.000000 -125.000000
    -40.000000 583.000000 -186.000000
    -90.000000 471.000000 -255.000000
    -134.000000 332.000000 -303.000000
    -158.000000 158.000000 -330.000000
    -185.000000 82.000000 -330.000000
    -178.000000 -38.000000 -316.000000
    -165.000000 -93.000000 -302.000000
    -174.000000 -154.000000 -279.000000
    -146.000000 -271.000000 -204.000000
    -149.000000 -326.000000 -149.000000
    -183.000000 -376.000000 -89.000000
    -149.000000 -385.000000 -25.000000
    -137.000000 -386.000000 23.000000
    -177.000000 -398.000000 -16.000000
    -210.000000 -379.000000 -99.000000
    -231.000000 -342.000000 -159.000000
    -237.000000 -292.000000 -210.000000
    -183.000000 -260.000000 -220.000000
    -105.000000 -242.000000 -215.000000
    47.000000 -220.000000 -159.000000
    116.000000 -176.000000 -151.000000
    103.000000 -121.000000 -203.000000
    55.000000 -102.000000 -244.000000
    93.000000 -58.000000 -248.000000
    168.000000 -64.000000 -206.000000
    204.000000 -86.000000 -161.000000
    201.000000 -143.000000 -101.000000
    194.000000 -174.000000 -58.000000
    153.000000 -220.000000 -22.000000
    133.000000 -245.000000 64.000000
    127.000000 -230.000000 123.000000
    95.000000 -226.000000 177.000000
    82.000000 -199.000000 221.000000
    67.000000 -158.000000 268.000000
    12.000000 -164.000000 290.000000
    13.000000 -128.000000 313.000000
    15.000000 -104.000000 327.000000
    14.000000 -32.000000 362.000000
    -10.000000 10.000000 382.000000
    -39.000000 45.000000 393.000000
    -60.000000 65.000000 403.000000
    -63.000000 95.000000 407.000000
    -34.000000 145.000000 409.000000
    -55.000000 165.000000 410.000000
    -50.000000 216.000000 412.000000
    -9.000000 296.000000 405.000000
    -44.000000 304.000000 404.000000
    -109.000000 287.000000 405.000000
    -242.000000 190.000000 402.000000
    -361.000000 142.000000 379.000000
    -455.000000 126.000000 351.000000
    -501.000000 195.000000 311.000000
    -451.000000 318.000000 290.000000
    -372.000000 434.000000 261.000000
    -273.000000 563.000000 199.000000
    -209.000000 619.000000 163.000000
    -155.000000 661.000000 116.000000
    -142.000000 672.000000 99.000000
    -134.000000 674.000000 110.000000
    -123.000000 680.000000 111.000000
    -116.000000 684.000000 106.000000
    -117.000000 683.000000 106.000000
    -119.000000 685.000000 102.000000
    -100.000000 692.000000 99.000000
    -93.000000 696.000000 101.000000
    -88.000000 696.000000 106.000000
    -93.000000 697.000000 106.000000
    -136.000000 680.000000 78.000000
    -175.000000 666.000000 62.000000
    -218.000000 642.000000 58.000000
    -268.000000 617.000000 60.000000
    -311.000000 590.000000 69.000000
    -333.000000 572.000000 78.000000
    -358.000000 554.000000 100.000000
    -360.000000 552.000000 100.000000
    -378.000000 545.000000 93.000000
    -393.000000 535.000000 93.000000
    -403.000000 526.000000 90.000000
    -447.000000 498.000000 82.000000
    -463.000000 479.000000 79.000000
    -479.000000 469.000000 65.000000
    -504.000000 446.000000 51.000000
    -516.000000 435.000000 40.000000
    -526.000000 430.000000 31.000000
    -535.000000 417.000000 16.000000
    -540.000000 413.000000 2.000000
    -541.000000 403.000000 -12.000000
    -546.000000 399.000000 -16.000000
    -538.000000 408.000000 -14.000000
    -538.000000 410.000000 -15.000000
    -536.000000 414.000000 -19.000000
    -534.000000 416.000000 -17.000000
    -522.000000 428.000000 -22.000000
    -504.000000 445.000000 -22.000000
    -479.000000 468.000000 -19.000000
    -464.000000 477.000000 -22.000000
    -446.000000 492.000000 -21.000000
    -422.000000 510.000000 -23.000000
    -408.000000 523.000000 -24.000000
    -401.000000 527.000000 -23.000000
    -385.000000 539.000000 -27.000000
    -380.000000 543.000000 -24.000000
    -384.000000 538.000000 -25.000000
    -380.000000 543.000000 -27.000000
    -354.000000 559.000000 -29.000000
    -318.000000 579.000000 -32.000000
    -296.000000 594.000000 -35.000000
    -277.000000 600.000000 -39.000000
    -273.000000 605.000000 -41.000000
    -271.000000 606.000000 -42.000000
    -234.000000 621.000000 -46.000000
    -226.000000 626.000000 -45.000000
    -242.000000 622.000000 -46.000000
    -240.000000 620.000000 -45.000000
    -238.000000 621.000000 -44.000000
    -234.000000 622.000000 -45.000000
    -238.000000 624.000000 -45.000000
    -236.000000 621.000000 -44.000000
    -244.000000 619.000000 -47.000000
    -251.000000 618.000000 -42.000000
    -292.000000 593.000000 -38.000000
    -326.000000 573.000000 -36.000000
    -342.000000 568.000000 -35.000000
    -313.000000 583.000000 -35.000000
    -293.000000 593.000000 -38.000000
    -249.000000 617.000000 -45.000000
    -201.000000 638.000000 -50.000000
    -163.000000 655.000000 -57.000000
    -68.000000 682.000000 -69.000000
    -32.000000 691.000000 -74.000000
    -69.000000 686.000000 -70.000000
    -87.000000 677.000000 -72.000000
    -94.000000 674.000000 -75.000000
    -66.000000 680.000000 -76.000000
    -43.000000 688.000000 -80.000000
    -15.000000 694.000000 -84.000000

    1. Hallo Andreas,

      eigentlich sieht alles gut aus – Kannst Du mal Ganzzahlen (int), also ohne Komma ausprobieren?

      Und reduziere mal testweise auf z.B. nur 10 Zeilen. Nicht dass doch irgendwo fehlerhafte Zeichen bei der Übertragung auftreten…

      Gruss,
      Alexander

    2. Noch etwas: falls alles nicht klappt, versuche bitte einmal diese Zeilen-Ende-Kombination für jede neue Zeile:

      1. carriage return character (ASCII 13, or ‘\r’) und direkt danach
      2. newline character (ASCII 10, or ‘\n’)

Leave a Reply to Andreas Groll Cancel reply

Your email address will not be published. Required fields are marked *

IMPORTANT! To be able to proceed, please enter the magic word 'eangad' so we know hat you are a human)

Enter the magic word:
Please leave these two fields as-is:

A blog on projects with robotics, computer vision, 3D printing, microcontrollers, car diagnostics, localization & mapping, digital filters, LiDAR and more