ESP32 with BMI160 Accelerometer & Gyroscope Sensor
Interfacing BMI160 Accelerometer & Gyroscope with ESP32 C++ Code to measure raw values, gyroscope pitch roll & acceleration in x, y, z axis
- Suitable forB.Tech/Diploma ESP32/IoT students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
Overview
In this tutorial, we will be
Interfacing the BMI160 Accelerometer & Gyroscope with ESP32
. Earlier we used
BMI160 with Arduin
o
and also
BMI160 with Raspberry Pi Pico
. The
BMI160
is a small, low-power, low-noise 16-bit
inertial measurement unit (IMU)
that integrates a
3-axis accelerometer
and
3-axis gyroscope
. It provides precise motion sensing for applications like augmented reality, fitness tracking, and indoor navigation.
This tutorial provides detailed information about the
BMI160 Accelerometer Gyroscope Module
, including its specifications and technical details. First, we will interface the BMI160 with an ESP32 board to read the
raw acceleration
and
gyroscope values
. Next, we will write an
auto-calibration code
to use with the
acceleration measurement code
to measure acceleration across all three axes. Finally, in the third example, we will use the
gyroscope data
to measure tilt angles such as
pitch
and
roll
. You can make a project called
Flight Black-Box Motion Recorder
using ESP32 and BMI160.
Bill of Materials
We will need the following components for this tutorial. You can purchase all the components from the given links.
S.N.
Components Name
Quantity
Purchase Link
1
ESP32 Board
1
Amazon
AliExpress
2
BMI160 Accelerometer Gyroscope
1
Amazon
AliExpress
3
Connecting Wires
10
Amazon
AliExpress
4
Breadboard
1
Amazon
AliExpress
BMI160 3 Axis Accelerometer & Gyroscope
The
BMI160
is a high-performance, small, and ultra-low-power 16-bit Inertial Measurement Unit (IMU) that combines a 3-axis accelerometer and a 3-axis gyroscope in a single package.
Fig: Front & Back of BMI160
It is designed to deliver accurate and reliable motion sensing for a wide range of applications, such as augmented reality, indoor navigation, wearable devices, and gaming. The sensor offers advanced features like motion detection,
step counting
, and gesture recognition, making it versatile for modern technology needs.
The
BMI160
supports both
I2C
and
SPI
interfaces for communication, making it compatible with a variety of microcontrollers and processors. It has a compact footprint of just
2.5 x 3.0 x 0.8 mm³
, which is smaller than many competing IMUs, such as the MPU6050, and is well-suited for space-constrained devices like wearables. Refer to Bosch
BMI160 Datasheet
for more information.
Technical Specifications of BMI160
Operating Voltage: 1.71 – 3.6 V (Breakout Board operates between 3.2V~6V)
Power Consumption
:
Gyroscope: 900 µA (full operation)
Gyroscope + Accelerometer: 950 µA (full operation)
Accelerometer: 180 µA (full operation)
Suspend Mode: 3 µA
Motion Detection: 200 µA
Acceleration Range: ±2g/±4g/±8g/±16g
Gyroscopes Range: ±125°/s,±250°/s,±500°/s,±1000°/s,±2000°/s
Acceleration Zero-g Offset: ±40mg
Gyroscopes Zero-g Offset: ±10°/s
Interfaces
:
Primary
: I2C or SPI
Secondary
: High-speed SPI for optical image stabilization
Noise Density
:
Accelerometer: 180 µg/√Hz
Gyroscope: 0.008 °/s/√Hz
Programmable Frequency: 25/32Hz~1600Hz
6D Detection and Location
16-bit Data Output
Shock Resistance: 1000gx 200us
2 Independent Programmable Interrupt Generators
In-built 1024 Byte FIFO
Applications of BMI160
Augmented Reality
: Tracks motion for virtual environments.
Indoor Navigation
: Supports step counting and positioning.
Mobile Devices
: For smartphones and smartwatches.
Wearables
: Powers fitness and health devices.
Gaming
: Tracks motion in controllers.
Cameras
: Stabilizes images.
Drones & Toys
: Enables motion sensing.
Pinout of BMI160
Fig: Pinout of BMI160 Gyroscope Accelerometer Module
Pin
Description
VIN
VIN
Power input pin. Accepts
3.3V to 5V
and steps it down for the BMI160 using an onboard regulator.
3.3V
3.3V
Regulated 3.3V output pin.
GND
GND
Ground pin. Connect to the ground of your system.
OCS
OCS
Optional Chip Select for secondary SPI interface (used in optical image stabilization).
INT1
INT1
Interrupt pin 1. Used for motion events or data-ready notifications.
INT2
INT2
Interrupt pin 2. An additional interrupt pin for more advanced use cases.
SCL/SCX
SCL
or
SCX
I2C clock line
when using I2C communication. Acts as
SPI clock (SCX)
in SPI mode.
SDA/SDX
SDA
or
SDX
I2C data line
when using I2C communication. Acts as
SPI data (SDX)
in SPI mode.
CS
CS
Chip Select pin for SPI communication. Used to select the BMI160 during SPI transactions.
SA0
SA0
I2C address selection pin. Connect to
GND
for address
0x68
or to
3.3V
for
0x69
Interfacing BMI160 Accelerometer & Gyroscope with ESP32
Let us interface the BMI160 Gyroscope Accelerometer module with ESP32 Microcontroller Board. We can either use the module in SPI Mode or in I2C Mode. For this tutorial, we are using the I2C Mode as it is very easy to configure and requires less wiring as well.
Here is an interfacing circuit diagram for the connection between BMI160 and ESP32 Board.
Figure: Interfacing BMI160 Accelerometer & Gyroscope with ESP32
BMI160 Pin
ESP32 Pin
Description
3.3V
3.3V
Power input for the BMI160.
GND
GND
Ground connection.
SCL
22
I2C Clock line.
SDA
21
I2C Data line.
SA0
GND
Sets I2C address to
0x68
Here we have connected the
SA0
Pin to the
GND
. Connecting
SA0
to
GND
sets the I2C address to
0x68
. If connected to 3.3V, the I2C address would change to
0x69
Figure: Breadboard Assembly of BMI160 & ESP32
Use a simple breadboard and connect as shown in the circuit diagram above.
Code: Reading the BMI160 Raw Acceleration and Gyroscope Data
After hardware connection is done, now lets write an
C++ Code
to read the Raw Acceleration and Gyroscope Data from BMI160 Module & ESP32.
To get the BMI160 readings, a very well-written library is available. Download the
BMI160 Library
maintained by
Hanyazou
and add it to Arduino IDE via library manager.
The following code initializes the BMI160 sensor over I2C, reads raw accelerometer and gyroscope data and prints them to the Serial Monitor for real-time monitoring.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include `BMI160Gen.h` //https://github.com/hanyazou/BMI160-Arduino
#include `Wire.h`
// I2C Configuration for ESP32
const
int
i2c_addr
=
0x68
;
// Default I2C address for BMI160
const
int
sda_pin
=
21
;
// I2C SDA Pin for ESP32 (default GPIO 21)
const
int
scl_pin
=
22
;
// I2C SCL Pin for ESP32 (default GPIO 22)
void
setup
(
)
{
// Initialize Serial communication at 115200 baud rate
Serial
begin
(
115200
)
;
while
(
!
Serial
)
;
// Wait for Serial Monitor to connect (not required on ESP32)
// Initialize I2C with custom SDA and SCL pins
Wire
begin
(
sda_pin
scl_pin
)
;
// Initialize the BMI160 device in I2C mode
if
(
!
BMI160
begin
(
BMI160GenClass
::
I2C_MODE
i2c_addr
)
)
{
Serial
println
(
\"BMI160 initialization failed!\"
)
;
while
(
1
)
;
// Halt if initialization fails
}
Serial
println
(
\"BMI160 initialized successfully in I2C mode!\"
)
;
}
void
loop
(
)
{
int
gx
gy
gz
;
// Raw gyroscope values
int
ax
ay
az
;
// Raw accelerometer values
// Read raw gyroscope measurements from the BMI160
BMI160
readGyro
(
gx
gy
gz
)
;
// Read raw accelerometer measurements from the BMI160
BMI160
readAccelerometer
(
ax
ay
az
)
;
// Display the gyroscope values (X, Y, Z) on the Serial Monitor
Serial
(
\"Gyroscope Data (X, Y, Z): \"
)
;
Serial
(
gx
)
;
Serial
(
\", \"
)
;
Serial
(
gy
)
;
Serial
(
\", \"
)
;
Serial
println
(
gz
)
;
// Display the accelerometer values (X, Y, Z) on the Serial Monitor
Serial
(
\"Accelerometer Data (X, Y, Z): \"
)
;
Serial
(
ax
)
;
Serial
(
\", \"
)
;
Serial
(
ay
)
;
Serial
(
\", \"
)
;
Serial
println
(
az
)
;
delay
(
100
)
;
// Delay for readability (500ms)
}
Copy the above code and paste it into your
Arduino IDE
. From the
Board Manager
, select the
ESP32 Board
and also the
COM port
. Then, upload the code to the ESP32.
After the code u
In this tutorial, we will be
Interfacing the BMI160 Accelerometer & Gyroscope with ESP32
. Earlier we used
BMI160 with Arduin
o
and also
BMI160 with Raspberry Pi Pico
. The
BMI160
is a small, low-power, low-noise 16-bit
inertial measurement unit (IMU)
that integrates a
3-axis accelerometer
and
3-axis gyroscope
. It provides precise motion sensing for applications like augmented reality, fitness tracking, and indoor navigation.
This tutorial provides detailed information about the
BMI160 Accelerometer Gyroscope Module
, including its specifications and technical details. First, we will interface the BMI160 with an ESP32 board to read the
raw acceleration
and
gyroscope values
. Next, we will write an
auto-calibration code
to use with the
acceleration measurement code
to measure acceleration across all three axes. Finally, in the third example, we will use the
gyroscope data
to measure tilt angles such as
pitch
and
roll
. You can make a project called
Flight Black-Box Motion Recorder
using ESP32 and BMI160.
Bill of Materials
We will need the following components for this tutorial. You can purchase all the components from the given links.
S.N.
Components Name
Quantity
Purchase Link
1
ESP32 Board
1
Amazon
AliExpress
2
BMI160 Accelerometer Gyroscope
1
Amazon
AliExpress
3
Connecting Wires
10
Amazon
AliExpress
4
Breadboard
1
Amazon
AliExpress
BMI160 3 Axis Accelerometer & Gyroscope
The
BMI160
is a high-performance, small, and ultra-low-power 16-bit Inertial Measurement Unit (IMU) that combines a 3-axis accelerometer and a 3-axis gyroscope in a single package.
Fig: Front & Back of BMI160
It is designed to deliver accurate and reliable motion sensing for a wide range of applications, such as augmented reality, indoor navigation, wearable devices, and gaming. The sensor offers advanced features like motion detection,
step counting
, and gesture recognition, making it versatile for modern technology needs.
The
BMI160
supports both
I2C
and
SPI
interfaces for communication, making it compatible with a variety of microcontrollers and processors. It has a compact footprint of just
2.5 x 3.0 x 0.8 mm³
, which is smaller than many competing IMUs, such as the MPU6050, and is well-suited for space-constrained devices like wearables. Refer to Bosch
BMI160 Datasheet
for more information.
Technical Specifications of BMI160
Operating Voltage: 1.71 – 3.6 V (Breakout Board operates between 3.2V~6V)
Power Consumption
:
Gyroscope: 900 µA (full operation)
Gyroscope + Accelerometer: 950 µA (full operation)
Accelerometer: 180 µA (full operation)
Suspend Mode: 3 µA
Motion Detection: 200 µA
Acceleration Range: ±2g/±4g/±8g/±16g
Gyroscopes Range: ±125°/s,±250°/s,±500°/s,±1000°/s,±2000°/s
Acceleration Zero-g Offset: ±40mg
Gyroscopes Zero-g Offset: ±10°/s
Interfaces
:
Primary
: I2C or SPI
Secondary
: High-speed SPI for optical image stabilization
Noise Density
:
Accelerometer: 180 µg/√Hz
Gyroscope: 0.008 °/s/√Hz
Programmable Frequency: 25/32Hz~1600Hz
6D Detection and Location
16-bit Data Output
Shock Resistance: 1000gx 200us
2 Independent Programmable Interrupt Generators
In-built 1024 Byte FIFO
Applications of BMI160
Augmented Reality
: Tracks motion for virtual environments.
Indoor Navigation
: Supports step counting and positioning.
Mobile Devices
: For smartphones and smartwatches.
Wearables
: Powers fitness and health devices.
Gaming
: Tracks motion in controllers.
Cameras
: Stabilizes images.
Drones & Toys
: Enables motion sensing.
Pinout of BMI160
Fig: Pinout of BMI160 Gyroscope Accelerometer Module
Pin
Description
VIN
VIN
Power input pin. Accepts
3.3V to 5V
and steps it down for the BMI160 using an onboard regulator.
3.3V
3.3V
Regulated 3.3V output pin.
GND
GND
Ground pin. Connect to the ground of your system.
OCS
OCS
Optional Chip Select for secondary SPI interface (used in optical image stabilization).
INT1
INT1
Interrupt pin 1. Used for motion events or data-ready notifications.
INT2
INT2
Interrupt pin 2. An additional interrupt pin for more advanced use cases.
SCL/SCX
SCL
or
SCX
I2C clock line
when using I2C communication. Acts as
SPI clock (SCX)
in SPI mode.
SDA/SDX
SDA
or
SDX
I2C data line
when using I2C communication. Acts as
SPI data (SDX)
in SPI mode.
CS
CS
Chip Select pin for SPI communication. Used to select the BMI160 during SPI transactions.
SA0
SA0
I2C address selection pin. Connect to
GND
for address
0x68
or to
3.3V
for
0x69
Interfacing BMI160 Accelerometer & Gyroscope with ESP32
Let us interface the BMI160 Gyroscope Accelerometer module with ESP32 Microcontroller Board. We can either use the module in SPI Mode or in I2C Mode. For this tutorial, we are using the I2C Mode as it is very easy to configure and requires less wiring as well.
Here is an interfacing circuit diagram for the connection between BMI160 and ESP32 Board.
Figure: Interfacing BMI160 Accelerometer & Gyroscope with ESP32
BMI160 Pin
ESP32 Pin
Description
3.3V
3.3V
Power input for the BMI160.
GND
GND
Ground connection.
SCL
22
I2C Clock line.
SDA
21
I2C Data line.
SA0
GND
Sets I2C address to
0x68
Here we have connected the
SA0
Pin to the
GND
. Connecting
SA0
to
GND
sets the I2C address to
0x68
. If connected to 3.3V, the I2C address would change to
0x69
Figure: Breadboard Assembly of BMI160 & ESP32
Use a simple breadboard and connect as shown in the circuit diagram above.
Code: Reading the BMI160 Raw Acceleration and Gyroscope Data
After hardware connection is done, now lets write an
C++ Code
to read the Raw Acceleration and Gyroscope Data from BMI160 Module & ESP32.
To get the BMI160 readings, a very well-written library is available. Download the
BMI160 Library
maintained by
Hanyazou
and add it to Arduino IDE via library manager.
The following code initializes the BMI160 sensor over I2C, reads raw accelerometer and gyroscope data and prints them to the Serial Monitor for real-time monitoring.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include `BMI160Gen.h` //https://github.com/hanyazou/BMI160-Arduino
#include `Wire.h`
// I2C Configuration for ESP32
const
int
i2c_addr
=
0x68
;
// Default I2C address for BMI160
const
int
sda_pin
=
21
;
// I2C SDA Pin for ESP32 (default GPIO 21)
const
int
scl_pin
=
22
;
// I2C SCL Pin for ESP32 (default GPIO 22)
void
setup
(
)
{
// Initialize Serial communication at 115200 baud rate
Serial
begin
(
115200
)
;
while
(
!
Serial
)
;
// Wait for Serial Monitor to connect (not required on ESP32)
// Initialize I2C with custom SDA and SCL pins
Wire
begin
(
sda_pin
scl_pin
)
;
// Initialize the BMI160 device in I2C mode
if
(
!
BMI160
begin
(
BMI160GenClass
::
I2C_MODE
i2c_addr
)
)
{
Serial
println
(
\"BMI160 initialization failed!\"
)
;
while
(
1
)
;
// Halt if initialization fails
}
Serial
println
(
\"BMI160 initialized successfully in I2C mode!\"
)
;
}
void
loop
(
)
{
int
gx
gy
gz
;
// Raw gyroscope values
int
ax
ay
az
;
// Raw accelerometer values
// Read raw gyroscope measurements from the BMI160
BMI160
readGyro
(
gx
gy
gz
)
;
// Read raw accelerometer measurements from the BMI160
BMI160
readAccelerometer
(
ax
ay
az
)
;
// Display the gyroscope values (X, Y, Z) on the Serial Monitor
Serial
(
\"Gyroscope Data (X, Y, Z): \"
)
;
Serial
(
gx
)
;
Serial
(
\", \"
)
;
Serial
(
gy
)
;
Serial
(
\", \"
)
;
Serial
println
(
gz
)
;
// Display the accelerometer values (X, Y, Z) on the Serial Monitor
Serial
(
\"Accelerometer Data (X, Y, Z): \"
)
;
Serial
(
ax
)
;
Serial
(
\", \"
)
;
Serial
(
ay
)
;
Serial
(
\", \"
)
;
Serial
println
(
az
)
;
delay
(
100
)
;
// Delay for readability (500ms)
}
Copy the above code and paste it into your
Arduino IDE
. From the
Board Manager
, select the
ESP32 Board
and also the
COM port
. Then, upload the code to the ESP32.
After the code u
Technologies used
Hardware components
- ESP32
- Arduino
- Raspberry Pi
- MPU6050
Software & tools
- Arduino IDE
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.