Read an MPU6050 over I2C
Read acceleration on three axes using an I2C register transaction.

Physics Phoebe
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolRead acceleration on three axes using an I2C register transaction.
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
- In the Simulation panel, inspect the MPU6050 wiring after pasting both supplied files.
- You will see
- The MPU6050 has power and ground connections, with SDA on ESP32 pin 21 and SCL on pin 22.
- Why it matters
- The MPU6050 combines an accelerometer and gyroscope on one I2C chip. Following its wires connects the motion readings to the interface carrying them.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- In the sketch.ino editor tab, find the address 0x68 and the acceleration divisions by 16384.0.
- You will see
- The code requests six acceleration bytes and divides each signed axis value by 16384.0 to print g units.
- Why it matters
- The chapter maps 1 g to 16,384 counts at the ±2 g setting. Keeping the range and scale together prevents a wrong conversion.

Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 3
- Do
- In the Simulation toolbar, click the play button and wait for compilation.
- You will see
- The timer starts and the Serial Monitor prints Wake status=0 (0 means ACK).
- Why it matters
- The startup acknowledgement checks communication before you interpret motion. It does not establish the bias, scale, or calibration of a physical sensor.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 4
- Do
- In the Serial Monitor panel, compare Ax, Ay, and Az with the expected stationary readings.
- You will see
- The Serial Monitor shows Ax=0.000 g, Ay=0.000 g, Az=1.000 g about every 500 ms.
- Why it matters
- At rest, horizontal axes read near zero while the vertical axis shows gravity. This is the chapter’s starting prediction for orientation checks.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- In the Simulation panel, select the MPU6050 and set X acceleration to 0.5 g.
- You will see
- The next readings show Ax=0.500 g, Ay=0.000 g, Az=1.000 g.
- Why it matters
- Changing one axis tests whether its label matches the reported value. The chapter asks you to predict the sign before reading the sensor.

Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 6
- Do
- In the MPU6050 control panel, set Z acceleration to 0 g.
- You will see
- The next readings show Ax=0.500 g, Ay=0.000 g, Az=0.000 g.
- Why it matters
- These controls supply model inputs rather than a physical board movement. A real motion test must compare the readings with the action actually made.

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 and Z acceleration to 1 g.
- You will see
- The Serial Monitor returns to Ax=0.000 g, Ay=0.000 g, Az=1.000 g.
- Why it matters
- Returning to the still state gives a repeat check. The chapter also repeats this test after power loss to reveal an old offset.

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 checkWhy can a single MEMS accelerometer not distinguish a slow tilt from a genuine horizontal acceleration?
Return to the chapter’s knowledge check