Arduino library 路 Sensors

Adafruit MPU6050: motion in real units

Adafruit MPU6050 is the friendly route to the GY-521 module. Instead of raw counts it gives you physical units through Adafruit鈥檚 Unified Sensor events: acceleration in meters per second squared, rotation in radians per second and the die temperature in degrees Celsius, filled into three sensors_event_t structures by one getEvent() call.

Built in on Mokxi: #include <Adafruit_MPU6050.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit MPU6050 does

Before reading, you set the ranges and the digital low-pass filter with names rather than register values: MPU6050_RANGE_8_G, MPU6050_RANGE_500_DEG, MPU6050_BAND_21_HZ. A narrower filter smooths vibration out of the readings at the cost of response time, which suits a tilt sensor more than a drum pad.

Remember the units when you compare with other tutorials. One g is about 9.81 m/s虏, so a board lying flat reads close to 9.8 on Z, and a rotation of 1 rad/s is about 57 degrees per second. The chip鈥檚 motion-detection interrupt can be configured, but in Mokxi it is never raised.

Library Manager nameAdafruit MPU6050
AuthorAdafruit
Include#include <Adafruit_MPU6050.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesAccelerometer and gyroscope, MPU6050

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_MPU6050.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit MPU6050" by Adafruit from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit MPU6050 to lib_deps in PlatformIO. Mokxi follows the calls of release 2.2.6, so a sketch written here builds there too.

What to know in the simulator: The motion-detection interrupt is written to the chip but never raised.

ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

Adafruit_MPU6050 mpu

Creates the sensor object.

mpu.begin()

Starts I2C and checks the chip at 0x68. Returns false when it is not found; begin(0x69) for a board with AD0 high.

mpu.setAccelerometerRange(MPU6050_RANGE_8_G)

Plus or minus 2, 4, 8 or 16 g. A wider range reads harder knocks with less resolution.

mpu.setGyroRange(MPU6050_RANGE_500_DEG)

Plus or minus 250, 500, 1000 or 2000 degrees per second.

mpu.setFilterBandwidth(MPU6050_BAND_21_HZ)

The low-pass filter, from 260 Hz down to 5 Hz.

mpu.getEvent(&a, &g, &temp)

Reads the chip once and fills three events.

sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.print(a.acceleration.x);
Serial.print(" ");
Serial.println(g.gyro.z);

temp.temperature

The chip鈥檚 own temperature in degrees Celsius. It runs a few degrees above the room, so it is not a room thermometer.

Example: Tilt an MPU6050

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino 路 Arduino Uno R3
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup() {
  Serial.begin(115200);
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) delay(10);
  }
  Serial.println("MPU6050 Found!");
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");
  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");
  delay(500);
}

Wiring

  • Arduino Uno R3 A4 to Accelerometer and gyroscope, MPU6050 SDA
  • Arduino Uno R3 A5 to Accelerometer and gyroscope, MPU6050 SCL
  • Arduino Uno R3 5V to Accelerometer and gyroscope, MPU6050 VCC
  • Arduino Uno R3 GND to Accelerometer and gyroscope, MPU6050 GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

Failed to find MPU6050 chip

begin() returned false. Check the I2C wires and the address (0x68, or 0x69 with AD0 high). Some modules sold as MPU6050 carry an MPU6500 or a clone with another ID, which the Adafruit library rejects on a real board.

Acceleration on Z reads about 9.8 and you expected 1

The library reports meters per second squared, not g. Divide by 9.81 (SENSORS_GRAVITY_STANDARD) if your code or a tutorial expects g.

fatal error: Adafruit_Sensor.h: No such file or directory

The library depends on Adafruit Unified Sensor and Adafruit BusIO. On a real board install both from the Library Manager, then include Adafruit_Sensor.h as the examples do.

Try Adafruit MPU6050 without installing anything

Open the example, change the sketch, and keep your version in a free account.