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
 9mpu = adafruit_mpu6050.MPU6050(i2c)
10
11while True:
12    print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (mpu.acceleration))
13    print("Gyro X:%.2f, Y: %.2f, Z: %.2f rad/s" % (mpu.gyro))
14    print("Temperature: %.2f C" % mpu.temperature)
15    print("")
16    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
 9mpu = adafruit_mpu6050.MPU6050(i2c)
10mpu.accelerometer_range = adafruit_mpu6050.Range.RANGE_2_G
11mpu.gyro_range = adafruit_mpu6050.GyroRange.RANGE_250_DPS
12
13while True:
14    # this prints out all the values like a tuple which Mu's plotter prefer
15    print("(%.2f, %.2f, %.2f " % (mpu.acceleration), end=", ")
16    print("%.2f, %.2f, %.2f)" % (mpu.gyro))
17    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
 9mpu = adafruit_mpu6050.MPU6050(i2c)
10
11# This example is meant to be used with the serial plotter which makes
12# it easier to see how the readings change with different settings.
13# Make sure to poke and prod the sensor while the demo is running to
14# generate some interesting data!
15
16while True:
17    # first show some 'normal' readings
18
19    mpu.sleep = False
20    mpu.cycle = False
21
22    for count in range(0, 100):
23        print(mpu.acceleration)
24        time.sleep(0.010)
25
26    # Next, set a slow cycle rate so the effect can be seen clearly.
27    mpu.cycle_Rate = adafruit_mpu6050.Rate.CYCLE_5_HZ
28    # ensure that we're not sleeping or cycle won't work
29    mpu.sleep = False
30    # Finally, enable cycle mode
31    mpu.cycle = True
32
33    for count in range(0, 100):
34        print(mpu.acceleration)
35        time.sleep(0.010)
36
37    # Finally enable sleep mode. Note that while we can still fetch
38    #  data from the measurement registers, the measurements are not
39    #  updated. In sleep mode the accelerometer and gyroscope are
40    #  deactivated to save power, so measurements are halted.
41
42    mpu.cycle = False
43    mpu.sleep = True
44
45    for count in range(0, 100):
46        print(mpu.acceleration)
47        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
15sensor = adafruit_mpu6050.MPU6050(i2c)
16
17
18# Given a point (x, y) return the angle of that point relative to x axis.
19# Returns: angle in degrees
20
21
22def vector_2_degrees(x, y):
23    angle = degrees(atan2(y, x))
24    if angle < 0:
25        angle += 360
26    return angle
27
28
29# Given an accelerometer sensor object return the inclination angles of X/Z and Y/Z
30# Returns: tuple containing the two angles in degrees
31
32
33def get_inclination(_sensor):
34    x, y, z = _sensor.acceleration
35    return vector_2_degrees(x, z), vector_2_degrees(y, z)
36
37
38while True:
39    angle_xz, angle_yz = get_inclination(sensor)
40    print("XZ angle = {:6.2f}deg   YZ angle = {:6.2f}deg".format(angle_xz, angle_yz))
41    time.sleep(0.2)