Home › Lessons › Embedded (ESP32) › I²C sensor: accelerometer tilt

I²C sensor: accelerometer tilt

Embedded (ESP32) · Intermediate · about 20 minutes · part of Zen’s Electronics Design Mastery

An accelerometer at rest measures gravity. How the 1 g splits across its x, y and z axes tells you how it is tilted. The ESP32 reads six bytes from the MPU-6050’s registers over I²C and turns them into pitch and roll.

I²C sensor: accelerometer tilt: the circuit on the simulator's bench

Open this lesson in the labFree, in your browser, no account needed.

How it works

ax = −sin(pitch), ay = sin(roll)·cos(pitch), az = cos(roll)·cos(pitch), in g.

pitch = atan2(−ax, √(ay² + az²)), roll = atan2(ay, az)

Writing 0 to register 0x6B wakes the chip; it starts in sleep mode and returns zeros until then. Data starts at 0x3B as big-endian 16-bit two’s-complement values.

Try it on the bench

  1. Select the IMU and drag its roll and pitch sliders: the sketch follows.
  2. Edit the sketch to delete the wake-up write to 0x6B: everything reads 0, a classic first-day bug.
  3. Swap SDA and SCL: Wire stops finding the device (endTransmission returns 2).

Parts on the bench

In the course

Zen’s Electronics Design Mastery, module “Microcontrollers & Embedded”.

More on embedded (esp32)