This project demonstrates how to use the STM32 NUCLEO-H7A3ZI-Q to read data from a BNO055 IMU sensor via I2C and transmit the data to a PC over UART. The code is based on the bno055_stm32 repository.
This repository now serves as documentation of how I configured the IMU sensor with the microcontroller. I hope it can help others who are starting with IMU sensors and STM32 development.
- Launch STM32CubeIDE and open the
.ioc
file for the project.
- Navigate to Pinout & Configuration.
- Under the peripherals list (A → Z), search for I2C1.
- Enable I2C1 and configure it in GPIO Settings as shown in the image below.
The default pins for I2C1 were PB6 (I2C1_SCL) and PB7 (I2C1_SDA), but these pins did not work in my setup.
So I changed the pins to:
- PB8 (I2C1_SCL)
- PB9 (I2C1_SDA)
- Go to Project Manager → Code Generator.
- Uncheck the box for Generate peripheral initialization as a pair of
.c/.h
files per peripheral.
- Press
Ctrl + S
to save the.ioc
file, which will regenerate the code with the updated configuration.
To enable floating-point support in printf
and scanf
, follow these steps:
- Go to Project → Properties in STM32CubeIDE.
- Navigate to C/C++ Build → Settings.
- Under the Tool Settings tab, find Miscellaneous (under MCU GCC Linker).
- In the Other Flags field, add the following flags:
-u _printf_float
-u _scanf_float
- Click Apply and Close to save the changes.
This step ensures that floating-point values can be properly handled in formatted input/output functions like printf
and scanf
.
Add these files to your project:
- Place
bno055.c
in the Core/Src folder. - Place
bno055.h
andbno055_stm32.h
in the Core/Inc folder.
- Open
main.c
in STM32CubeIDE. - Add the following includes inside the
/* USER CODE BEGIN Includes */
section:
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "string.h"
#include "bno055_stm32.h"
/* USER CODE END Includes */
- In the
/_ USER CODE BEGIN 2 _/
section ofmain.c
, add the following setup code to initialize the BNO055 IMU sensor:
/* USER CODE BEGIN 2 */
bno055_assignI2C(&hi2c1);
bno055_setup();
bno055_setOperationModeNDOF();
/* USER CODE END 2 */
- Inside the
/_ USER CODE BEGIN 0 _/
section ofmain.c
, add the following function:
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void send_imu_data() {
char buffer[100];
// Get Orientation and Linear Acceleration data
bno055_vector_t orient = bno055_getVectorEuler();
bno055_vector_t linear = bno055_getVectorLinearAccel();
// IMU data
snprintf(buffer, sizeof(buffer),
"[IMU Data] Orient: x= %.2f, y= %.2f, z= %.2f ; Linear: x= %.2f, y= %.2f, z= %.2f\r\n",
orient.x, orient.y, orient.z, linear.x, linear.y, linear.z);
HAL_UART_Transmit(&huart3, (uint8_t*) buffer, strlen(buffer), HAL_MAX_DELAY);
HAL_Delay(100); // 10 Hz
}
/* USER CODE END 0 */
- Call the function inside the main loop to continuously fetch and transmit data:
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
send_imu_data();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
To connect the BNO055 IMU sensor to the STM32 NUCLEO-H7A3ZI-Q, use the following wiring:
BNO055 Pin | NUCLEO Pin |
---|---|
Vin | 3V3 |
GND | GND |
SDA | SDA (PB9) |
SCL | SCL (PB8) |
Ensure the connections are secure to avoid communication issues. The image below illustrates the wiring:
For more detailed information about the external header connections of the NUCLEO-H7A3ZI-Q, refer to the image below:
- IDE: STM32CubeIDE (1.16.1)
- Microcontroller: STM32 NUCLEO-H7A3ZI-Q
- IMU Sensor: Adafruit 9-DOF Absolute Orientation IMU Fusion Breakout - BNO055
This project uses code from the bno055_stm32 repository, licensed under the MIT License. The original copyright belongs to Ivy Knob and contributors.