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.
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolMeasure a stationary MPU6050 offset, remove it without erasing gravity, and distinguish a controlled motion input from rest.
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.
sketch.ino
- Use the launch button above to open the ESP32 editor in Wokwi.
- Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
- Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.
Steps
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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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.
What physically moves inside a MEMS capacitive accelerometer when the device accelerates?
Return to the chapter’s knowledge checkAn 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 checkWhy can a single MEMS accelerometer not distinguish a slow tilt from a genuine horizontal acceleration?
Return to the chapter’s knowledge check
Return to IMU Motion and Barometric Calibration Limits · Browse Labs