Simple test¶
Ensure your device works with this simple test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_mpu6050
i2c = board.I2C() # uses board.SCL and 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 rad/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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_mpu6050
i2c = board.I2C() # uses board.SCL and 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
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 46 47 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_mpu6050
i2c = board.I2C() # uses board.SCL and 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 interesting 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)
|
Inclinometer Example¶
Provides an example on how to use the sensor as an inclinometer
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 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Display inclination data five times per second
# See this page to learn the math and physics principals behind this example:
# https://learn.adafruit.com/how-tall-is-it/gravity-and-acceleration
import time
from math import atan2, degrees
import board
import adafruit_mpu6050
i2c = board.I2C() # uses board.SCL and board.SDA
sensor = adafruit_mpu6050.MPU6050(i2c)
# Given a point (x, y) return the angle of that point relative to x axis.
# Returns: angle in degrees
def vector_2_degrees(x, y):
angle = degrees(atan2(y, x))
if angle < 0:
angle += 360
return angle
# Given an accelerometer sensor object return the inclination angles of X/Z and Y/Z
# Returns: tuple containing the two angles in degrees
def get_inclination(_sensor):
x, y, z = _sensor.acceleration
return vector_2_degrees(x, z), vector_2_degrees(y, z)
while True:
angle_xz, angle_yz = get_inclination(sensor)
print("XZ angle = {:6.2f}deg YZ angle = {:6.2f}deg".format(angle_xz, angle_yz))
time.sleep(0.2)
|