Skip to content

Commit 63111d4

Browse files
authored
Merge pull request #5 from FoamyGuy/master
adding inclinometer example
2 parents 0540df6 + d7a9ea7 commit 63111d4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

examples/mpu6050_inclinometer.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
def vector_2_degrees(x, y):
20+
angle = degrees(atan2(y, x))
21+
if angle < 0:
22+
angle += 360
23+
return angle
24+
25+
26+
# Given an accelerometer sensor object return the inclination angles of X/Z and Y/Z
27+
# Returns: tuple containing the two angles in degrees
28+
29+
def get_inclination(_sensor):
30+
x, y, z = _sensor.acceleration
31+
return vector_2_degrees(x, z), vector_2_degrees(y, z)
32+
33+
34+
while True:
35+
angle_xz, angle_yz = get_inclination(sensor)
36+
print("XZ angle = {:6.2f}deg YZ angle = {:6.2f}deg".format(angle_xz, angle_yz))
37+
time.sleep(0.2)

0 commit comments

Comments
 (0)