Example Code for Arduino-Get geomagnetic data & compass angle
Last revision 2025/12/17
This project guides users through reading 3-axis geomagnetic data and calculating compass angles using Arduino and the BMM150 sensor, including example code and data conversion methods.
Hardware Preparation
- FireBeetle ESP32-E IoT Microcontroller(SKU: DFR0654) (or similar) x 1
- BMM150 Triple Axis Magnetometer (SKU: SEN0529)x 1
- Gravity-4P I2C/UART Sensor Cable(included with SEN0529) × 1
- Dupont Wires
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Install SDK: click to enter FireBeetle ESP32-E IoT Microcontroller Wiki for SDK installation tutorial
- Download the DFRobot_BMM150 Library: DFRobot_BMM150 Library.
- For Arduino IDE V1.8.19 (or earlier), install the library manually: How to Add a Library
- For Arduino IDE V2.0.0 (or later), directly search for the "DFRobot_BMM150 Library" in the Library Manager and install it.
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
#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 */
//When using SPI communication, use the following program to construct an object by DFRobot_BMM150_SPI
#define BMM150_CS D3
//DFRobot_BMM150_SPI bmm150(/*cs = */BMM150_CS);
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!");
bmm150.setOperationMode(BMM150_POWERMODE_NORMAL);//Normal mode
bmm150.setPresetMode(BMM150_PRESETMODE_HIGHACCURACY);//High-accuracy mode
bmm150.setRate(BMM150_DATA_RATE_10HZ);//10HZ
bmm150.setMeasurementXYZ();//Default config of X-, Y-, and Z-axis
}
void loop()
{
sBmm150MagData_t magData = bmm150.getGeomagneticData();
Serial.print("mag x = "); Serial.print(magData.x); Serial.println(" uT");
Serial.print("mag y = "); Serial.print(magData.y); Serial.println(" uT");
Serial.print("mag z = "); Serial.print(magData.z); Serial.println(" uT");
/* float type data*/
//Serial.print("mag x = "); Serial.print(magData.xx); Serial.println(" uT");
//Serial.print("mag y = "); Serial.print(magData.yy); Serial.println(" uT");
//Serial.print("mag z = "); Serial.print(magData.zz); Serial.println(" uT");
float compassDegree = bmm150.getCompassDegree();
Serial.print("the angle between the pointing direction and north (counterclockwise) is:");
Serial.println(compassDegree);
Serial.println("--------------------------------");
delay(100);
}
Result
Burn sample code, and serial prints the geomagnetic data and compass angle obtained by the sensor.

Was this article helpful?
