## Lesson 11 – micro:bit Compass

#### Hummingbird Components

1 Tri-color LED, 1 Rotation Servo

#### Python Concepts

Compound boolean statements

#### Teacher Materials

In this lesson, you will learn to use the compass that is on the micro:bit. The compass tells you the direction of your micro:bit relative to magnetic north.

Before you use the compass, you need to calibrate it in the BlueBird Connector.  To do this, click on the purple compass button next to the name of your device. Follow along with the video to move your Bit around in different directions to calibrate it. You should see a green check when you have successfully calibrated. If you see a red X, try again.

Once you have successfully calibrated, you can use the getCompass() method to read the value of the compass. This method requires no parameters and returns the value of the compass from 0° and 359°. 0° corresponds to the direction of magnetic north. The angle increases as the robot turns clockwise, so 90° is east, 180° is south, and 270° is west. To use the compass, the micro:bit must be positioned so that the LED display is parallel to the ground. Otherwise, the compass will not provide useful measurements.

As an example, suppose you want to display ‘E’ on the micro:bit screen when the angle of the micro:bit is between 75° and 105° (i.e., pointing within 15° of 90°). The code below will do this. Notice that you can use the and keyword in Python to combine two Boolean statements. When you use and to combine two Boolean statements, the resulting compound Boolean statement is true only if both of the Boolean comparisons are true. In this case, the program will only print ‘E’ if the compass value is both greater than 75° and less than 105°.

#### Exercise 1

Try out this code (remember to calibrate the compass first). Rotate your micro:bit slowly to find out which direction is east. Then modify your program so that the micro:bit displays the letter ‘S’ when the compass is between 165° and 195°. The program should continue to display ‘E’ when the micro:bit is pointing east.

#### Exercise 2

Modify your program so that the micro:bit screen displays ‘W’ when the micro:bit is pointing west.

#### Exercise 3

Modify your program so that the micro:bit screen displays ‘N’ when the compass reading is greater than 345° or less that 15°. Note that you will need the Python keyword or. If you combine two Boolean comparisons with or, the result is true if either or both of the comparisons are true. Now you can display all four directions!

#### Exercise 4

Modify your program so that the tri-color LED and the rotation servo behave differently for each of the four directions. The LED and rotation servo should be off when the micro:bit is not pointing north, south, east, or west.

#### Extra Challenge

Write a program that meets the following requirements:

• When the compass reading is larger than 190°, the rotation servo should turn very slowly at a positive speed.
• When the compass reading is less than 170°, the rotation servo should turn very slowly at a negative speed.
• The rotation servo should be off when the compass reading is between 170° and 190°.

Next, tape the servo horn of the rotation servo to the bottom of the Hummingbird Bit. Place the horn on the servo and hold on to the rotation servo while you run your program. You program should make the Bit move so that the compass points toward the south. If you turn your hand to point the compass in a different direction, it will turn back toward the south.