Getting Started with MicroPython and the SparkFun Inventor's Kit for micro:bit

Pages
Contributors: LightningHawk

Experiment 11: Using the Compass (Magnetometer)

Introduction

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!

Parts Needed

You will need the following parts:

• 1x micro:bit
• 1x Micro B USB Cable
• 1x micro:bit Breakout (with Headers)
• 4x Jumper Wires
• 2x LEDs
• 2x 100Ω Resistors

Didn't Get the SIK for micro:bit?

If you are conducting this experiment and didn't get the Inventor's Kit, we suggest using these parts:

DEV-14208
\$14.95
6

PRT-12615
\$5.95
22

BOB-13989
\$5.95
3

PRT-11026
\$1.95
20

COM-09590
\$0.35

Resistor 100 Ohm 1/4 Watt PTH - 20 pack (Thick Leads)

PRT-14493
\$1.20

Before continuing with this experiment, we recommend you be familiar with the concepts in the following tutorial:

Introducing the Compass (Magnetometer)

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 micro:bit has an onboard compass (a.k.a. a magnetometer) that can detect its orientation using Earth's magnetic field. Depending on the version that you have, the accelerometer and compass can be on separate ICs or combined into a single IC.

 v1.0 w/ Magnetometer On Separate IC v1.5 w/ Combined Accelerometer and Magnetometer

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!

Hardware Hookup

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.

Wiring Diagram for the Experiment

Having a hard time seeing the circuit? Click on the wiring diagram for a closer look.

Note: The full sized breadboard power rails have a break down the middle. If you end up using the lower half of the power rail you will need to jump between the upper end and lower end.

``````language:python
# SparkFun Electronics
# Experiment 11
# Using the Compass (Magnetometer)

from microbit import *

compass.calibrate()
while True:
sleep(10)

#bearing is the degrees
if ( bearing > 10 and bearing < 180 ):
pin0.write_digital(0)
pin16.write_digital(1)
#bearing is the degrees
elif ( bearing > 180 and bearing < 350 ):
pin0.write_digital(1)
pin16.write_digital(0)
#bearing is the degrees
else:
pin0.write_digital(1)
pin16.write_digital(1)
``````

Code to Note

Let's take a look at the code in this experiment.

The `compass heading` function 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 `bearing`.

and

Much of the rest of this code is straightforward, but the logical `and` operator is used. This combines two logical statements into one statement that returns true when both of the other statements are true and only true.

What You Should See

When your code is loaded you will first see instructions on the micro:bit LED array. The instructions will ask you to tilt the micro:bit around until all of the LEDs on the micro:bit face have been lit up. Once this has been done, it will display 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!

Note: If you calibrate your micro:bit while inside and in a proximity to your computer or something that produces a larger magnetic field it will skew your micro:bits sense of direction. You will end up creating a metal detector rather than a compass!

Troubleshooting

Not Really North?

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.

LEDs Seem Backward

You might have your logical statement backward! Try flipping your greater than to less than, or the other way around.

In 2003, CU student Nate Seidle fried a power supply in his dorm room and, in lieu of a way to order easy replacements, decided to start his own company. Since then, SparkFun has been committed to sustainably helping our world achieve electronics literacy from our headquarters in Boulder, Colorado.

No matter your vision, SparkFun's products and resources are designed to make the world of electronics more accessible. In addition to over 2,000 open source components and widgets, SparkFun offers curriculum, training and online tutorials designed to help demystify the wonderful world of embedded electronics. We're here to help you start something.