### Install BMI160 Arduino Library Source: https://github.com/hanyazou/bmi160-arduino/blob/master/README.md This command demonstrates how to install the BMI160-Arduino library by copying the project files into the Arduino IDE's library folder. ```shell cp ~/github/BMI160-Arduino ~/Documents/Arduino/libraries/ ``` -------------------------------- ### Configure and Read FIFO Buffer on BMI160 Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt This snippet demonstrates how to initialize the BMI160 sensor, enable FIFO buffering for both accelerometer and gyroscope data, and perform burst reads once a specific data threshold is reached. It uses the BMI160Gen library to manage sensor state and buffer retrieval. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, 10); // Enable FIFO for both sensors BMI160.setGyroFIFOEnabled(true); BMI160.setAccelFIFOEnabled(true); // Enable header mode for mixed data identification BMI160.setFIFOHeaderModeEnabled(true); // Enable FIFO full interrupt (optional) BMI160.setIntFIFOBufferFullEnabled(true); Serial.println("FIFO configured"); } void loop() { // Check FIFO count uint16_t fifoCount = BMI160.getFIFOCount(); if (fifoCount >= 100) { // Process when enough data Serial.print("FIFO count: "); Serial.println(fifoCount); // Read FIFO data uint8_t buffer[100]; BMI160.getFIFOBytes(buffer, min(fifoCount, 100)); // Process buffer data... // Each frame: 6 bytes accel + 6 bytes gyro (in headerless mode) Serial.println("FIFO data read"); } delay(100); } ``` -------------------------------- ### Initialize BMI160 Sensor Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Demonstrates how to initialize the BMI160 sensor using either SPI or I2C communication protocols. It includes optional hardware interrupt pin configuration and device ID verification. ```cpp #include const int select_pin = 10; const int interrupt_pin = 2; void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, select_pin); uint8_t dev_id = BMI160.getDeviceID(); Serial.print("Device ID: 0x"); Serial.println(dev_id, HEX); } void loop() {} ``` ```cpp #include const int i2c_addr = 0x68; void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::I2C_MODE, i2c_addr); uint8_t dev_id = BMI160.getDeviceID(); Serial.print("Device ID: 0x"); Serial.println(dev_id, HEX); } void loop() {} ``` -------------------------------- ### FIFO Configuration and Data Acquisition Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Configures the BMI160 FIFO buffer for gyro and accelerometer data and demonstrates how to read data from the buffer. ```APIDOC ## FIFO Operations ### Description Configures the FIFO buffer for sensor readings and retrieves buffered data to minimize MCU overhead. ### Method N/A (Library Function Calls) ### Endpoint BMI160 FIFO Interface ### Parameters #### Configuration Methods - **setGyroFIFOEnabled** (bool) - Required - Enables/disables gyro data in FIFO - **setAccelFIFOEnabled** (bool) - Required - Enables/disables accelerometer data in FIFO - **setFIFOHeaderModeEnabled** (bool) - Required - Enables header mode for mixed data identification ### Request Example BMI160.setGyroFIFOEnabled(true); BMI160.setAccelFIFOEnabled(true); ### Response #### Success Response - **getFIFOCount** (uint16_t) - Returns the number of bytes currently in the FIFO - **getFIFOBytes** (void) - Reads data from the FIFO into a provided buffer #### Response Example uint16_t fifoCount = BMI160.getFIFOCount(); BMI160.getFIFOBytes(buffer, count); ``` -------------------------------- ### Step Counter API Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Enables pedometer functionality and configures step detection sensitivity. ```APIDOC ## void setStepDetectionMode(int mode) ### Description Configures the sensitivity mode for the built-in pedometer. ### Parameters - **mode** (int) - Required - Sensitivity mode (CURIE_IMU_STEP_MODE_NORMAL, CURIE_IMU_STEP_MODE_SENSITIVE, CURIE_IMU_STEP_MODE_ROBUST). ## uint32_t getStepCount() ### Description Retrieves the current total step count. ### Response - **count** (uint32_t) - The total number of steps detected. ``` -------------------------------- ### Motion Detection API Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Configures thresholds and durations for motion and shock detection events. ```APIDOC ## void setDetectionThreshold(int type, float threshold) ### Description Sets the sensitivity threshold for specific motion detection types. ### Parameters - **type** (int) - Required - The detection type (e.g., CURIE_IMU_MOTION, CURIE_IMU_SHOCK). - **threshold** (float) - Required - The threshold value in mg. ## void setDetectionDuration(int type, int duration) ### Description Sets the duration required for a motion event to be registered. ### Parameters - **type** (int) - Required - The detection type. - **duration** (int) - Required - Duration in milliseconds. ``` -------------------------------- ### Enable Step Counter Functionality Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Utilizes the BMI160's built-in step detection capabilities for pedometer applications. This code snippet demonstrates how to set the step detection mode (normal, sensitive, robust), enable the step counter, and attach an interrupt to handle step detection events. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, 10, 2); // Set step detection mode // CURIE_IMU_STEP_MODE_NORMAL - balanced // CURIE_IMU_STEP_MODE_SENSITIVE - detects more steps // CURIE_IMU_STEP_MODE_ROBUST - fewer false positives BMI160.setStepDetectionMode(CURIE_IMU_STEP_MODE_NORMAL); // Enable step counter BMI160.setStepCountEnabled(true); // Enable step interrupt BMI160.setIntStepEnabled(true); BMI160.attachInterrupt(stepHandler); Serial.println("Step counter started. Start walking!"); } void stepHandler() { if (BMI160.stepsDetected()) { Serial.print("Step! Total count: "); Serial.println(BMI160.getStepCount()); } } void loop() { // Periodically display step count static unsigned long lastPrint = 0; if (millis() - lastPrint > 5000) { lastPrint = millis(); Serial.print("Total steps: "); Serial.println(BMI160.getStepCount()); } } ``` -------------------------------- ### Initialize and Read Gyro Data (Arduino) Source: https://github.com/hanyazou/bmi160-arduino/blob/master/README.md This Arduino code demonstrates how to initialize the BMI160 sensor in SPI mode and read raw gyroscope data (gx, gy, gz). It then prints the values to the serial monitor. The code requires the BMI160Gen library and can be adapted for I2C mode by changing the begin() method parameters. ```Arduino #include const int select_pin = 10; const int i2c_addr = 0x68; void setup() { Serial.begin(9600); // initialize Serial communication while (!Serial); // wait for the serial port to open // initialize device BMI160.begin(BMI160GenClass::SPI_MODE, select_pin); //BMI160.begin(BMI160GenClass::I2C_MODE, i2c_addr); } void loop() { int gx, gy, gz; // raw gyro values // read raw gyro measurements from device BMI160.readGyro(gx, gy, gz); // display tab-separated gyro x/y/z values Serial.print("g:\t"); Serial.print(gx); Serial.print("\t"); Serial.print(gy); Serial.print("\t"); Serial.print(gz); Serial.println(); delay(500); } ``` -------------------------------- ### Configure Hardware Interrupts with attachInterrupt() Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Sets up hardware interrupt handling for event-driven motion detection using the BMI160 sensor. The callback function is invoked when configured events like taps are detected. Requires the BMI160Gen library and specifies SPI communication mode. ```cpp #include const int select_pin = 10; const int interrupt_pin = 2; volatile bool tapDetected = false; void bmi160_interrupt() { tapDetected = true; } void setup() { Serial.begin(9600); while (!Serial); // Initialize with interrupt pin BMI160.begin(BMI160GenClass::SPI_MODE, select_pin, interrupt_pin); // Attach interrupt callback BMI160.attachInterrupt(bmi160_interrupt); // Enable tap detection interrupt BMI160.setIntTapEnabled(true); Serial.println("Tap the sensor!"); } void loop() { if (tapDetected) { tapDetected = false; Serial.println("TAP detected!"); // Check which axis was tapped if (BMI160.tapDetected(X_AXIS, POSITIVE)) Serial.println(" +X axis"); if (BMI160.tapDetected(X_AXIS, NEGATIVE)) Serial.println(" -X axis"); if (BMI160.tapDetected(Y_AXIS, POSITIVE)) Serial.println(" +Y axis"); if (BMI160.tapDetected(Y_AXIS, NEGATIVE)) Serial.println(" -Y axis"); if (BMI160.tapDetected(Z_AXIS, POSITIVE)) Serial.println(" +Z axis"); if (BMI160.tapDetected(Z_AXIS, NEGATIVE)) Serial.println(" -Z axis"); } } ``` -------------------------------- ### attachInterrupt() - Configure Hardware Interrupts Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Sets up hardware interrupt handling for event-driven motion detection. The callback function is triggered when configured events like taps are detected. ```APIDOC ## void attachInterrupt(void (*callback)(void)) ### Description Registers a callback function to be executed when a hardware interrupt is triggered by the BMI160 sensor. ### Parameters - **callback** (function pointer) - Required - The function to call when an interrupt occurs. ### Request Example BMI160.attachInterrupt(my_callback_function); ### Response - **void** - This method does not return a value. ``` -------------------------------- ### Configure Sensor Measurement Ranges Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Sets the full-scale range for the gyroscope and accelerometer. Higher ranges allow for measuring more intense motion but reduce sensitivity. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, 10); BMI160.setGyroRange(250); Serial.print("Gyro range: +/- "); Serial.print(BMI160.getGyroRange()); Serial.println(" deg/s"); BMI160.setAccelerometerRange(4); Serial.print("Accel range: +/- "); Serial.print(BMI160.getAccelerometerRange()); Serial.println(" G"); } void loop() {} ``` -------------------------------- ### Read Accelerometer Data Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Retrieves raw acceleration data from the accelerometer. The raw values are converted to G-force units based on the configured range. ```cpp #include void setup() { Serial.begin(9600); BMI160.begin(BMI160GenClass::SPI_MODE, 10); BMI160.setAccelerometerRange(2); } void loop() { int axRaw, ayRaw, azRaw; BMI160.readAccelerometer(axRaw, ayRaw, azRaw); float ax = (axRaw * 2.0) / 32768.0; float ay = (ayRaw * 2.0) / 32768.0; float az = (azRaw * 2.0) / 32768.0; Serial.print("Accel (G): "); Serial.print(ax); Serial.print("\t"); Serial.print(ay); Serial.print("\t"); Serial.println(az); delay(100); } ``` -------------------------------- ### Configure Sensor Sampling Rates Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Sets the Output Data Rate (ODR) for the sensors. Higher rates provide more frequent data updates but increase power consumption. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, 10); BMI160.setGyroRate(100); Serial.print("Gyro rate: "); Serial.print(BMI160.getGyroRate()); Serial.println(" Hz"); BMI160.setAccelerometerRate(100); Serial.print("Accel rate: "); Serial.print(BMI160.getAccelerometerRate()); Serial.println(" Hz"); } void loop() {} ``` -------------------------------- ### Implement Motion and Shock Detection Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Configures the BMI160 sensor to detect various motion events including shocks (high-G impacts) and general motion. It allows setting detection thresholds and durations, and enables corresponding interrupts. The motionHandler function processes detected events. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, 10, 2); BMI160.attachInterrupt(motionHandler); // Configure motion detection threshold (in mg) BMI160.setDetectionThreshold(CURIE_IMU_MOTION, 100.0); // 100mg threshold BMI160.setDetectionDuration(CURIE_IMU_MOTION, 10); // 10ms duration // Configure shock detection BMI160.setDetectionThreshold(CURIE_IMU_SHOCK, 1500.0); // 1500mg threshold // Enable interrupts BMI160.setIntMotionEnabled(true); BMI160.setIntShockEnabled(true); Serial.println("Motion detection active..."); } void motionHandler() { // Check motion status if (BMI160.getIntMotionStatus()) { Serial.print("MOTION: "); if (BMI160.motionDetected(X_AXIS, POSITIVE)) Serial.print("+X "); if (BMI160.motionDetected(X_AXIS, NEGATIVE)) Serial.print("-X "); if (BMI160.motionDetected(Y_AXIS, POSITIVE)) Serial.print("+Y "); if (BMI160.motionDetected(Y_AXIS, NEGATIVE)) Serial.print("-Y "); if (BMI160.motionDetected(Z_AXIS, POSITIVE)) Serial.print("+Z "); if (BMI160.motionDetected(Z_AXIS, NEGATIVE)) Serial.print("-Z "); Serial.println(); } // Check shock status if (BMI160.getIntShockStatus()) { Serial.println("SHOCK detected!"); } } void loop() { delay(10); } ``` -------------------------------- ### Read 6DoF Motion Data with BMI160 Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Reads both accelerometer and gyroscope data in a single call. This method is efficient for retrieving all six axes of motion data simultaneously. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, 10); BMI160.setGyroRange(250); BMI160.setAccelerometerRange(2); } void loop() { int ax, ay, az; int gx, gy, gz; BMI160.readMotionSensor(ax, ay, az, gx, gy, gz); Serial.print("Accel: "); Serial.print(ax); Serial.print("\t"); Serial.print(ay); Serial.print("\t"); Serial.print(az); Serial.print("\tGyro: "); Serial.print(gx); Serial.print("\t"); Serial.print(gy); Serial.print("\t"); Serial.println(gz); delay(100); } ``` -------------------------------- ### Perform Automatic Sensor Calibration Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Calibrates sensor offsets to remove bias. The device must remain stationary during this process, with specific orientation for accelerometer axis calibration. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, 10); Serial.println("Place device flat and stationary..."); delay(3000); BMI160.autoCalibrateGyroOffset(); BMI160.autoCalibrateAccelerometerOffset(X_AXIS, 0); BMI160.autoCalibrateAccelerometerOffset(Y_AXIS, 0); BMI160.autoCalibrateAccelerometerOffset(Z_AXIS, 1); Serial.println("Calibration complete!"); } void loop() {} ``` -------------------------------- ### Read Internal Temperature from BMI160 Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Reads the raw internal temperature sensor data and converts it to Celsius. Useful for environmental monitoring or thermal compensation. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BMI160.begin(BMI160GenClass::SPI_MODE, 10); } void loop() { int rawTemp = BMI160.readTemperature(); float tempC = (rawTemp / 512.0) + 23.0; Serial.print("Temperature: "); Serial.print(tempC); Serial.println(" C"); delay(1000); } ``` -------------------------------- ### Read Gyroscope Data Source: https://context7.com/hanyazou/bmi160-arduino/llms.txt Retrieves raw angular velocity data from the gyroscope. The raw values are converted to degrees per second based on the configured range. ```cpp #include void setup() { Serial.begin(9600); BMI160.begin(BMI160GenClass::SPI_MODE, 10); BMI160.setGyroRange(250); } void loop() { int gxRaw, gyRaw, gzRaw; BMI160.readGyro(gxRaw, gyRaw, gzRaw); float gx = (gxRaw * 250.0) / 32768.0; float gy = (gyRaw * 250.0) / 32768.0; float gz = (gzRaw * 250.0) / 32768.0; Serial.print("Gyro (deg/s): "); Serial.print(gx); Serial.print("\t"); Serial.print(gy); Serial.print("\t"); Serial.println(gz); delay(100); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.