Skip to content

Calibrate an MPU6050 motion offset

Measure a stationary MPU6050 offset, remove it without erasing gravity, and distinguish a controlled motion input from rest.

Physics Phoebe: I want you to preserve gravity as evidence while separating repeatable offset from real motion., your practice guide

Physics Phoebe: I want you to preserve gravity as evidence while separating repeatable offset from real motion.
Predict the reading, then compare it with the measurement.

Wokwi ESP32

Third party Tool

Measure a stationary MPU6050 offset, remove it without erasing gravity, and distinguish a controlled motion input from rest.

Tier 1 · Web · No account

Version tested: Compiled and booted by the Wokwi gate (ESP32 Arduino core 3.3.11); captured in the Wokwi web editor on 2026-09-09. Date: 2026-09-09.

Open the ESP32 editor, paste diagram.json, then paste sketch.ino.

Open Wokwi to paste in the files (new tab)

Get the files

Use both prepared files. This is a paste-in setup; saving a project requires a Wokwi account.

diagram.json

575 bytes · Circuit connections

Download

sketch.ino

2,436 bytes · ESP32 program

Download

  1. Use the launch button above to open the ESP32 editor in Wokwi.
  2. Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
  3. Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.

Steps

Screens captured against Wokwi ESP32 Compiled and booted by the Wokwi gate (ESP32 Arduino core 3.3.11); captured in the Wokwi web editor on 2026-09-09 on 2026-09-09; the tool may have moved on — the text steps are the contract.

  1. 1 Step 1

    Do
    Paste diagram.json and sketch.ino into a new Wokwi ESP32 project, then trace the I2C bus.
    You will see
    The MPU6050 uses GPIO21 for SDA, GPIO22 for SCL, 3.3 V, and a common ground.
    Why it matters
    A valid offset trace begins with a known bus and power boundary.
    Step 1: The MPU6050 connects to ESP32 GPIO21 and GPIO22 over I2C. The ring marks the IMU.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 Step 2

    Do
    Inspect the counts-per-g conversion, visible teaching bias, and stationary calibration loop.
    You will see
    The sketch uses 16,384 counts per g, averages 12 stationary samples, and subtracts one g only from the Z-axis offset estimate.
    Why it matters
    Gravity is a reference, not an error; removing it during calibration would corrupt later tilt and motion evidence.
    Step 2: The code converts 16384 counts per g and estimates offsets while preserving one g on Z. The ring marks the calibration logic.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 Step 3

    Do
    Start the simulation and inspect the initialization line.
    You will see
    The sensor at address 0x68 acknowledges with the plus-or-minus 2 g scale.
    Why it matters
    An ACK and explicit scale bind the following register counts to a physical unit.
    Step 3: IMU START ack=0 range=2g scale=16384 The ring marks the acknowledged configuration.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 Step 4

    Do
    Keep the simulated board stationary and read the first uncorrected sample.
    You will see
    The visible teaching model reports 0.050 g, -0.020 g, and 1.020 g before calibration.
    Why it matters
    Keeping the before-state makes the correction auditable instead of hiding it inside a library call.
    Step 4: RAW rest ax=0.050 ay=-0.020 az=1.020 The ring marks the uncorrected stationary sample.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 Step 5

    Do
    Read the calibration summary and the next corrected rest sample.
    You will see
    Offsets of 0.050, -0.020, and 0.020 g produce corrected acceleration of 0, 0, and 1 g.
    Why it matters
    The corrected rest vector retains gravity while removing the modeled sensor and mounting offsets.
    Step 5: CAL offset ax=0.050 ay=-0.020 az=0.020 samples=12 IMU raw=0.050,-0.020,1.020 corrected=0.000,0.000,1.000 magnitude=1.000 state=REST The ring marks the calibrated rest result.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 Step 6

    Do
    Select the MPU6050 and set X acceleration to 0.5 g.
    You will see
    Corrected X becomes 0.500 g, magnitude becomes about 1.118 g, and state changes to MOTION.
    Why it matters
    A magnitude threshold tests motion while remaining independent of which axis was changed.
    Step 6: corrected=0.500,0.000,1.000 magnitude=1.118 state=MOTION The ring marks the controlled motion result.
    Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  7. 7 Step 7

    Do
    Restore X acceleration to 0 g.
    You will see
    The corrected vector returns to 0, 0, 1 g with magnitude 1.000 and state REST.
    Why it matters
    Returning to the reference pose checks repeatability without claiming that one pose calibrates every orientation.
    Step 7: corrected=0.000,0.000,1.000 magnitude=1.000 state=REST The ring marks the restored reference pose.
    Step 7 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)

Chapter checks

These questions refer to the chapter’s examples. Use the return links to review their answers.

  1. What physically moves inside a MEMS capacitive accelerometer when the device accelerates?

    Return to the chapter’s knowledge check
  2. An ADXL335 outputs 300 mV/g with 0 g at 1.5 V. Its axis reads 1.20 V while stationary. What is happening?

    Return to the chapter’s knowledge check
  3. Why can a single MEMS accelerometer not distinguish a slow tilt from a genuine horizontal acceleration?

    Return to the chapter’s knowledge check

Caution

The added teaching bias is declared in source and is not a measured MPU6050 defect. On hardware, keep the unit motionless during calibration, characterize temperature and orientation dependence, preserve gravity, verify the full operating range, and do not treat one startup pose as a production calibration.

Return to IMU Motion and Barometric Calibration Limits · Browse Labs