Docs

RL-GNSS-001 · Navigation · KAMAL NAV GM1

Magnetometer

MMC5983MA compass integration notes and a single-shot I2C read example.

Published May 4, 2026Updated May 9, 2026Reading time ~15 min

01Compass behavior#

The MMC5983MA provides 3-axis magnetic-field readings over I2C. Use SET/RESET at startup and periodically during operation to reduce residual magnetization effects. Calibrate hard-iron and soft-iron offsets in the final airframe, not on the bench.

02Single-shot read example#

#define MMC5983MA_ADDR  0x30
#define REG_XOUT_0      0x00
#define REG_STATUS      0x08
#define REG_CTRL0       0x09
#define BIT_TM_M        0x01
#define BIT_MEAS_DONE   0x01

void mmc5983ma_read_xyz(int32_t *x, int32_t *y, int32_t *z) {
  i2c_write_byte(MMC5983MA_ADDR, REG_CTRL0, BIT_TM_M);

  uint8_t status = 0;
  while (!(status & BIT_MEAS_DONE)) {
    status = i2c_read_byte(MMC5983MA_ADDR, REG_STATUS);
  }

  uint8_t buf[7];
  i2c_read_bytes(MMC5983MA_ADDR, REG_XOUT_0, buf, 7);

  uint32_t rx = ((uint32_t)buf[0] << 10) | ((uint32_t)buf[1] << 2) | ((buf[6] >> 6) & 0x03);
  uint32_t ry = ((uint32_t)buf[2] << 10) | ((uint32_t)buf[3] << 2) | ((buf[6] >> 4) & 0x03);
  uint32_t rz = ((uint32_t)buf[4] << 10) | ((uint32_t)buf[5] << 2) | ((buf[6] >> 2) & 0x03);

  *x = (int32_t)rx - 131072;
  *y = (int32_t)ry - 131072;
  *z = (int32_t)rz - 131072;
}
c

03Calibration#

Perform compass calibration after the module is mounted in the final airframe. Moving the board, cable, battery, or power wiring can change hard-iron and soft-iron offsets.

Was this page helpful?Send feedback
Back to top