We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 10a49d0 commit 62f1412Copy full SHA for 62f1412
examples/lis2mdl_compass.py
@@ -0,0 +1,26 @@
1
+""" Display compass heading data five times per second """
2
+import time
3
+from math import atan2, degrees
4
+import board
5
+import busio
6
+import adafruit_lis2mdl
7
+
8
+i2c = busio.I2C(board.SCL, board.SDA)
9
+sensor = adafruit_lis2mdl.LIS2MDL(i2c)
10
11
12
+def vector_2_degrees(x, y):
13
+ angle = degrees(atan2(y, x))
14
+ if angle < 0:
15
+ angle += 360
16
+ return angle
17
18
19
+def get_heading(_sensor):
20
+ magnet_x, magnet_y, _ = _sensor.magnetic
21
+ return vector_2_degrees(magnet_x, magnet_y)
22
23
24
+while True:
25
+ print("heading: {:.2f} degrees".format(get_heading(sensor)))
26
+ time.sleep(0.2)
0 commit comments