<link rel="stylesheet" href="/fonts/devicon/devicon.css" data-beasties-skip>

MPU-6050 Attitude Monitor

Bare-metal Artificial Horizon on STM32L031K6

Role
Embedded firmware engineer
Year
2026
Status
Prototype
Stack
C · STM32L031K6 · I2C · Bare-metal · SSD1306 · MPU-6050 · Keil µVision
MCU
STM32L031K6 · Cortex-M0+
Buses
2 × I²C
Framebuffer
1 KB
Frame rate
≈5–7 fps

Overview

Bare-metal STM32L031K6 firmware that renders an MPU-6050 accelerometer/gyro on a 128×64 SSD1306 OLED as an aircraft-style artificial horizon. No HAL, no CubeMX: direct register access, two independent I²C buses (hardware and bit-banged), and a boot diagnostic ladder that pinpoints wiring faults instead of just reporting "not found".

What I built

  1. Direct register access throughout: no HAL, no CubeMX
  2. Two independent I²C buses: hardware I²C1 for the OLED, bit-banged for the sensor
  3. Boot diagnostic ladder: OLED self-test, DC pin test, bus scans, then init or a failure screen that says why
  4. Bus-scan result feeds the driver, so the sensor is used at whichever address answers
  5. Roll and pitch derived from gravity via atan2, drift-free; yaw left out rather than shown drifting
  6. Artificial horizon, roll scale and acceleration gauge rendered into a 1 KB framebuffer
  7. WHO_AM_I not whitelisted: GY-521 clone dies (0x70–0x98) accepted; only a NACK means absent
  8. DC pin test proves wire and module power with no I²C traffic, using the internal pull-down against the breakout pull-ups

Key decisions

MCU
STM32L031K6 · Cortex-M0+
Buses
2 × I²C
Framebuffer
1 KB
Frame rate
≈5–7 fps

Stack

C · STM32L031K6 · I2C · Bare-metal · SSD1306 · MPU-6050 · Keil µVision

Outcome

≈5–7 fps artificial horizon from a 1 KB framebuffer on a Cortex-M0+. Prototype · 2026.

Next case — 07 / 10

OV5640 Camera Capture DCMI + DMA Image Pipeline on STM32F767ZI · 2026 All work