When i rotate the sensor on X/Y i get numbers that don't make sense. Right now a full rotation gives me seemingly random values between 100 and 120 degrees.
Simple python but i'm missing something here, any help would be appreciated
Code: Select all
import time
from math import atan2, degrees
import board
import adafruit_lis3mdl
i2c = board.I2C() # uses board.SCL and board.SDA
sensor = adafruit_lis3mdl.LIS3MDL(i2c)
def vector_2_degrees(x, y):
angle = degrees(atan2(y, x))
if angle < 0:
angle += 360
return angle
def get_heading(_sensor):
magnet_x, magnet_y, _ = _sensor.magnetic
return vector_2_degrees(magnet_x, magnet_y)
def run():
while True:
print("heading: {:.2f} degrees".format(get_heading(sensor)))
time.sleep(1.0)
if __name__ == '__main__':
run()
I was able to calibrate it but I also can't find a doc on how to apply these calibration numbers to the above code.
I also am getting the same results from this arduio - North is 115, East and West are both 104 etc
Code: Select all
void loop() {
lis3mdl.read(); // get X Y and Z data at once
sensors_event_t event;
lis3mdl.getEvent(&event);
float heading = (atan2(event.magnetic.y, event.magnetic.x) * 180) / M_PI;
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
Serial.println(heading);
delay(100);
Serial.println();
}