Skip to main content

bmi270-code


#include <Wire.h>
#include "SparkFun_BMI270_Arduino_Library.h"

// 定义I2C引脚
#define SDA_PIN 4
#define SCL_PIN 5

// Create a new sensor object
BMI270 imu;

// I2C address selection
uint8_t i2cAddress = BMI2_I2C_PRIM_ADDR; // 0x68
//uint8_t i2cAddress = BMI2_I2C_SEC_ADDR; // 0x69

void setup() {
// Start serial
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN); // 初始化硬件 I2C
Serial.println("BMI270 Example 1 - Basic Readings I2C");

// Initialize the I2C library
Wire.begin();

// Check if sensor is connected and initialize
// Address is optional (defaults to 0x68)
while (imu.beginI2C(i2cAddress) != BMI2_OK) {
// Not connected, inform user
Serial.println("Error: BMI270 not connected, check wiring and I2C address!");

// Wait a bit to see if connection is established
delay(1000);
}

Serial.println("BMI270 connected!");
}

void loop() {
// Get measurements from the sensor. This must be called before accessing
// the sensor data, otherwise it will never update
imu.getSensorData();

// Print acceleration data
Serial.print("Acceleration in g's");
Serial.print("\t");
Serial.print("X: ");
Serial.print(imu.data.accelX, 3);
Serial.print("\t");
Serial.print("Y: ");
Serial.print(imu.data.accelY, 3);
Serial.print("\t");
Serial.print("Z: ");
Serial.print(imu.data.accelZ, 3);

Serial.print("\t");

// Print rotation data
Serial.print("Rotation in deg/sec");
Serial.print("\t");
Serial.print("X: ");
Serial.print(imu.data.gyroX, 3);
Serial.print("\t");
Serial.print("Y: ");
Serial.print(imu.data.gyroY, 3);
Serial.print("\t");
Serial.print("Z: ");
Serial.println(imu.data.gyroZ, 3);

// Print 50x per second
delay(20);
}