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

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
    In the Wokwi simulation panel, paste diagram.json and sketch.ino into a new Wokwi ESP32 project, then trace the I2C bus.
    You will see
    Input: inspect diagram.json beside the running Wokwi IMU. The MPU6050 uses GPIO 21 for SDA and GPIO 22 for SCL. Its power lead reaches 3.3 V and ground is shared. Serial Monitor shows corrected=0.000,0.000,1.000 at REST.
    Why it matters
    The I2C and power paths are visible in the captured tool. The REST line belongs to this wired simulated board. The corrected Z axis still preserves one g. A known bus is needed before interpreting offsets.
    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
    In the Wokwi simulation panel, inspect the counts-per-g conversion, visible teaching bias, and stationary calibration loop.
    You will see
    Input: inspect sketch.ino while the Wokwi simulation runs. The source sets COUNTS_PER_G=16384.0 and CALIBRATION_SAMPLES=12. Visible teaching bias is X=0.050, Y=-0.020, Z=0.020. Serial Monitor shows raw=0.050,-0.020,1.020 and corrected=0.000,0.000,1.000.
    Why it matters
    Twelve stationary samples form the offset estimate. The corrected Z reading preserves gravity at one g. The raw and corrected tuples are both visible in this run. Subtracting gravity itself would corrupt later 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 Wokwi and inspect the initialization line in Serial Monitor.
    You will see
    Input: boot the supplied Wokwi ESP32 and MPU6050 diagram. Observed: "IMU START ack=0 range=2g scale=16384". Expected: an acknowledged 2 g range and 16384 counts per g. The run then printed a stationary raw sample.
    Why it matters
    ACK=0 shows the simulated I2C setup responded. The scale states how raw counts become g units. These settings precede the calibration loop. The serial label is not a physical range test.
    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 in Serial Monitor.
    You will see
    Input: keep the simulated board stationary. Observed: "RAW rest ax=0.050 ay=-0.020 az=1.020". Expected: the visible teaching bias shifts the rest reading. The screenshot shows the same three values.
    Why it matters
    X and Y are offset from zero at rest. Z is 0.020 g above the ideal one-g baseline. This raw line supplies the calibration input. The offsets are deliberate firmware teaching values.
    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 corrected rest sample in Serial Monitor.
    You will see
    Input: continue through 12 stationary calibration samples. Observed: "CAL offset ax=0.050 ay=-0.020 az=0.020 samples=12". Observed: "IMU raw=0.050,-0.020,1.020 corrected=0.000,0.000,1.000 magnitude=1.000 state=REST". Expected: correction returns the rest pose to 0, 0, 1 g.
    Why it matters
    The offset summary reflects the 12-sample fixture. Subtracting it removes the visible teaching bias. The corrected magnitude is one g in this run. A fixed rest calibration does not cover changing physical bias.
    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
    In the MPU6050 control panel, set X acceleration to 0.5 g.
    You will see
    Input: set X acceleration to 0.5 g in the MPU6050 panel. Observed: "IMU raw=0.550,-0.020,1.020 corrected=0.500,0.000,1.000 magnitude=1.118 state=MOTION". Expected: corrected X rises to 0.500 g and state changes to MOTION. The panel control read X=0.5 g.
    Why it matters
    The calibration offset remains applied during the change. The vector magnitude rises from 1.000 to 1.118. The state transition is based on that controlled input. The simulated change is not a measured real gesture.
    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
    In the MPU6050 control panel, restore X acceleration to 0 g.
    You will see
    Input: restore X acceleration to 0 g in the MPU6050 panel. Observed: "IMU raw=0.050,-0.020,1.020 corrected=0.000,0.000,1.000 magnitude=1.000 state=REST". Expected: corrected rest values return. The panel control read X=0 again.
    Why it matters
    The final corrected vector matches the earlier baseline. The state returns from MOTION to REST. The unchanged Y/Z values help isolate the X change. This verifies the simulator sequence only.
    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