Example Code for Arduino-Threshold interrupt
Last revision 2025/12/17
This article guides users through implementing a threshold interrupt function for the BMM150 sensor on Arduino, teaching them to configure interrupts triggered by magnetic field values below a threshold and manage interrupt events efficiently.
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 is set correctly and the interrupt pin (INT) of the BMM150 is connected to the appropriate pin on the ESP32-E (e.g., pin 13 or 2 as per the code).
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 */
#define BMM150_CS D3
volatile uint8_t interruptFlag = 0;
void myInterrupt(void)
{
interruptFlag = 1; // interrupt flag
#if defined(ESP32) || defined(ESP8266)||defined(ARDUINO_SAM_ZERO)
detachInterrupt(13); //Connect to pin INT
#else
detachInterrupt(0);
#endif
}
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);//rate of 10HZ
bmm150.setMeasurementXYZ();//Default config of X-, Y-, and Z-axis
bmm150.setThresholdInterrupt(LOW_THRESHOLD_INTERRUPT, 0, POLARITY_LOW);//trigger interrupt when below threshold
#if defined(ESP32) || defined(ESP8266)||defined(ARDUINO_SAM_ZERO)
pinMode(/*Pin */13 ,INPUT_PULLUP);//Low-polarity pull-up output
attachInterrupt(/*interput io*/13, myInterrupt, LOW);
pinMode(/*Pin */2 ,INPUT_PULLUP);
attachInterrupt(/*Interrupt No*/0, /*function*/myInterrupt ,/*state*/LOW );//Set pin interrupt
#endif
}
void loop()
{
if(interruptFlag == 1){
sBmm150ThresholdData_t threshold = bmm150.getThresholdData();
if(threshold.x != NO_DATA){
Serial.print("mag x = "); Serial.print(threshold.x); Serial.println(" uT");
}
if(threshold.y != NO_DATA){
Serial.print("mag y = "); Serial.print(threshold.y); Serial.println(" uT");
}
if(threshold.z != NO_DATA){
Serial.print("mag z = "); Serial.print(threshold.z); Serial.println(" uT");
}
Serial.println();
interruptFlag = 0;
#if defined(ESP32) || defined(ESP8266)||defined(ARDUINO_SAM_ZERO)
attachInterrupt(13, myInterrupt, LOW);
#else
attachInterrupt(0, myInterrupt ,LOW);
#endif
}
delay(100);
}
Result
Burn sample code, rock the sensor to trigger threshold interrupt, and serial prints data change.

Was this article helpful?
