Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

microbit compass in micropython

I have a microbit project where the microbit is inserted into a kiktronics robot vertically.

I would like to get the heading of the robot, but the

compass.heading()

only works if the microbit is horizontal. I have tried reading the x,y,z co-ordinates of the compass using get_x(), get_y(), get_z()

But the ranges of numbers I am getting are scaled differently for the z axis and the x,y axis.

Does anyone know what the ranges are for the different sensors?

like image 682
mksteve Avatar asked Jan 17 '26 09:01

mksteve


1 Answers

I used the test code below. I can get an accurate compass reading if I run the compass_calibrate() function first, even with the magnetometer vertical.

After commenting out the compass_calibrate() line, when moving the board around 3 axis in free space, I can see that the z value does not vary as much as x and y. So I got a small magnet. Moving that around the magnetometer makes the x,y,z values appear to change within roughly the same limits - this is a rough eyeball experiment.

Looking at the data sheet for the MAG3110 magnetometer, I can't see any indication that the 3 magnetometer axis are different. So why are the z-readings different without an external field? I hypothesise that there is a ground plane in the PCB. This is common in PCB construction. This could be acting as a shield for the z-axis.

from microbit import *

# compass.calibrate()
while True:
    sleep(250)
    # c = compass.heading()
    x = compass.get_x()
    y = compass.get_y()
    z = compass.get_z()
    print('x:{} y:{} z: {}'.format(x,y,z))
like image 197
Oppy Avatar answered Jan 21 '26 03:01

Oppy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!