Lesson 13 – Compass

Python Concepts


Teacher Materials

Get Access

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

This picture shows that the compass is in the micro:bit in the tail of the Finch.

Calibrating the Compass

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 Finch around in different directions to calibrate it. You should see a green check.

This picture shows the location of the compass button in the BlueBird Connector.

Using the Compass

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 Finch must be level 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 Finch is between 75° and 105° (i.e., pointing within 15° of 90°). 

while not(bird.getButton('A')): # While button A isn't pressed 
if bird.getCompass() < 75 and bird.getCompass() < 105: # If both Booleans are true 
bird.print('E') # Print E 
bird.print('') # Print nothing

Exercise 1

Write a function that returns a string that indicates the direction that the Finch is facing. The function should return the following directions: N, NE, E, SE, S, SW, W, NW. Any compass heading within ±15° of a given direction should return that direction; other headings should return an empty string. For example, angles between 75° and 105° should return ‘E’, while angles between 120° and 150° should return ‘SE’. Test your function by printing the direction on the micro:bit screen as you slowly turn the Finch. Remember to include a pause so that your robot has time to print two letters to the micro:bit screen.

Exercise 2

Write a program that makes the Finch always drive north. This problem is similar to line tracking. When the robot is facing west of north, it should turn right. When it is facing east of north, it should turn left.

Exercise 3

Modify your code from Exercise 2 so that the Finch can also avoid obstacles. If there is no obstacle, the Finch should head north. If there is an obstacle, the Finch should back up and turn right.


Suppose that you want the Finch to play a different note for each direction. One way to do this would be to use an if-elif-else statement and include a case for each direction. However, you can make your code more efficient using a dictionary. A dictionary is similar to a list, except that every element consists of a key and a value. Instead of accessing a value using its index (as you did with lists), you access a value by placing its key inside square brackets. For example, this code defines a dictionary sampleDictionary with three entries. Each entry has a string key that is related to a note value. The playNote() method accesses note 64 using the key ‘e’.

sampleDictionary = {'c':60, 'd': 62, 'e':64} # Dictionary with two entries 

When using a dictionary, you often want to check if a given key is in the dictionary. You can do this using the in keyword. For example, this code will not play a note because the key ‘a’ is not in the dictionary.

note = 'a' 
if note in sampleDictionary: 

Exercise 4

Create a dictionary that contains eight notes. The key for each note should be one of the directions returned by your function from Exercise 1. Write a program that uses the dictionary to make the Finch play notes as you turn it in different directions. Remember that your direction function sometimes returns the empty string. The program should not play a note when it receives the empty string.

Dictionaries can also contain values that are lists. For example, each key in this dictionary corresponds to a list of three elements.

# Dictionary of lists 
directionColor = {'N':[100,0,0], 'NE': [0,100,0], 'E':[0,0,100]}

Exercise 5

Create a dictionary named directionColor that contains eight lists with three elements. Each list should represent a color. The key for each color should be one of the directions returned by your function from Exercise 1. Use this dictionary to modify your code from Exercise 3 so that the beak and tail of the Finch change to a different color for each note.

Hint: You can access the elements of each list using a second set of square brackets. For example, this code accesses the first element of the list that corresponds to key.

Back to Top