Skip to main content

Basic readings

What this example shows​

Reading the compensated magnetic field on all three axes, in µT, and the sensor's temperature, in a loop about 10 times a second. This is the library's simplest example and the one the Quick Start uses.

The example code​

Select Board
basicReadings.ino
/**
**************************************************
*
* @file basicReadings.ino
*
* @brief Example showing how to read compensated magnetometer and
* temperature data from the Soldered BMM350 Geomagnetic
* Sensor breakout over I2C
*
* @link solde.red/333359 : Soldered BMM350 Geomagnetic Sensor breakout
*
* @authors Soldered Electronics
***************************************************/
#include <BMM350-SOLDERED.h>

// Create an instance of the BMM350 sensor
Soldered_BMM350 magSensor;

void setup()
{
// Initialize serial for output
Serial.begin(115200);
Serial.println("BMM350 Example - Basic Readings");

// Initialize I2C bus
Wire.begin();

// Check if sensor is connected and initialize it.
// Address is optional, defaults to 0x14 (ADSEL tied low on this breakout)
while (magSensor.begin() != BMM350_OK)
{
Serial.println("Error: BMM350 not connected, check wiring and I2C address!");
delay(1000);
}

Serial.println("BMM350 connected!");
}

void loop()
{
// Get new measurements from the sensor. This must be called before
// accessing the magX/magY/magZ/temperature data, otherwise it will never update
if (magSensor.getSensorData() == BMM350_OK)
{
Serial.print("X: ");
Serial.print(magSensor.magX, 2);
Serial.print(" uT\t");
Serial.print("Y: ");
Serial.print(magSensor.magY, 2);
Serial.print(" uT\t");
Serial.print("Z: ");
Serial.print(magSensor.magZ, 2);
Serial.print(" uT\t");
Serial.print("Temperature: ");
Serial.print(magSensor.temperature, 2);
Serial.println(" degC");
}

// Print 10x per second
delay(100);
}

This example also uses Wire, Serial and delay(), which come with the Arduino core and are not part of the BMM350 library. delay() pauses the sketch for the number of milliseconds you give it: delay(1000) waits one second between connection attempts, and delay(100) sets the pace of about 10 readings a second.

Expected result, at 115200 baud: the two start lines, then one line per reading, about every 100 ms: X:, Y: and Z:, each in uT with two decimals, then Temperature: in degC, separated by tabs.

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
BMM350 Example - Basic Readings
BMM350 connected!
X: 12.84 uT Y: -21.37 uT Z: -38.52 uT Temperature: 24.61 degC
X: 12.91 uT Y: -21.30 uT Z: -38.47 uT Temperature: 24.62 degC
X: 12.79 uT Y: -21.42 uT Z: -38.55 uT Temperature: 24.61 degC
X: 12.88 uT Y: -21.35 uT Z: -38.49 uT Temperature: 24.63 degC

If the sensor does not answer, the sketch prints Error: BMM350 not connected, check wiring and I2C address! once a second.

Functions used​

Soldered_BMM350()returns Soldered_BMM350

Constructor. Creates the sensor object; nothing is sent to the sensor until begin().

ReturnsThe sensor object.

int8_t begin(uint8_t address = 0x14, TwoWire &wirePort = Wire)returns int8_t

Initializes the sensor over I2C, then sets a 100 Hz output data rate with 4-sample averaging, enables the X, Y and Z axes and starts normal mode.

ReturnsBMM350_OK (0) on success, an error code otherwise.

Parameters

TypeNameDescription
uint8_taddressI2C address. Defaults to 0x14; use 0x15 when JP5 is moved.
TwoWire &wirePortI2C bus to use. Defaults to Wire.
int8_t getSensorData()returns int8_t

Reads compensated magnetometer and temperature data and stores them in the magX, magY and magZ members (µT) and the temperature member (°C). Call it before reading those members, or they never update.

ReturnsBMM350_OK (0) on success, an error code otherwise.

Putting it together​

begin() returns BMM350_OK once the sensor is set up, so the while loop in setup() keeps retrying once a second until it answers. Each pass of loop() calls getSensorData() and prints magX, magY, magZ and temperature only when it returns BMM350_OK.

For a board with JP5 moved to 0x15, start the sensor with magSensor.begin(0x15).