deckle studioIm Editor öffnen

Arduino Uno: Kugel-Wasserwaage (MPU6050 + LED-Matrix)

MPU6050-Neigung rollt Kugel auf LED-Matrix.

  • Arduino Uno
  • MPU-6050
  • MAX7219
Im Simulator öffnenOhne Konto: Simulieren drücken, und es läuft.

So funktioniert es

Der Bewegungssensor MPU-6050 (GY-521, I²C) misst die Erdbeschleunigung; eine 2×2-„Kugel“ rollt auf der 8×8-LED-Matrix (MAX7219, LedControl-Bibliothek) zur tiefsten Stelle. Neigung über Beschleunigung X / Y im Inspektor einstellen (z. B. 0,4 g), auch während die Simulation läuft.

Verdrahtung

Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.

NetzPins
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

Bauteile

Firmware

sketch.ino · 26 Zeilen

// 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);
}
Im Simulator öffnen

Weitere Beispiele: Arduino