STM32-Based IMU Attitude Estimation: Difference between revisions
Line 107: | Line 107: | ||
Yaw Z-axis Rotating left/right | Yaw Z-axis Rotating left/right | ||
In this project, we mainly estimate roll and pitch. Yaw is harder to determine accurately without a magnetometer. | In this project, we mainly estimate roll and pitch. Yaw is harder to determine accurately without a magnetometer. | ||
[[File:roll_pitch_yaw_axes.png|center|thumb|500px|Illustration of roll (X-axis), pitch (Y-axis), and yaw (Z-axis) in a 3D coordinate system.]] | |||
=== Using the Accelerometer === | === Using the Accelerometer === |
Revision as of 10:47, 6 April 2025
This project involves using an STM32 microcontroller and an MPU6050 sensor module to measure angular velocity and acceleration. The collected sensor data will be processed to determine the attitude angles (roll, pitch, and yaw) of the system. The project will include sensor calibration, data filtering (e.g., complementary or Kalman filter), and real-time angle computation for accurate motion tracking applications.
Introduction
This project aims to build a simple but functional system that can estimate the orientation (attitude) of an object in real time. We used an STM32F103C8T6 microcontroller together with an MPU6050 sensor, which includes a 3-axis gyroscope and a 3-axis accelerometer.
By collecting data from the sensor and processing it through our own code, we can estimate how the object is tilted—specifically its roll, pitch, and yaw angles. To make the results more stable and accurate, we applied filtering techniques (e.g., complementary filter) to combine acceleration and gyroscope data.
The project mainly involves:
Reading raw sensor data through I2C communication
Calibrating the sensor to reduce offset and noise
Using a filtering algorithm to compute stable angle values
Updating the angles in real time
This kind of system is commonly used in applications like drones, mobile phones, and robot balance systems. Our goal is to understand how such a system works from both a hardware and software perspective by building one from scratch.
Team Members
LI DING
FAN XUTING
Project Overview
The idea of this project is to estimate the orientation (attitude) of an object using sensor data. We used the MPU6050 module, which combines a gyroscope and an accelerometer, to measure how the object is moving and rotating in space. The sensor data is collected using the STM32F103C8T6 microcontroller through I2C communication.
Since the raw data from the sensor is not very stable or accurate by itself, we applied a filtering method (in our case, a complementary filter) to combine the gyroscope and accelerometer readings. This allows us to reduce noise and drift, and get smoother and more reliable angle values over time.
The final goal is to calculate the roll, pitch, and yaw angles of the system in real time. This kind of setup is commonly used in things like self-balancing robots, drones, camera stabilizers, or even mobile phones for detecting screen orientation. Our project is a small-scale version of such systems, built from scratch to better understand how attitude estimation actually works.
Setup
Equipment
Here are the main components used in this project:
- **STM32F103C8T6 ("Blue Pill")** – A versatile microcontroller for controlling the system.
- **MPU6050** – A 6-axis IMU sensor that includes both a 3-axis accelerometer and a 3-axis gyroscope.
- **USB to TTL Serial Converter** – Used for programming the STM32 and communicating with the serial terminal.
- **Jumper wires and breadboard** – For making connections between the STM32 and MPU6050.
- **Power supply (3.3V)** – Provided by the STM32 or through an external 3.3V regulator.
The MPU6050 module is powered with 3.3V, and it communicates with the STM32 over the I2C bus.
Wiring / Circuit Overview
Here's how the components are connected:
MPU6050 Pin | STM32 Pin | Notes |
---|---|---|
VCC | 3.3V | Powered by STM32 |
GND | GND | Common ground |
SDA | PB7 | I2C data line |
SCL | PB6 | I2C clock line |
The sensor is connected to the STM32 using the I2C interface, which allows the STM32 to read the sensor's data. I2C is a simple and efficient communication protocol for embedded systems.
Optional: A USB to TTL converter is used for programming the STM32 and debugging through the serial port. This allows us to print the sensor data or estimated angles to the terminal.
Software & Code
All the software for this project was written in C, using STM32's standard peripheral libraries. The key steps in the code are:
- **Initialize I2C communication** – Set up the I2C peripheral on the STM32 and ensure it can communicate with the MPU6050.
- **Configure the MPU6050** – Set the sensor’s output scale, enable the gyroscope and accelerometer, and set the operating mode.
- **Read raw data** – Read sensor registers over I2C (accelerometer in g, gyroscope in °/s).
- **Convert raw values** – Convert to physical units like acceleration (g) and angular velocity (°/s).
- **Estimate angles** – Use a complementary filter to fuse data, compute roll and pitch.
- **Output results** – Print the final angles over serial for easy monitoring.
The code runs in a continuous loop, updating at a fixed interval (e.g., every 10ms) for smooth, real-time performance.
Theory & Angle Estimation
To estimate the orientation (attitude) of an object using the MPU6050 sensor, we need to understand both the physical meaning of the sensor outputs and how they can be processed to obtain rotation angles.
Sensor Principles
The MPU6050 is a 6-axis MEMS-based Inertial Measurement Unit (IMU). It consists of:
A 3-axis accelerometer – measures linear acceleration in x, y, z directions (in units of g)
A 3-axis gyroscope – measures angular velocity around x, y, z axes (in °/s)
These sensors give raw physical measurements:
The accelerometer reflects the direction of gravity and motion
The gyroscope measures how fast the object rotates
By analyzing both, we can estimate the orientation (roll, pitch, yaw) of the object.
Attitude Angles: Roll, Pitch, Yaw
Attitude refers to the rotation of an object in 3D space. It is commonly expressed using three angles:
Angle Axis Description Roll X-axis Tilting side-to-side Pitch Y-axis Tilting forward/backward Yaw Z-axis Rotating left/right In this project, we mainly estimate roll and pitch. Yaw is harder to determine accurately without a magnetometer.

Using the Accelerometer
When the object is still or moving slowly, the accelerometer can be used to estimate roll and pitch based on the gravity vector. The equations are:
These formulas give a rough estimate of the angle, but are sensitive to motion and vibration, which cause noise.
Using the Gyroscope
The gyroscope provides angular velocity. We can integrate this value over time to get angle changes: . However, small measurement errors accumulate over time, causing drift.
Complementary Filter
To overcome the weaknesses of both sensors, we use a complementary filter, which combines:
The short-term precision of the gyroscope
The long-term stability of the accelerometer
The formula is: . Where: 𝜔 is angular velocity (from gyroscope), 𝜃 is the angle from the accelerometer, 𝛼 is a constant (typically 0.95 ~ 0.98).
This filter gives a smooth and responsive estimation of the roll and pitch angles.