top of page

Building a 9-DOF Attitude and Heading Reference System (AHRS) with the MKR WiFi 1010, MPU6050, and HMC5883L

In a previous project, we explored how to use an MPU6050 and the Arduino IDE to create a real-time 3D aircraft visualization using Processing. While the system successfully displayed pitch, roll, and yaw motion, one major limitation remained: yaw drift.


This occurs because the MPU6050 contains only an accelerometer and a gyroscope. Although the gyroscope provides rotational information, small measurement errors accumulate over time, causing the calculated heading to slowly drift away from reality.


To solve this problem, we expanded the system by integrating an HMC5883L three-axis magnetometer. Together, the MPU6050 and HMC5883L form a complete 9-degree-of-freedom (9-DOF) orientation sensing system capable of providing stable pitch, roll, and heading information.


In this article, we will walk through the complete process of integrating the HMC5883L with the MPU6050 and Arduino MKR WiFi 1010, implementing Madgwick quaternion-based sensor fusion, and visualizing the resulting aircraft orientation in Processing.


Why Add a Magnetometer?


The MPU6050 contains:


  • 3-axis accelerometer

  • 3-axis gyroscope



These sensors allow us to determine:


  • Pitch

  • Roll

  • Angular velocity


However, yaw estimation relies primarily on integrating gyroscope measurements over time:

Yaw = ∫ Gyroscope Z-axis. Even very small errors eventually accumulate, causing heading drift.

The HMC5883L solves this problem by measuring Earth's magnetic field and providing a reference to magnetic north.


This allows us to determine:

  • Absolute heading

  • Compass direction

  • Long-term yaw stability


By combining all nine sensor axes, we can create a complete Attitude and Heading Reference System (AHRS).


What is the Madgwick Filter in a 9-DOF Attitude and Heading Reference System?


The Madgwick Filter is a sensor fusion algorithm developed by Sebastian Madgwick to accurately estimate an object's orientation in three-dimensional space using data from multiple sensors. Rather than relying on a single sensor, the filter combines measurements from an accelerometer, gyroscope, and magnetometer to produce a stable, reliable estimate of an object's attitude. The gyroscope provides fast rotational information, the accelerometer provides a reference to gravity, and the magnetometer provides a reference to Earth's magnetic field and magnetic north. By intelligently blending these measurements, the Madgwick Filter minimizes drift, reduces noise, and maintains accurate orientation tracking over long periods.



One of the key advantages of the Madgwick Filter is that it represents orientation using quaternions rather than traditional pitch, roll, and yaw angles. Quaternions are mathematical constructs that describe rotation in three dimensions without suffering from a problem known as gimbal lock, which can occur when using Euler angles.


Sensor Fusion with Madgwick Filter: accelerometer, gyroscope, and magnetometer feed filter; outputs roll, pitch, yaw.

The Madgwick Filter serves as the "brain" that combines data from the MPU6050 and HMC5883L sensors. The MPU6050 supplies acceleration and rotational rate measurements, while the HMC5883L provides a stable heading reference. Together, these measurements enable the filter to continuously calculate the aircraft model's orientation in real time. The resulting quaternion and Euler angle outputs are then transmitted to Processing, where they are used to rotate and animate a 3D aircraft visualization. This same sensor fusion principle is used in many modern flight controllers and attitude-heading reference systems, providing an excellent introduction to the technologies behind autonomous aircraft, spacecraft, and advanced navigation systems.


Understanding Quaternions


When working with orientation and rotation in three-dimensional space, many people are familiar with describing an object's position using pitch, roll, and yaw. While these angles are intuitive and easy to visualize, they can become problematic in advanced applications because they are susceptible to gimbal lock, in which certain rotations can cause a loss of one degree of freedom, leading to unstable or inaccurate calculations. To overcome this limitation, engineers and scientists often use a mathematical representation known as a quaternion. A quaternion consists of four values—typically labeled q₀, q₁, q₂, and q₃—that together describe an object's orientation in three-dimensional space. Unlike Euler angles, quaternions provide a smooth and continuous representation of rotation without singularities, making them ideal for applications that require precise attitude estimation.


Colorful math diagram of circular arrows linking nodes i, j, k, 1, -1 with labels xi, xj, xk on a white background

Quaternions are widely used in aerospace, robotics, computer graphics, virtual reality, and autonomous navigation systems because they enable complex rotations to be represented and computed efficiently and accurately. Rather than storing orientation as a series of separate rotations about different axes, quaternions represent orientation as a single mathematical entity. This makes it easier to combine rotations, interpolate between orientations, and maintain numerical stability over time. Flight controllers used in drones, satellites, and spacecraft often perform all internal orientation calculations using quaternions before converting the results into pitch, roll, and yaw values that humans can easily understand.


In our project, the Madgwick Filter continuously calculates a quaternion using data from the MPU6050 and HMC5883L sensors. This quaternion represents the real-time orientation of the aircraft model and serves as the foundation for all attitude calculations. The quaternion values are then converted into roll, pitch, and yaw angles for display within Processing, allowing us to visualize the aircraft's motion while still benefiting from the accuracy and stability of quaternion-based mathematics. Understanding quaternions is an important step toward learning how modern flight control systems, navigation computers, and attitude-heading reference systems determine orientation in real-world aerospace applications.


System Overview

Our final system consists of:

  • Arduino MKR WiFi 1010

  • Arduino IDE

  • MPU6050 IMU

  • HMC5883L Magnetometer

  • Processing 4 Visualization


Data flow:

HMC5883L---> Madgwick Filter ---> Quaternion ---> Euler Angles ---> Processing//MPU6050



Hardware Connections

MPU6050

MPU6050

MKR WiFi 1010

VCC

3.3V

GND

GND

SDA

SDA

SCL

SCL

HMC5883L

HMC5883L

MKR WiFi 1010

VCC

5V

GND

GND

SDA

SDA

SCL

SCL

INT

Not Connected

One interesting discovery during testing was that the HMC5883L module did not appear on the I2C bus when powered from 3.3V.

After switching the module's VCC pin to the MKR's 5V supply, the device successfully appeared at address:


0x1E


This confirmed that the module's onboard regulator required a higher input voltage.


Verifying Communication with an I2C Scanner

Before writing any sensor fusion code, it is always a good idea to verify communication.

After connecting both sensors, our scanner reported:

Found device at 0x1EFound device at 0x68

Address meanings:

0x1E = HMC5883L

0x68 = MPU6050



Terminal screen with white text on dark background listing found devices at 0x1E, 0x60, 0x68, 0x6B and scan complete

At this point, we knew the hardware was functioning correctly.


Code for I2C Scanner Onboard Systems Verification:



Reading Sensor Data


The MPU6050 provides:

  • Acceleration

  • Angular velocity


The HMC5883L provides:

  • Magnetic field strength along X

  • Magnetic field strength along Y

  • Magnetic field strength along Z


Each sensor communicates over the shared I2C bus.

The Arduino continuously reads all nine channels and passes them into the Madgwick filter.

The filter then updates the current orientation quaternion.


Snippet of Arduino IDE Code:


Full Downloadable Arduino IDE Code Located Here:




Converting Quaternions to Euler Angles

Although quaternions are mathematically superior, humans understand orientation more easily as:

  • Pitch

  • Roll

  • Yaw


For visualization purposes, the quaternion is converted back into Euler angles.

These values are then transmitted over serial:


q0,q1,q2,q3,roll,pitch,yaw


Example:


0.998421,0.013211,-0.025133,0.043812,1.52,-2.88,94.61


Visualizing Orientation in Processing


Processing receives the serial data stream and rotates a 3D aircraft model in real time.

The aircraft rotates according to:

rotateY(yaw)rotateX(pitch)rotateZ(roll)


This produces a live representation of the sensor's orientation.



To make testing easier, a calibration feature was added.

Pressing the R key stores the current orientation as the new local reference frame.


This effectively re-centers the aircraft so that:

Roll = 0°Pitch = 0°Yaw = 0°


regardless of the sensor's physical orientation.


Processing 3D Visualization Code


Before running Processing, close the Arduino Serial Monitor or Serial Plotter. Only one program can use the COM port at a time.


This sketch uses COM6, since that was the working port in our setup. If your port is different, change this line:


myPort = new Serial(this, "COM6", 9600);


to either COM7 , COM8 , COM9 etc.


Full Sketch:






















































































Applications


The resulting system forms the foundation of many aerospace and robotics projects.

Potential applications include:

  • Flight controllers

  • Rocket stabilization systems

  • Thrust vector control

  • UAV attitude estimation

  • Robotics navigation

  • Ground station telemetry displays

  • Educational aerospace demonstrations


Because the Madgwick filter outputs quaternions, the system can easily be expanded into more advanced control systems.


Future Improvements


While this project successfully demonstrates real-time attitude estimation using the MPU6050, HMC5883L, and Madgwick Filter, there are numerous opportunities for future expansion. One potential enhancement is integrating a BMP180 barometric pressure sensor, which would enable the system to measure altitude, temperature, and vertical rate of climb. Incorporating a GPS module would further expand the project's capabilities by providing real-time position, velocity, and navigation data. Flight data could also be recorded on an SD card module, enabling post-flight analysis and mission replay.


Beyond data collection, wireless telemetry could be added to transmit sensor information to a remote ground station in real time, allowing users to monitor vehicle performance during flight. The system could also be expanded to include servo-based thrust vector control, where orientation data from the sensor fusion algorithm is used to actively stabilize a rocket during ascent. More advanced developments might include autonomous waypoint navigation for UAV applications and custom ground station software capable of displaying live telemetry, flight paths, and vehicle orientation. By combining these technologies into a single platform, it becomes possible to develop a complete flight computer suitable for advanced model rockets, autonomous aircraft, and educational aerospace research projects.


Conclusion


By combining the MPU6050, HMC5883L, and Arduino MKR WiFi 1010, we successfully built a complete 9-DOF attitude-and-heading reference system.


The addition of the magnetometer eliminated long-term yaw drift, while the Madgwick filter provided smooth quaternion-based orientation estimation.


Most importantly, this project demonstrates how relatively inexpensive sensors can be combined with modern sensor fusion algorithms to create systems that are, in principle, similar to those used in professional aerospace applications.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

©2025 by Sigma Unlimited . Proudly created with Wix.com

bottom of page