Simple test

Ensure your device works with this simple test.

examples/mpu6050_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import time
import board
import busio
import adafruit_mpu6050

i2c = busio.I2C(board.SCL, board.SDA)
mpu = adafruit_mpu6050.MPU6050(i2c)

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)

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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import time
import board
import busio
import adafruit_mpu6050

i2c = busio.I2C(board.SCL, board.SDA)
mpu = adafruit_mpu6050.MPU6050(i2c)
mpu.accelerometer_range = adafruit_mpu6050.Range.RANGE_2_G
mpu.gyro_range = adafruit_mpu6050.GyroRange.RANGE_250_DPS
while True:
    # this prints out all the values like a tuple which Mu's plotter prefer
    print("(%.2f, %.2f, %.2f "%(mpu.acceleration), end=", ")
    print("%.2f, %.2f, %.2f)"%(mpu.gyro))
    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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import time
import board
import busio
import adafruit_mpu6050

i2c = busio.I2C(board.SCL, board.SDA)
mpu = adafruit_mpu6050.MPU6050(i2c)

# This example is meant to be used with the serial plotter which makes
# it easier to see how the readings change with different settings.
# Make sure to poke and prod the sensor while the demo is running to
# generate some intersting data!

while True:
    # first show some 'normal' readings

    mpu.sleep = False
    mpu.cycle = False

    for count in range(0, 100):
        print(mpu.acceleration)
        time.sleep(0.010)

    # Next, set a slow cycle rate so the effect can be seen clearly.
    mpu.cycle_Rate = adafruit_mpu6050.Rate.CYCLE_5_HZ
    # ensure that we're not sleeping or cycle won't work
    mpu.sleep = False
    # Finally, enable cycle mode
    mpu.cycle = True

    for count in range(0, 100):
        print(mpu.acceleration)
        time.sleep(0.010)

    # Finally enable sleep mode. Note that while we can still fetch
    #  data from the measurement registers, the measurements are not
    #  updated. In sleep mode the accelerometer and gyroscope are
    #  deactivated to save power, so measurements are halted.

    mpu.cycle = False
    mpu.sleep = True

    for count in range(0, 100):
        print(mpu.acceleration)
        time.sleep(0.010)