deckle studioOpen in the editor

Arduino Uno: ball spirit level (MPU6050 + LED matrix)

MPU6050 tilt rolls a ball on an LED matrix.

  • Arduino Uno
  • MPU-6050
  • MAX7219
Open in the simulatorNo account needed: press Simulate and it runs.

How it works

The MPU-6050 motion sensor (GY-521, I²C) measures gravity; a 2×2 "ball" rolls to the lowest point on the 8×8 LED matrix (MAX7219, LedControl library). Set the tilt with acceleration X / Y in the inspector (for example 0.4 g), even while the simulation runs.

Wiring

Each line is one connection (net) and the pins it joins.

NetPins
GNDA1 GNDDS1 GNDU2 GNDV1 −
5VA1 5VDS1 VCCU2 VCCV1 +
CSA1 ~D10/SSDS1 CS
DINA1 D12/MISODS1 DIN
MCLKA1 ~D11/MOSIDS1 CLK
SCLA1 A5/SCLU2 SCL
SDAA1 A4/SDAU2 SDA

Parts

Firmware

sketch.ino · 26 lines

// MPU6050 (I²C, A4/A5) measures the tilt, a 2×2 "ball" rolls on the 8×8 matrix (MAX7219:
// DIN D12, CLK D11, CS D10) to wherever it goes downhill
#include <Adafruit_MPU6050.h>
#include <LedControl.h>

Adafruit_MPU6050 mpu;
LedControl matrix(12, 11, 10, 1);

void setup() {
  Serial.begin(115200);
  matrix.shutdown(0, false);       // the MAX7219 is in shutdown after power-on
  matrix.setIntensity(0, 6);
  matrix.clearDisplay(0);
  if (!mpu.begin()) Serial.println(F("MPU6050 missing"));
  mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
}

void loop() {
  sensors_event_t a, g, t;
  mpu.getEvent(&a, &g, &t);
  int x = constrain((int)round(3 + a.acceleration.x / 9.81 * 6), 0, 6);
  int y = constrain((int)round(3 - a.acceleration.y / 9.81 * 6), 0, 6);
  for (int row = 0; row < 8; row++) matrix.setRow(0, row, (row == y || row == y + 1) ? (0xC0 >> x) : 0);
  Serial.print(F("ax ")); Serial.print(a.acceleration.x, 2); Serial.print(F("  ay ")); Serial.println(a.acceleration.y, 2);
  delay(50);
}
Open in the simulator

More examples: Arduino