pxt-calliope/docs/reference/input/compass-heading.md
2016-03-25 16:47:20 -07:00

1.6 KiB

Compass Heading

Get the compass heading of the micro:bit in degrees. Your micro:bit has a built-in magnetometer so it can your direction with respect to the North Magnetic Pole.

input.compassHeading();

Returns

  • Number - the heading in degrees (0 to 360 degrees). If the compass is calibrating, it returns -1003.

Simulator

Calibration does not work on the simulator.

Example

The following code gets the compass heading and stores it in the degrees variable:

let degrees = input.compassHeading()

~hint

When running code with this function in a web browser, click and drag the on-screen compass needle to change heading.

~

Example: compass

The following example gets the compass heading and then displays a letter depending on the value of degrees: N for north, E for East, S for South, and W for West.

basic.forever(() => {
    let degrees = input.compassHeading()
    if (degrees < 45)
        basic.showString("N")
    else if (degrees < 135)
        basic.showString("E")
    else if (degrees < 225)
        basic.showString("S")
    else basic.showString("W")
})

Calibration

On the first use of the compass, the calibration procedure will automatically start. The user must draw a circle with the device until it is fully calibrated.

An enclosure made from metal, or using in proximity of metal objects, might affect the accuracy of the reading and calibration.

During calibration, compass heading returns -1003.

Lessons

compass

See also

acceleration