3. case 01:Compass#
3.1. Our Goal#
Use watch kit to make a portable compass with colorful LED indicators.
1 x Watch kit
3.3. Background Knowledge#
What is compass?#
Compass was called Sinan in chinese ancient times. Its major component is a magnetic pin installed on a shaft. The magnetic pin can rotate freely under the effect of the natural magnetic field and maintain in the tangential direction of the magnetic meridian. The north pole of the magnetic pin points to the geographical south pole. We can use this feature to recognise directions.
micro:bit has a compass module that performs the function of compass. When the compass points to the north, the return value is 0, i.e. the return value is increased to 360 in the clockwise direction. We can judge directions according to their return values.
3.4. Hardware Connection#
Plug the LED ring into power:bit like the picture below.
We need to initialize the compass every time before we use it. The initialization is a little game. After lightening a circle on micro:bit LED screen, a smile face will appear to tell you that the initialization is completed.
Advancedin the code drawer of MakeCode to see more options.
To program for the LED ring, we have to add a package. Find
Add Packagein the bottom of code drawer and click on it. This will pop up a dialogue box. Search for “nexpixel” and then click to download this package.
Note: If you get a hint that some packages will be deleted due to the problem of incompatibility, you can either follow the prompts, or create a new project in the project menu.
on startblock from
Basicand insert it into
calibrate compassblock. This will initialize the compass module on micro:bit.
set toblock and place it under a
Create a variable item and set it to return value of the compass.
Because the range of return value is 0-360 and there is 8 beads on LED ring, so we divide 360 degree into 8 section. The range of north direction is from 337 degrees to 22 degrees. It is gradually increasing.
When pointing to the north, the 5th LED becomes red, while micro:bit is giving a sound prompt.
Link of the whole program: https://makecode.microbit.org/_cj1EUmFJH6At
You can also download the whole program from the page below.
When pointing to the north, the 5th LED becomes red and we can hear a sound prompt from micro:bit.
Q: Why the pointing direction is not fixed every time when we use it?
A: The compass direction is just an approximate direction. It is relative to the result of compass calibration.