Skip to content

Commit 22b4c24

Browse files
committed
adding inclinometer example
1 parent 6964303 commit 22b4c24

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

examples/lsm303_inclinometer.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
""" Display inclination data five times per second """
2+
3+
import time
4+
from math import atan2, degrees
5+
import board
6+
import busio
7+
import adafruit_mpu6050
8+
9+
i2c = busio.I2C(board.SCL, board.SDA)
10+
sensor = adafruit_mpu6050.MPU6050(i2c)
11+
12+
13+
def vector_2_degrees(x, y):
14+
angle = degrees(atan2(y, x))
15+
if angle < 0:
16+
angle += 360
17+
return angle
18+
19+
20+
def get_inclination(_sensor):
21+
x, y, z = _sensor.acceleration
22+
return vector_2_degrees(x, z), vector_2_degrees(y, z)
23+
24+
25+
while True:
26+
angle_xz, angle_yz = get_inclination(sensor)
27+
print("XZ angle = {:6.2f}deg YZ angle = {:6.2f}deg".format(angle_xz, angle_yz))
28+
time.sleep(0.2)

0 commit comments

Comments
 (0)