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