ArduinoArduino UnoExact chip

Tilt (MPU6050)

Acceleration and rotation over I2C, read with Wire. Tilt it in the inspector.

Tilt (MPU6050) in Argynix Lab

The code

sketch.inoC++
// MPU6050 over I2C (address 0x68): wake it, then read acceleration (g) and rotation (°/s).
#include <Wire.h>

const int MPU = 0x68;

int16_t next16() {
  int16_t hi = Wire.read();
  int16_t lo = Wire.read();
  return (hi << 8) | lo;
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission();
}

void loop() {
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 14);
  float ax = next16() / 16384.0;
  float ay = next16() / 16384.0;
  float az = next16() / 16384.0;
  next16();
  float gx = next16() / 131.0;
  Serial.print("ax:");
  Serial.print(ax, 2);
  Serial.print(",ay:");
  Serial.print(ay, 2);
  Serial.print(",az:");
  Serial.print(az, 2);
  Serial.print(",gx:");
  Serial.println(gx, 1);
  delay(200);
}