Source: https://mokxi.com/libraries/madgwick
Updated: 2026-11-02

Libraries / Data and time
Arduino library · Data and time

# Madgwick: roll, pitch and yaw from an IMU

An accelerometer knows which way is down but is shaken by every bump, and a gyroscope is smooth but drifts. Sebastian Madgwick’s orientation filter blends the two into one steady estimate of roll, pitch and yaw, and the Arduino Madgwick library (MadgwickAHRS.h) packages it in five calls. It reads no sensor itself: you feed it numbers from an MPU6050 or any other IMU.

Open the example in the editor See the calls

Built in on Mokxi: #include <MadgwickAHRS.h> and it builds, with nothing to install. Free, and no account needed.

## What Madgwick does

The filter needs to know how often you will call it, given in hertz to begin(), and you must then call it at that rate. Call it faster or slower than you said and the angles lag or overshoot, because the gyro readings are integrated over the time step you promised.

Units matter too: updateIMU() takes rotation in degrees per second and acceleration in any consistent unit, usually g. Yaw from accelerometer and gyro alone drifts slowly, since gravity says nothing about heading; update() with a magnetometer’s three axes fixes that.

Library Manager name | Madgwick

Author | Arduino

Include | #include <MadgwickAHRS.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Accelerometer and gyroscope, MPU6050

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <MadgwickAHRS.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Madgwick" by Arduino from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Madgwick to lib_deps in PlatformIO. Mokxi follows the calls of release 1.2.0, so a sketch written here builds there too.

ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### Madgwick filter

Creates the filter.

### filter.begin(25)

Tells the filter it will be updated 25 times a second.

### filter.updateIMU(gx, gy, gz, ax, ay, az)

One step with gyro readings in degrees per second and accelerometer readings in g.

### filter.update(gx, gy, gz, ax, ay, az, mx, my, mz)

The same step with a magnetometer’s readings, which holds yaw steady.

### filter.getRoll()

Roll in degrees. getRollRadians() gives radians.

### filter.getPitch()

Pitch in degrees.

### filter.getYaw()

Yaw (heading) in degrees, 0 to 360. Without a magnetometer it drifts over minutes.

## Example: Roll from an MPU6050 through Madgwick’s filter

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

### Wiring

- Arduino Uno R3 A4 to Accelerometer and gyroscope, MPU6050 SDA
- Arduino Uno R3 A5 to Accelerometer and gyroscope, MPU6050 SCL
- Arduino Uno R3 5V to Accelerometer and gyroscope, MPU6050 VCC
- Arduino Uno R3 GND to Accelerometer and gyroscope, MPU6050 GND

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### The angles move far too slowly or swing past the real tilt

The rate passed to begin() does not match how often updateIMU() is called. Time the calls with micros() as the example does, and give begin() the same number.

### Roll and pitch jump to large values when the board is still

The gyro values are raw counts or radians. Convert raw MPU6050 counts to degrees per second (divide by 131 at the default range) before passing them in.

### Yaw creeps round by itself

That is expected with only an accelerometer and gyro, which cannot see heading. Calibrate the gyro offsets to slow it, or add a magnetometer and call update() with its readings.

## Keep going

### Learn

- Arduino MPU6050: Accelerometer and Tilt

### Parts

- Accelerometer and gyroscope, MPU6050

### More data and time libraries

- QRCode
- PID
- TimerOne
- MsTimer2
- Low-Power
- RTClib
- DS1307RTC
- DS3231 (NorthernWidget) and DS3231 (Rinky-Dink Electronics)

Every built-in library · Open the editor

## Try Madgwick without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
