/* jquery */ /* jquery accordion style*/ /* jquery init */

29 May 2017

Magnetometer Compass (micro:bit MicroPython)

This time we'll take advantage of the micro:bit's magnetometer sensor (which detects magnetic fields) to build a North-finding compass app.

from microbit import *

# draw a circle calibration

while True:
   val = compass.heading()
   if (val < 10 or val > 350):
      # the micro:bit is pointing North

Note that the magnetometer needs to be calibrated before we use it. This is done by tipping the micro:bit around to draw a circle on the LEDs. Be aware that sometimes the magnetometer calibration can be affected by the magnetic fields emanating from an electronic device, such as a computer.

As a coding challenge you could try to add compass directions for South, East and West.

Return to the micro:bit MicroPython Coding Tutorials page.

No comments: