Source: https://mokxi.com/libraries/mpu6050-tockn
Updated: 2026-11-11

Libraries / Sensors

Arduino library · Sensors

# MPU6050_tockn: tilt angles in three calls

MPU6050_tockn, by the maker who goes by tockn, is a short path from an MPU-6050 to angles in degrees. It does not expose the chip’s many settings. begin() fixes the gyroscope at ±500 °/s and the accelerometer at ±2 g, and every update() blends the two into angleX, angleY and angleZ with a complementary filter: 98 % integrated gyro and 2 % accelerometer, unless the constructor says otherwise.

Open the example in the editor See the calls

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

## What MPU6050_tockn does

Before the angles mean much, calcGyroOffsets() averages 3000 gyro readings with the sensor still and subtracts that bias from then on. By default it pauses one second before and three seconds after; given true it prints "Calculating gyro offsets" and "DO NOT MOVE MPU6050" to the serial monitor. The example below passes 0 for both pauses so it starts at once.

Its class is called MPU6050, the same name MPU6050_light and i2cdevlib use, so only one of them can be in a sketch and code written for one will not compile against another. getAngleZ() comes from the gyro alone and slowly drifts, since a six-axis chip has nothing to correct heading with. begin() returns nothing, so it cannot tell you the chip is missing. In Mokxi, tilt the mpu6050 part on the canvas and the angles follow.

Library Manager name | MPU6050_tockn

Author | tockn

Include | #include <MPU6050_tockn.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 <MPU6050_tockn.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "MPU6050_tockn" by tockn from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add MPU6050_tockn to lib_deps in PlatformIO. Mokxi follows the calls of release 1.5.2, 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
- WCH CH32V003
- 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.

### MPU6050 mpu6050(Wire)

Creates the driver on a TwoWire bus. Two optional numbers set the filter: MPU6050 mpu6050(Wire, 0.1, 0.9) leans more on the accelerometer.

### mpu6050.begin()

Sets the ranges, wakes the chip and seeds the angles from the accelerometer. It has no return value.

### mpu6050.calcGyroOffsets(true, 0, 0)

Measures the gyro bias over 3000 readings and prints the offsets; the two zeros skip the default one-second and three-second pauses.

### mpu6050.setGyroOffsets(-1.45, 0.62, 0.11)

Uses offsets saved from an earlier run instead of measuring at every start. getGyroXoffset() and its Y and Z twins read the current ones.

### mpu6050.update()

Reads the fourteen data bytes and advances the filter. Call it every pass of loop(), since the gyro term integrates over the time between calls.

### mpu6050.getAngleX()

Roll in degrees from the filter. getAngleY() is pitch, and getAngleZ() is yaw from the gyro alone.

### mpu6050.getAccAngleX()

The angle from the accelerometer only, noisy but without drift. getGyroAngleX() is the gyro only; plotting both beside getAngleX() shows what the filter does.

### mpu6050.getTemp()

The chip’s own temperature in degrees Celsius.

## Example: Tilt angles with MPU6050_tockn

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 stay at 0 or nan and never change

begin() does not check for the chip, so a missing or miswired MPU6050 fails without a message. Run an I2C scanner: the library talks only to 0x68, so AD0 must be low.

### getAngleZ() creeps away while the board sits still

Yaw is the gyro integrated over time, so any leftover bias keeps adding up. Rerun calcGyroOffsets() with the board perfectly still; a correct heading needs a magnetometer as well.

### redefinition of 'class MPU6050'

Two MPU6050 libraries are included and each defines a class of that name. Keep only #include <MPU6050_tockn.h> and remove MPU6050.h or MPU6050_light.h.

### The sketch sits silent for several seconds after reset

calcGyroOffsets() waits one second before measuring and three after by default. Pass calcGyroOffsets(true, 0, 0) to drop the waits, as long as the sensor is already still.

## Questions and answers

### Can I change the MPU6050_tockn ranges?

Not through its calls. begin() always sets ±500 °/s and ±2 g; writeMPU6050() can write a register directly if you know the datasheet values.

### What do the two numbers in the constructor do?

They are the accelerometer and gyro weights of the filter, 0.02 and 0.98 by default. A larger accelerometer weight corrects drift faster but lets vibration through.

## Keep going

### Learn

- Arduino MPU6050: Accelerometer and Tilt
- Arduino Self-Balancing Robot: MPU6050 and PID Code

### Parts

- Accelerometer and gyroscope, MPU6050

### More sensors libraries

- DHT sensor library
- DHT20
- VL53L0X
- Adafruit_VL53L0X
- HX711 Arduino Library
- MPU6050
- Adafruit MPU6050
- BME280

Every built-in library · Open the editor

## Try MPU6050_tockn without installing anything

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

Start building free

Open the editor
