IMU
The IMU Library provides a comprehensive interface for working with the BMI270 6-axis Inertial Measurement Unit (accelerometer + gyroscope). This library offers both raw sensor data access and process
Overview
The IMU library provides a simple interface for the BMI270 6-axis inertial measurement unit. It handles sensor initialization, calibration, and offers methods for reading acceleration, gyroscope data, and calculating orientation.
IMU()
Creates a new IMU object
begin()
Initializes the sensor and performs gyroscope calibration
reset()
Resets all angle calculations to zero
getAccelXms(), getAccelYms(), getAccelZms()
Returns acceleration in m/s² for each axis
getGyroXdeg(), getGyroYdeg(), getGyroZdeg()
Returns absolute rotation angle (0-360°) for each axis
getForceX(), getForceY(), getForceZ()
Returns acceleration in G-force for each axis
getRawAccelX(), getRawAccelY(), getRawAccelZ()
Returns raw 16-bit accelerometer values
getRawGyroX(), getRawGyroY(), getRawGyroZ()
Returns raw 16-bit gyroscope values
getOrientation(roll, pitch, yaw)
Calculates fused orientation using accelerometer and gyroscope
calibrateAccel()
Calibrates accelerometer bias (sensor must be flat and stationary)
calibrateGyro()
Calibrates gyroscope bias (sensor must be stationary)
Quick Start Example
#include <EchoLib.h>
IMU imu;
void setup() {
Serial.begin(115200);
imu.begin(); // Initializes and calibrates gyro
}
void loop() {
Serial.print("Raw Accel X: ");
Serial.println(imu.getRawAccelX());
Serial.print("Raw Gyro Y: ");
Serial.println(imu.getRawGyroY());
delay(100);
}Method Details
Constructor
IMU()
Creates an IMU object. All internal values are initialized to zero.
Initialization
void begin()
Initializes I2C communication (pins 17 and 18), resets the sensor, loads the configuration file, and performs automatic gyroscope calibration. The sensor must be stationary during initialization.
Reset
void reset()
Resets all calculated angles (roll, pitch, yaw, and gyroscope integration) back to zero. Useful when you need to re-establish a reference point.
Acceleration (m/s²)
float getAccelXms(), getAccelYms(), getAccelZms()
Returns calibrated acceleration in meters per second squared for the X, Y, or Z axis.
Acceleration (G-force)
float getForceX(), getForceY(), getForceZ()
Returns acceleration in terms of Earth's gravity (G-force). 1.0 = 9.81 m/s².
Gyroscope Angles
float getGyroXdeg(), getGyroYdeg(), getGyroZdeg()
Returns the absolute rotation angle (0-360°) for each axis. These values are continuously integrated from gyroscope readings and wrap around at 360°.
Raw Sensor Values
int16_t getRawAccelX(), getRawAccelY(), getRawAccelZ()
Returns the raw 16-bit accelerometer register values before any scaling or calibration.
int16_t getRawGyroX(), getRawGyroY(), getRawGyroZ()
Returns the raw 16-bit gyroscope register values before any scaling or calibration.
Orientation Fusion
void getOrientation(float &roll, float &pitch, float &yaw)
Calculates fused orientation by combining accelerometer and gyroscope data using a complementary filter. Pass three float variables by reference to receive the results.
Roll: Rotation around X-axis
Pitch: Rotation around Y-axis
Yaw: Rotation around Z-axis (relative only)
Calibration
void calibrateAccel()
Collects 100 samples to determine accelerometer bias. The sensor must be placed flat and motionless during calibration.
void calibrateGyro()
Collects 100 samples to determine gyroscope bias. The sensor must be completely stationary. This is automatically called during begin().
Complete Example
This example demonstrates orientation tracking with all three methods working together.
Important Notes
Yaw Drift
Without a magnetometer, yaw measurements are relative to the starting position and will drift over time. This drift is more pronounced during:
Vibration or shaking
Rapid rotational movements
Extended operation (hours)
Temperature changes
Solution: Use reset() periodically to re-establish a reference, or add an external magnetometer for absolute heading.
Accelerometer Accuracy
Roll and pitch calculations from the accelerometer assume gravity is the dominant force. During acceleration, these readings may be temporarily inaccurate:
During movement (walking, driving)
During vibration
When experiencing external forces
The complementary filter in getOrientation() minimizes this effect, but rapid movements will still cause brief inaccuracies.
Initialization
Always call begin() once in your setup() function before using any other IMU methods. This initializes the sensor and performs automatic gyroscope calibration.
Update Rate
For best results with getOrientation(), call it regularly in your loop (at least 10-20 times per second). The complementary filter uses time-based integration, so frequent updates improve accuracy.
Calibration
Gyroscope: Automatically calibrated during
begin(). Keep sensor stationary.Accelerometer: Optional calibration with
calibrateAccel(). Keep sensor level and stationary.
Moving the sensor during calibration will introduce errors that persist until you recalibrate.
I2C Configuration
This library uses pins 17 (SDA) and 18 (SCL) for I2C communication at 400kHz. These pins are hardcoded in the library. Ensure your hardware supports I2C on these pins.
Hardware Specifications
BMI270 Specifications
Accelerometer Range: ±2g (default)
Gyroscope Range: ±500°/s (default)
Resolution: 16-bit ADC
I2C Address: 0x68
I2C Clock: 400kHz (Fast Mode)
Last updated