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 ToolOpen 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
- 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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