This experiment is just plain fun! Have you ever used a compass? Are you a little confused in terms of which direction to go? Fear not! We will build a digital compass that will keep you on track to the North Pole using the micro:bit’s onboard compass chip!
You will need the following parts:
In the previous experiment you learned about the accelerometer, which measured gravity. The compass, or technically the magnetometer, measures a magnetic field. Magnetic fields come in different sizes, but the biggest is the one produced by Earth itself, which is why compasses work.
The magnetometer detects magnetic north and then represents your heading in degrees with north being 0 degrees, east being 90 degrees, south being 180 degrees and west being 270. Pretty cool! Now let’s put this compass to good use!
Ready to start hooking everything up? Check out the wiring diagram below to see how everything is connected.
Polarized Components | Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction. |
Red LED Module | Micro Bit Breakout |
GND | GND |
VCC | 3.3v |
OUT | P0 |
Blue LED Module | Micro Bit Breakout |
GND | GND |
VCC | 3.3v |
OUT | P16 |
Either copy and paste, or re-create the following code into your own MakeCode editor by clicking the open icon in the upper right-hand corner of the editor window. You can also just download this example by clicking the download button in the lower right-hand corner of the code window.
Let’s take a look at the code blocks in this experiment.
If you are having a hard time viewing this code, click on the image above to get a better look!
The compass heading
block returns the heading that you are facing if you are holding the micro:bit flat with the pins toward you. Zero degrees is north. We store this heading in a variable called item
.
Much of the rest of this code is straightforward, but the logical and
block is used. This combines two logical statements into one statement that returns true when both of the other statements are true and only true.
When your code is loaded you will first see instructions on the micro:bit LED array. The instructions will ask you to draw a circle, move the micro:bit around until all of the part of the circle have been added and it displays a smiley face. This process is to calibrate the micro:bit’s magnetometer with its surroundings. The LEDs will start to turn on — one or the other, or both. While standing still, rotate in the direction of the LED that is on. When both LEDs are lit, you are facing generally north (if you are outside)! You now have a compass that helps you find north, or any other direction you choose if you change the code!
You may have a motor or magnet close by! The magnetometer is sensitive to all magnetic fields, including ones that may be produced by other electronics, metal, or even…a magnet.
You might have your logical statement backward! Try flipping your greater than to less than, or the other way around.
DownLoad Url osoyoo.com