Embedded systems · Independent
STM32 Accelerometer Bubble Level
A tilt-responsive LED display that switches between a moving bubble and a horizon line using accelerometer readings.
- C
- ADXL345
- SPI
- I2C
Goal
Connect an ADXL345 accelerometer to an STM32F411 and an 8×8 bicolor LED matrix, translating acceleration readings into a bubble-level or horizon view.
My Role
I wrote the register-level SPI sensor API and I2C display API, the conversion and display-mapping math, and the state machine that chooses which view to draw.
How It Works
Translate raw sensor bytes
Six bytes are decoded into three signed 16-bit axis values. My implementation uses a scale factor of 0.004 g/count to convert those values into acceleration units.
Map gravity onto a small display
X/Y acceleration positions a bounded 2×2 bubble. The horizon calculation draws a line perpendicular to gravity, with near-zero-axis handling to avoid division by zero.
Switch views in a state machine
A two-state, function-pointer state machine switches at configured ±0.707 g axis thresholds. A 16-byte pixel buffer holds the image sent to the LED matrix.
Testing & Debugging
Mode switching worked on hardware, but the horizon initially moved in the wrong direction and disappeared at some angles. I plotted the line equation in Desmos, worked through the math, and corrected the display-mapping functions so the horizon followed accelerometer tilt.
Result
On the hardware, the display switched automatically between bubble and horizon modes, and the corrected horizon followed the board’s tilt.