How to Calibrate your Magnetometer – Tutorial

Pmod CMPS2, oblique view.
Pmod CMPS2 3-axis digital magnetometer/compass.

In a previous post, we discussed how to start getting data from your magnetometer, using the Pmods CMPS2 as an example. The Pmod CMPS2 uses the I2C communication protocol so instructions were given in psuedo code for an I2C controller. The same applies here.

Calibrating your magnetometer is usually the hardest part about using this type of sensor and successfully turning it into a digital compass. The instructions are below, but brace yourself because it’s a long one.

Calibration

To calibrate the magnetometer, the offset associated with the magnetic sensors and the environment needs to be calculated and removed from future measurements. The internal offset can be calculated and accounted for through the following method:

  1. Power on the Pmod CMPS2 and wait for 10 mS before further operation.
  2. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  3. Wait to receive an ACK from the Pmod CMPS2.
  4. Send the Internal Control Register 0 (address 0x07) as the register to communicate with

    I2CWrite(0x07); //address 0x07 corresponds to Control Register 0
  5. Wait to receive an ACK from the Pmod CMPS2.
  6. Write the command to recharge the capacitor to prepare for the SET action.

    I2CWrite(0x80); //0x80 refills the capacitor
  7. Wait to receive an ACK from the Pmod CMPS2.
  8. Delay at least 50 mS to allow the Pmod CMPS2 to finish preparing.
  9. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  10. Wait to receive an ACK from the Pmod CMPS2.
  11. Send the Internal Control Register 0 (address 0x07) as the register to communicate with

    I2CWrite(0x07); //address 0x07 corresponds to Control Register 0
  12. Wait to receive an ACK from the Pmod CMPS2.
  13. Write the command to start a SET action.

    I2CWrite(0x20); //0x20 starts the SET action
  14. Wait to receive an ACK from the Pmod CMPS2.
  15. Delay at least 1 mS to allow the Pmod CMPS2 to finish the SET action.
  16. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  17. Wait to receive an ACK from the Pmod CMPS2.
  18. Send the Internal Control Register 0 (address 0x07) as the register to communicate with

    I2CWrite(0x07); //address 0x07 corresponds to Control Register 0
  19. Wait to receive an ACK from the Pmod CMPS2.
  20. Write the command to take a measurement by setting bit 0 high followed by a STOP bit.

    I2CWrite(0x01); //0x01 initiates a data acquisition
  21. Delay at least 7.92 mS by default to allow the Pmod CMPS2 to finish collecting data.
  22. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  23. Wait to receive an ACK from the Pmod CMPS2.
  24. Send the Status Register (0x03) as the register to read

    I2CWrite(0x03); //indicate you wish to interact with address 0x03
  25. Provide a START condition and call the device ID with a read bit

    I2CBegin(0xA1); //device ID 0x30 with a read (1) bit
  26. Wait to receive an ACK from the Pmod CMPS2.
  27. Cycle the SCL line to receive the Status Register data on the SDA line. Keep reading the Status Register by repeating steps 8 through 13 until bit 0 is set to ‘1’, indicating that the data on all 3 axes as available to be read.
  28. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  29. Wait to receive an ACK from the Pmod CMPS2.
  30. Send the first register address corresponding to Xout LSB (0x00) as the register to be read.

    I2CWrite(0x00); //address 0x00 as the first register to be read
  31. Provide a START condition and call the device ID with a read bit

    I2CBegin(0xA1); //device ID 0x30 with a read (1) bit
  32. Wait to receive an ACK from the Pmod CMPS2.
  33. Cycle the SCL line to receive the data bits from the X, Y, and Z registers in the SDA line, providing an ACK between each data byte. The Pmod CMPS2 address pointer automatically moves to each consecutive byte. End the communication by sending a NACK followed by a STOP command.

    I2CReadMultiple(6); //read six bytes, sending an ACK to the slave device between each byte received
                                       and a NACK after the last byte
  34. Each of the readings will contain the external magnetic field H in addition to offset associated with the current put through the coil by the SET action.
    Output1=+H+OffsetOutput1=+H+Offset
  35. Now a RESET action will be performed to reverse the magnetization for the sensing resistors to get the inverse offset value.
  36. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  37. Wait to receive an ACK from the Pmod CMPS2.
  38. Send the Internal Control Register 0 (address 0x07) as the register to communicate with

    I2CWrite(0x07); //address 0x07 corresponds to Control Register 0
  39. Wait to receive an ACK from the Pmod CMPS2.
  40. Write the command to recharge the capacitor to prepare for the RESET action.

    I2CWrite(0x80); //0x80 refills the capacitor
  41. Wait to receive an ACK from the Pmod CMPS2.
  42. Delay at least 50 mS to allow the Pmod CMPS2 to finish preparing.
  43. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  44. Wait to receive an ACK from the Pmod CMPS2.
  45. Send the Internal Control Register 0 (address 0x07) as the register to communicate with

    I2CWrite(0x07); //address 0x07 corresponds to Control Register 0
  46. Wait to receive an ACK from the Pmod CMPS2.
  47. Write the command to start a RESET action.

    I2CWrite(0x40); //0x40 starts the RESET action
  48. Wait to receive an ACK from the Pmod CMPS2.
  49. Delay at least 1 mS to allow the Pmod CMPS2 to finish the SET action.
  50. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  51. Wait to receive an ACK from the Pmod CMPS2.
  52. Send the Internal Control Register 0 (address 0x07) as the register to communicate with

    I2CWrite(0x07); //address 0x07 corresponds to Control Register 0
  53. Wait to receive an ACK from the Pmod CMPS2.
  54. Write the command to take a measurement by setting bit 0 high followed by a STOP bit.

    I2CWrite(0x01); //0x01 initiates a data acquisition
  55. Delay at least 7.92 mS by default to allow the Pmod CMPS2 to finish collecting data.
  56. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  57. Wait to receive an ACK from the Pmod CMPS2.
  58. Send the Status Register (0x03) as the register to read

    I2CWrite(0x03); //indicate you wish to interact with address 0x03
  59. Provide a START condition and call the device ID with a read bit

    I2CBegin(0xA1); //device ID 0x30 with a read (1) bit
  60. Wait to receive an ACK from the Pmod CMPS2.
  61. Cycle the SCL line to receive the Status Register data on the SDA line. Keep reading the Status Register by repeating steps 8 through 13 until bit 0 is set to ‘1’, indicating that the data on all 3 axes as available to be read.
  62. Provide a START condition and call the device ID with a write bit

    I2CBegin(0xA0); //device ID 0x30 with a write (0) bit
  63. Wait to receive an ACK from the Pmod CMPS2.
  64. Send the first register address corresponding to Xout LSB (0x00) as the register to be read.

    I2CWrite(0x00); //address 0x00 as the first register to be read
  65. Provide a START condition and call the device ID with a read bit

    I2CBegin(0xA1); //device ID 0x30 with a read (1) bit
  66. Wait to receive an ACK from the Pmod CMPS2.
  67. Cycle the SCL line to receive the data bits from the X, Y, and Z registers in the SDA line, providing an ACK between each data byte. The Pmod CMPS2 address pointer automatically moves to each consecutive byte. End the communication by sending a NACK followed by a STOP command.

    I2CReadMultiple(6); //read six bytes, sending an ACK to the slave device between each byte received
                                       and a NACK after the last byte
  68. Each of the readings will contain the external magnetic field H in addition to offset associated with the current put through the coil by the RESET action.
    OutputH+Offset
  69. The offset associated with the device can be calculated and then subtracted from future measurements to obtain the actual magnetic field.
    Offse= (Output1+Output2)/2

The other method of calibration is to remove the hard iron bias introduced to the magnetometer by the surrounding environment by measuring (either continually or for a set time) all of the possible magnetometer readings in a Cartesian sphere and keeping track of the highest and lowest readings for each axis. The offset can then be calculated in the same fashion as described above with the SET and RESET actions. The advantage of this method is when the Pmod CMPS2 is continually changing locations or when other external magnetic fields may be introduced into the test environment.

Don’t forget, the data also needs to be converted from units of Gauss into a proper compass heading. A future post will describe this in detail, or you can visit the Pmod CMPS2 reference manual.

If you’d like to see some example code written in the Arduino IDE for how we acquire, calibrate and convert data from the Pmod CMPS2, see the Pmod CMPS2 Resource Center.

For questions or comments, please post in the comment section below or visit the Digilent Forum!

Author

One Comment on “How to Calibrate your Magnetometer – Tutorial”

Leave a Reply to Abdulrahman Nasser Alzhrani Cancel reply

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