Simple test

Ensure your device works with this simple test.

examples/mpu6050_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import adafruit_mpu6050
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10mpu = adafruit_mpu6050.MPU6050(i2c)
11
12while True:
13    print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (mpu.acceleration))
14    print("Gyro X:%.2f, Y: %.2f, Z: %.2f rad/s" % (mpu.gyro))
15    print("Temperature: %.2f C" % mpu.temperature)
16    print("")
17    time.sleep(1)

Plotter Example

See the effects of changing the gyroscope and accelerometer range by viewing the data in a serial plotter

examples/mpu6050_plotter_example.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import adafruit_mpu6050
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10mpu = adafruit_mpu6050.MPU6050(i2c)
11mpu.accelerometer_range = adafruit_mpu6050.Range.RANGE_2_G
12mpu.gyro_range = adafruit_mpu6050.GyroRange.RANGE_250_DPS
13
14while True:
15    # this prints out all the values like a tuple which Mu's plotter prefer
16    print("(%.2f, %.2f, %.2f " % (mpu.acceleration), end=", ")
17    print("%.2f, %.2f, %.2f)" % (mpu.gyro))
18    time.sleep(0.010)

Sleep Example

Observe how the cycle and sleep modes effect measurements by viewing the data in a serial plotter

examples/mpu6050_sleep_example.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import adafruit_mpu6050
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10mpu = adafruit_mpu6050.MPU6050(i2c)
11
12# This example is meant to be used with the serial plotter which makes
13# it easier to see how the readings change with different settings.
14# Make sure to poke and prod the sensor while the demo is running to
15# generate some interesting data!
16
17while True:
18    # first show some 'normal' readings
19
20    mpu.sleep = False
21    mpu.cycle = False
22
23    for count in range(0, 100):
24        print(mpu.acceleration)
25        time.sleep(0.010)
26
27    # Next, set a slow cycle rate so the effect can be seen clearly.
28    mpu.cycle_Rate = adafruit_mpu6050.Rate.CYCLE_5_HZ
29    # ensure that we're not sleeping or cycle won't work
30    mpu.sleep = False
31    # Finally, enable cycle mode
32    mpu.cycle = True
33
34    for count in range(0, 100):
35        print(mpu.acceleration)
36        time.sleep(0.010)
37
38    # Finally enable sleep mode. Note that while we can still fetch
39    #  data from the measurement registers, the measurements are not
40    #  updated. In sleep mode the accelerometer and gyroscope are
41    #  deactivated to save power, so measurements are halted.
42
43    mpu.cycle = False
44    mpu.sleep = True
45
46    for count in range(0, 100):
47        print(mpu.acceleration)
48        time.sleep(0.010)

Inclinometer Example

Provides an example on how to use the sensor as an inclinometer

examples/mpu6050_inclinometer.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Display inclination data five times per second
 5
 6# See this page to learn the math and physics principals behind this example:
 7# https://learn.adafruit.com/how-tall-is-it/gravity-and-acceleration
 8
 9import time
10from math import atan2, degrees
11import board
12import adafruit_mpu6050
13
14i2c = board.I2C()  # uses board.SCL and board.SDA
15# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
16sensor = adafruit_mpu6050.MPU6050(i2c)
17
18
19# Given a point (x, y) return the angle of that point relative to x axis.
20# Returns: angle in degrees
21
22
23def vector_2_degrees(x, y):
24    angle = degrees(atan2(y, x))
25    if angle < 0:
26        angle += 360
27    return angle
28
29
30# Given an accelerometer sensor object return the inclination angles of X/Z and Y/Z
31# Returns: tuple containing the two angles in degrees
32
33
34def get_inclination(_sensor):
35    x, y, z = _sensor.acceleration
36    return vector_2_degrees(x, z), vector_2_degrees(y, z)
37
38
39while True:
40    angle_xz, angle_yz = get_inclination(sensor)
41    print("XZ angle = {:6.2f}deg   YZ angle = {:6.2f}deg".format(angle_xz, angle_yz))
42    time.sleep(0.2)