Skip to content

Read an MPU6050 over I2C

Read acceleration on three axes using an I2C register transaction.

Physics Phoebe, your practice guide

Physics Phoebe
Predict the reading, then compare it with the measurement.

Wokwi ESP32

Third party Tool

Read acceleration on three axes using an I2C register transaction.

Tier 2 · Web · paste-in setup · Account needed to save a project

Version tested: Compiled with arduino-cli 1.5.2-rc.1 for esp32 core 3.3.11; booted headlessly with wokwi-cli 0.26.1 on 2026-09-07; serial line 'Ax=0.000 g, Ay=0.000 g, Az=1.000 g' seen at 7.268 s (wall time from CLI start; scenario timeout 20 s simulated time); captured in the Wokwi web editor on 2026-09-09. Date: 2026-09-06.

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

871 bytes · Circuit connections

Download

sketch.ino

600 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 with arduino-cli 1.5.2-rc.1 for esp32 core 3.3.11; booted headlessly with wokwi-cli 0.26.1 on 2026-09-07; serial line 'Ax=0.000 g, Ay=0.000 g, Az=1.000 g' seen at 7.268 s (wall time from CLI start; scenario timeout 20 s simulated time); captured in the Wokwi web editor on 2026-09-09 on 2026-09-06; the tool may have moved on — the text steps are the contract.

  1. 1 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: The sensor power, ground, and signal wiring connect to the ESP32. The ring marks the sensor.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 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: The named source lines show the sampling and conversion calculations. The ring marks those code lines.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 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: The simulation is running and Serial Monitor is open. The ring marks the Stop control.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 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: Stationary acceleration is 0, 0, and 1 g. The ring marks the three axes.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 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: X acceleration changes to 0.500 g while Z remains 1.000 g. The ring marks the axes.
    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 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: Z acceleration changes to 0.000 g. The ring marks the axes.
    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 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: The baseline 0, 0, and 1 g readings return. The ring marks the restored axes.
    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. Why can a single MEMS accelerometer not distinguish a slow tilt from a genuine horizontal acceleration?

    Return to the chapter’s knowledge check

Caution

Tool versions change and screens may differ. Reopen the supplied setup, check the tool documentation, and use the site feedback control if the problem remains. The screenshots show browser measurements, not hardware calibration; values may vary slightly between runs.

Return to Protocol Diagnostics and Bus Design · Browse Labs