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
5
6import board
7
8import adafruit_mpu6050
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12mpu = adafruit_mpu6050.MPU6050(i2c)
13
14while True:
15 print(
16 f"Acceleration: X:{mpu.acceleration[0]:.2f}, Y: {mpu.acceleration[1]:.2f}, Z: {mpu.acceleration[2]:.2f} m/s^2" # noqa: E501
17 )
18 print(f"Gyro X:{mpu.gyro[0]:.2f}, Y: {mpu.gyro[1]:.2f}, Z: {mpu.gyro[2]:.2f} rad/s")
19 print(f"Temperature: {mpu.temperature:.2f} C")
20 print("")
21 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
5
6import board
7
8import adafruit_mpu6050
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12mpu = adafruit_mpu6050.MPU6050(i2c)
13mpu.accelerometer_range = adafruit_mpu6050.Range.RANGE_2_G
14mpu.gyro_range = adafruit_mpu6050.GyroRange.RANGE_250_DPS
15
16while True:
17 # this prints out all the values like a tuple which Mu's plotter prefer
18 print(
19 f"({mpu.acceleration[0]:.2f}, {mpu.acceleration[1]:.2f}, {mpu.acceleration[2]:.2f} ",
20 end=", ",
21 )
22 print(f"{mpu.gyro[0]:.2f}, {mpu.gyro[1]:.2f}, {mpu.gyro[2]:.2f})")
23 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
5
6import board
7
8import adafruit_mpu6050
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12mpu = adafruit_mpu6050.MPU6050(i2c)
13
14# This example is meant to be used with the serial plotter which makes
15# it easier to see how the readings change with different settings.
16# Make sure to poke and prod the sensor while the demo is running to
17# generate some interesting data!
18
19while True:
20 # first show some 'normal' readings
21
22 mpu.sleep = False
23 mpu.cycle = False
24
25 for count in range(0, 100):
26 print(mpu.acceleration)
27 time.sleep(0.010)
28
29 # Next, set a slow cycle rate so the effect can be seen clearly.
30 mpu.cycle_Rate = adafruit_mpu6050.Rate.CYCLE_5_HZ
31 # ensure that we're not sleeping or cycle won't work
32 mpu.sleep = False
33 # Finally, enable cycle mode
34 mpu.cycle = True
35
36 for count in range(0, 100):
37 print(mpu.acceleration)
38 time.sleep(0.010)
39
40 # Finally enable sleep mode. Note that while we can still fetch
41 # data from the measurement registers, the measurements are not
42 # updated. In sleep mode the accelerometer and gyroscope are
43 # deactivated to save power, so measurements are halted.
44
45 mpu.cycle = False
46 mpu.sleep = True
47
48 for count in range(0, 100):
49 print(mpu.acceleration)
50 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
11
12import board
13
14import adafruit_mpu6050
15
16i2c = board.I2C() # uses board.SCL and board.SDA
17# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
18sensor = adafruit_mpu6050.MPU6050(i2c)
19
20
21# Given a point (x, y) return the angle of that point relative to x axis.
22# Returns: angle in degrees
23
24
25def vector_2_degrees(x, y):
26 angle = degrees(atan2(y, x))
27 if angle < 0:
28 angle += 360
29 return angle
30
31
32# Given an accelerometer sensor object return the inclination angles of X/Z and Y/Z
33# Returns: tuple containing the two angles in degrees
34
35
36def get_inclination(_sensor):
37 x, y, z = _sensor.acceleration
38 return vector_2_degrees(x, z), vector_2_degrees(y, z)
39
40
41while True:
42 angle_xz, angle_yz = get_inclination(sensor)
43 print(f"XZ angle = {angle_xz:6.2f}deg YZ angle = {angle_yz:6.2f}deg")
44 time.sleep(0.2)
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/mpu6050_displayio_simpletest.py
1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3#
4# SPDX-License-Identifier: MIT
5
6import time
7
8import board
9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_mpu6050
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C() # uses board.SCL and board.SDA
20mpu = adafruit_mpu6050.MPU6050(i2c)
21
22
23# Create Label(s) to show the readings. If you have a very small
24# display you may need to change to scale=1.
25display_output_acceleration = Label(FONT, text="", scale=1)
26display_output_gyro = Label(FONT, text="", scale=1)
27display_output_temperature = Label(FONT, text="", scale=1)
28
29# place the label(s) in the middle of the screen with anchored positioning
30display_output_acceleration.anchor_point = (0, 0)
31display_output_acceleration.anchored_position = (
32 4,
33 board.DISPLAY.height // 2 - 60,
34)
35display_output_gyro.anchor_point = (0, 0)
36display_output_gyro.anchored_position = (
37 4,
38 board.DISPLAY.height // 2 - 40,
39)
40display_output_temperature.anchor_point = (0, 0)
41display_output_temperature.anchored_position = (
42 4,
43 board.DISPLAY.height // 2 - 20,
44)
45
46
47# add the label(s) to the main_group
48main_group.append(display_output_acceleration)
49main_group.append(display_output_gyro)
50main_group.append(display_output_temperature)
51
52# set the main_group as the root_group of the built-in DISPLAY
53board.DISPLAY.root_group = main_group
54
55# begin main loop
56while True:
57 # update the text of the label(s) to show the sensor readings
58 accx, accy, accz = mpu.acceleration
59 gyro_x, gyro_y, gyro_z = mpu.gyro
60 display_output_acceleration.text = f"Acc X:{accx:.1f} Y:{accy:.1f} Z:{accz:.1f} m/s^2"
61 display_output_gyro.text = f"Gyro X:{gyro_x:.1f} Y:{gyro_y:.1f} Z:{gyro_z:.1f} rad/s"
62 display_output_temperature.text = f"Temp: {mpu.temperature:.1f} C"
63 # wait for a bit
64 time.sleep(0.5)