3

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?

mksteve
  • 12,614
  • 3
  • 28
  • 50

2 Answers2

2

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))
Oppy
  • 2,662
  • 16
  • 22
0

For those who are working with micro:bit v2, you can get the compass readings by using compass.heading() function too.

Code example:

from microbit import *

while True:
    if button_a.was_pressed():
        display.scroll(str(compass.heading()))

At the first time after running this code, the micro:bit board will ask you to tilt the board in different directions, and then it will start providing you with the required directions.

According to your questions about the compass ranges, here is a quick summarization for them as in the image below:

enter image description here

Meqdad Dev
  • 131
  • 1
  • 2
  • 10