boussole_bmm150.ino

Arduino Nicla Sense ME · File : boussole_bmm150.ino · Language : Arduino · Use case : Lire le magnétomètre BMM150

The BMM150 (12-bit, ±1300/±2500 µT) gives the raw magnetic field: magnetic heading via atan2, magnet or door-open detection. The complement of the fused quaternion when you want raw material, not fusion.

Hardware

An Arduino Nicla Sense ME (nRF52832, BLE only) with Bosch BHI260AP hub, BMM150, BMP390, BME688 sensors, RGB LED on an I2C driver and LiPo charger. The board alone. For compass use, keep it away from desk metal and the PC speaker while reading.

Wiring

No wiring. The BMM150 sits on a dedicated I2C bus (docs-db) — the hub handles it, nothing to do at the pins.

How it works

SensorMag follows the same BHY2 pattern (begin/checkNewData/x-y-z in µT). Heading = atan2(y,x) wrapped to 0-360°; correct it by local declination for geographic north.

Running it

Programmed from the Arduino IDE (ArduinoCore-mbed); Arduino_BHY2 and Nicla_System libraries; USB through a serial bridge.

Variants

Calibrate hard/soft iron offsets (figure-eight motion), detect passing ferrous objects by norm deviation at rest, or log heading for a track recorder.

boussole_bmm150.ino

// BMM150 : magnétomètre 3 axes (±1300/±2500 µT) derrière le hub.
// Attention : le hub compense gyro+acc+mag pour l'orientation ;
// ici on lit le champ brut pour une boussole ou un détecteur
// de métal / d'ouverture de porte.

#include "Arduino_BHY2.h"

SensorMag mag(0);

void setup() {
  Serial.begin(115200);
  BHY2.begin();
  mag.begin();
  BHY2.update();
  Serial.println("Magnétomètre prêt (µT)");
}

void loop() {
  BHY2.update();

  if (mag.checkNewData()) {
    float x = mag.x(), y = mag.y(), z = mag.z();

    // Cap magnétique : atan2 sur le plan horizontal
    float cap = atan2f(y, x) * 57.2958;
    if (cap < 0) cap += 360.0;

    Serial.print("X="); Serial.print(x);
    Serial.print(" Y="); Serial.print(y);
    Serial.print(" Z="); Serial.print(z);
    Serial.print(" uT  cap="); Serial.println(cap);
  }
  delay(50);
}

← Back to the board