Example Code for Arduino-Get config status

Last revision 2025/12/17

This article offers example code for Arduino to fetch the BMM150 sensor's current configuration status, including initialization, operation mode settings, and data retrieval.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0529-I2C wiring diagram

Sensor Pin name Controller board Pin name
BMM150 Sensor VCC ESP32-E Microcontroller 3V3
BMM150 Sensor GND ESP32-E Microcontroller GND
BMM150 Sensor SCL ESP32-E Microcontroller SCL
BMM150 Sensor SDA ESP32-E Microcontroller SDA

Other Preparation Work

Ensure the I2C address of the BMM150 sensor is set correctly using the 2-bit DIP switch. The default I2C address is 0X13 (A0=1, A1=1). Refer to the Pinout section for I2C address combinations.

Sample Code

Initialize BMM150 magnetometer, configure mode and read running status

#include "DFRobot_BMM150.h"
DFRobot_BMM150_I2C bmm150(&Wire, I2C_ADDRESS_4);
/*i2c Address select, that CS and SDO pin select 1 or 0 indicates the high or low respectively. There are 4 combinations: 
 *I2C_ADDRESS_1 0x10  (CS:0 SDO:0)
 *I2C_ADDRESS_2 0x11  (CS:0 SDO:1)
 *I2C_ADDRESS_3 0x12  (CS:1 SDO:0)
 *I2C_ADDRESS_4 0x13  (CS:1 SDO:1) default i2c address */
 
#define BMM150_CS D3

void setup() 
{
  Serial.begin(115200);
  while(!Serial);
  while(bmm150.begin()){
    Serial.println("bmm150 init failed, Please try again!");
    delay(1000);
  } Serial.println("bmm150 init success!");

  Serial.println(bmm150.selfTest(BMM150_SELF_TEST_NORMAL));//The returned character string indicates the self test result

  bmm150.setOperationMode(BMM150_POWERMODE_NORMAL);//Normal mode

  bmm150.setPresetMode(BMM150_PRESETMODE_HIGHACCURACY);// High-accuracy mode

  bmm150.setRate(BMM150_DATA_RATE_30HZ);//30HZ

  bmm150.setMeasurementXYZ();//Default config of X-, Y-, and Z-axis

  uint8_t rate = bmm150.getRate();//Get the configured data rate
  Serial.print("rate is "); Serial.print(rate); Serial.println(" HZ");

  Serial.println(bmm150.getMeasurementStateXYZ());

  /**!
   * Get the sensor operation mode, return the sensor operation mode as character string
   */
  Serial.println(bmm150.getOperationMode());

  bmm150.softReset();  //Software reset  
}

void loop() 
{

  Serial.println(bmm150.getOperationMode());
  delay(1000);
}

Result

Burn sample code, and serial prints the current config status of the sensor.

SEN0529-I2C get sensor status result

Was this article helpful?

TOP