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
compass.calibrate()
while True:
sleep(100)
val = compass.heading()
if (val < 10 or val > 350):
# the micro:bit is pointing North
display.show('N')
else:
display.show(Image.NO)
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:
Post a Comment