Skip to main content

BHI385 Smart IMU - Accelerometer and Gyroscope

The corrected accelerometer and gyroscope virtual sensors deliver bias-corrected acceleration (in g) and angular velocity (in deg/s).


Reading accelerometer and gyroscope

After initialization, enable the accelerometer and gyroscope, then call update() in your loop. Check accelUpdated() or gyroUpdated() before reading to confirm that new data arrived in the latest FIFO drain.

void setup()
{
// ... begin() and loadFirmware() as above ...

imu.enableAccelerometer(100.0f, BHI385_ACCEL_8G); // 100 Hz, ±8 g
imu.enableGyroscope(100.0f, BHI385_GYRO_2000DPS); // 100 Hz, ±2000 deg/s
}

void loop()
{
if (imu.update())
{
if (imu.accelUpdated())
{
Serial.print("Accel X: "); Serial.print(imu.getAccelX(), 4); Serial.print(" g ");
Serial.print("Y: "); Serial.print(imu.getAccelY(), 4); Serial.print(" g ");
Serial.print("Z: "); Serial.println(imu.getAccelZ(), 4);
}
if (imu.gyroUpdated())
{
Serial.print("Gyro X: "); Serial.print(imu.getGyroX(), 2); Serial.print(" dps ");
Serial.print("Y: "); Serial.print(imu.getGyroY(), 2); Serial.print(" dps ");
Serial.print("Z: "); Serial.println(imu.getGyroZ(), 2);
}
imu.clearUpdatedFlags();
}
delay(20);
}
Serial monitor Accelerometer and gyroscope readings
Serial monitor Accelerometer and gyroscope output

imu.enableAccelerometer()

Enables the corrected accelerometer virtual sensor at the specified output data rate and dynamic range. The firmware applies bias removal and calibration to the raw accelerometer counts before delivering them to the FIFO.

Returns value: true if the sensor was configured successfully

Function parameters:

TypeNameDescription
floatrateHzOutput data rate in Hz (e.g. 100.0f). Default: 100.0f
bhi385AccelRangerangeDynamic range - BHI385_ACCEL_4G, BHI385_ACCEL_8G (default), BHI385_ACCEL_16G, or BHI385_ACCEL_32G

imu.enableGyroscope()

Enables the corrected gyroscope virtual sensor at the specified output data rate and full-scale range. The firmware applies bias correction before outputting angular velocity.

Returns value: true if the sensor was configured successfully

Function parameters:

TypeNameDescription
floatrateHzOutput data rate in Hz. Default: 100.0f
bhi385GyroRangerangeFull-scale range - BHI385_GYRO_125DPS, BHI385_GYRO_250DPS, BHI385_GYRO_500DPS, BHI385_GYRO_1000DPS, or BHI385_GYRO_2000DPS (default)

imu.update()

Reads and parses all pending data from both the wake-up and non-wake-up FIFOs, updating internal accelerometer, gyroscope, quaternion, step, gesture, and tap fields. Sets the corresponding *Updated flags for any sensor events found. Also drains the STATUS FIFO to prevent overflow. Call this regularly in loop().

Returns value: true if the FIFO read completed (even if no sensor events were present)

imu.getAccelX() / getAccelY() / getAccelZ()

Returns the most recently parsed accelerometer reading for the given axis, in units of g (gravitational acceleration). Values are only valid after update() has returned with accelUpdated() true.

Returns value: Acceleration in g as a float

imu.getGyroX() / getGyroY() / getGyroZ()

Returns the most recently parsed gyroscope reading for the given axis, in degrees per second (dps). Values are only valid after update() has returned with gyroUpdated() true.

Returns value: Angular velocity in deg/s as a float

imu.clearUpdatedFlags()

Clears all *Updated flags (accelUpdated, gyroUpdated, quatUpdated, stepUpdated, wristGestureUpdated, tapUpdated). Call after processing data from a single update() cycle to avoid acting on stale flags in the next iteration.

Returns value: None