Self-test
What this example shows
Checking that the sensor's X and Y channels work, with the BMM350's built-in
self-test. The sensor generates an internal field of about 130 µT on one axis
at a time and measures the difference. A channel passes when the difference is
at least 130 µT. The sketch runs the test once, in setup().
The example code
/**
**************************************************
*
* @file selfTest.ino
*
* @brief Example showing how to run the BMM350's built-in self-test.
* The self-test generates an internal ~130 uT magnetic field on
* the X and Y channels and reports the resulting field
* difference; per the datasheet (section 5.1.6), a channel is
* considered working if its reported value is >= 130 uT.
*
* @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;
// Minimum self-test field difference to consider a channel passing,
// per the datasheet's self-test section
const float SELF_TEST_THRESHOLD_UT = 130.0;
void setup()
{
// Initialize serial for output
Serial.begin(115200);
Serial.println("BMM350 Example - Self Test");
// 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!");
// Run the self-test. The sensor automatically returns to normal mode
// afterwards since begin() already left it there.
struct bmm350_self_test result;
if (magSensor.performSelfTest(&result) == BMM350_OK)
{
Serial.print("X axis field: ");
Serial.print(result.out_ust_x, 2);
Serial.print(" uT -> ");
Serial.println(result.out_ust_x >= SELF_TEST_THRESHOLD_UT ? "PASS" : "FAIL");
Serial.print("Y axis field: ");
Serial.print(result.out_ust_y, 2);
Serial.print(" uT -> ");
Serial.println(result.out_ust_y >= SELF_TEST_THRESHOLD_UT ? "PASS" : "FAIL");
}
else
{
Serial.println("Self-test failed to run.");
}
}
void loop()
{
}
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.
Expected result, at 115200 baud: the two start lines, then two result lines,
X axis field: and Y axis field:, each with the field difference in uT and
PASS or FAIL.
BMM350 Example - Self Test
BMM350 connected!
X axis field: 148.37 uT -> PASS
Y axis field: 151.92 uT -> PASS
If the test cannot run, the sketch prints Self-test failed to run. instead.
Functions used
Soldered_BMM350()returns Soldered_BMM350Constructor. 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_tInitializes 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
| Type | Name | Description |
|---|---|---|
uint8_t | address | I2C address. Defaults to 0x14; use 0x15 when JP5 is moved. |
TwoWire & | wirePort | I2C bus to use. Defaults to Wire. |
int8_t performSelfTest(struct bmm350_self_test *result)returns int8_tRuns the sensor's built-in self-test on the X and Y axes and stores the result. The result's out_ust_x and out_ust_y members hold the field difference on each axis in µT.
ReturnsBMM350_OK (0) on success, an error code otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
struct bmm350_self_test * | result | Where to store the self-test result. |
Putting it together
begin() leaves the sensor in normal mode, so it returns to normal mode after
the self-test, as the sketch's comment says. The sketch compares out_ust_x
and out_ust_y with SELF_TEST_THRESHOLD_UT (130.0) and prints PASS or
FAIL for each axis. loop() is empty: reset the board to run the test again.