MPU6050 6-Axis Accelerometer and Gyro: Created by Bryan Siepert
MPU6050 6-Axis Accelerometer and Gyro: Created by Bryan Siepert
The MPU-6050 is a popular six axis accelerometer and gyroscope (gyro) that has all the info you need on how things
are shakin' and spinnin' . With six axes of sensing and 16-bit measurements, you'll have everything you need to give
your robot friend a sense of balance, using the MPU-6050 as its inner ear.
This combination of gyroscopes and accelerometers is commonly referred to as an Inertial Measurement Unit or IMU.
Not so long ago IMUs were the size of a breadbox (https://adafru.it/GEp)and cost upwards of $50,000! While you're
not going to be using it to guide your mars rocket, the MPU-6050 is several orders of magnitude smaller and a bargain
at a price three orders of magnitude less! Now you can add telemetry to your water rocket (https://adafru.it/GEq)(with
some waterproofing).
"What is a sensor without a driver?" you might say. To that I would reply "Who cares, we got some for you right here".
Be it Arduino, CircuitPython or Python on a computer (single board or even multi-board! (https://adafru.it/FWD)), we've
got you covered.
Power Pins
Vin - this is the power pin. Since the sensor chip uses 3 VDC, we have included a voltage regulator on board that
will take 3-5VDC and safely convert it down. To power the board, give it the same power as the logic level of
your microcontroller - e.g. for a 5V microcontroller like Arduino, use 5V
3Vo - this is the 3.3V output from the voltage regulator, you can grab up to 100mA from this if you like
GND - common ground for power and logic
Other Pins
INT -This is the interrupt pin. You can setup the MPU-6050 to pull this low when certain conditions are met such
as new measurement data being available. Consult the datasheet and register map (https://adafru.it/GEr) for
usage
AD0 - I2C Address pin. Pulling this pin high or bridging the solder jumper on the back will change the I2C
address from 0x68 to 0x69
FS, SCE, SDE, CLKIN - Pins for advanced users to connect the MPU-6050 to another sensor. Consult the
datasheet and register map (https://adafru.it/GEr) for usage
Library Installation
Once wired up, to start using the MPU-6050 you'll need to install the Adafruit_MPU6050 library (https://adafru.it/GEs).
The library is available through the Arduino library manager so we recommend taking that approach.
Then follow the same process for the Adafruit BusIO library.
One you've uploaded the sketch to your board open up the Serial Monitor (Tools->Serial Monitor) at 115200 baud. You
should see the acceleration, rotation measurements, and temperature being printed like so:
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
Serial.print("Accelerometer range set to: ");
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
Serial.println("");
delay(100);
}
void loop() {
Serial.print("Rotation X: ");
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);
Serial.println(" deg/s");
Serial.print("Temperature: ");
Serial.println("");
delay(500);
}
You can use this sensor with any CircuitPython microcontroller board or with a Linux single board computer that has
GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library (https://adafru.it/BSN).
First make sure you are running the latest version of Adafruit CircuitPython (https://adafru.it/Amd) for your board.
Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these
libraries from Adafruit's CircuitPython library bundle (https://adafru.it/ENC). Our CircuitPython starter guide has a great
page on how to install the library bundle (https://adafru.it/ABU).
For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from
the bundle:
adafruit_mpu6050.mpy
adafruit_bus_device
adafruit_register
Before continuing make sure your board's lib folder or root filesystem has the adafruit_mpu6050.mpy,
adafruit_bus_device, and adafruit_register files and folders copied over.
Next connect to the board's serial REPL (https://adafru.it/Awz)so you are at the CircuitPython >>> prompt.
Once that's done, from your command line run the following command:
If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use
CircuitPython on Python 2.x, it isn't supported!
Run the following code to import the necessary modules and initialize the I2C connection with the sensor:
import time
import board
import busio
import adafruit_mpu6050
Now you're ready to read values from the sensor using these properties:
For example, to print out the acceleration, gyro, and temperature values:
That's all there is to it! Go forth and imbue your robot friends with the gift of being able to maybe not fall down all the
time. Maybe.
Example Code
while True:
print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2"%(mpu.acceleration))
print("Gyro X:%.2f, Y: %.2f, Z: %.2f degrees/s"%(mpu.gyro))
print("Temperature: %.2f C"%mpu.temperature)
print("")
time.sleep(1)
Schematic
Fab Print