Lesson 8 – Finch Accelerometer

Python Concepts

If-elif-else statements

Teacher Materials

Get Access

The micro:bit contains an accelerometer. This sensor can be used to measure the orientation of the Finch. For example, you can detect when the Finch is upside down. The micro:bit accelerometer is very similar to the accelerometer that enables a smartphone or tablet to change the screen from portrait to landscape when the user turns the device.

Picture shows that the accelerometer is positioned in the micro:bit in the Finch tail.

Finding Finch Orientation

Use the getOrientation() method to find the position of the Finch. This method does not require any parameters. It returns a string that represents the orientation of the Finch. The possible values are “Beak up”, “Beak down”, “Tilt left”, “Tilt right”, “Level”, “Upside down”, and “In between”. “In between” is any orientation that is not one of the other six positions.

Picture shows the orientations of the Finch: "Beak down" is with the beak pointing at the ground; "Beak up" is with the beak pointing at the ceiling; "Tilt left" is with the Finch's left wing pointing at the ground; "Tilt right" is with the Finch's right wing pointing at the ground; "Level" is with the wheels on the ground; and "Upside down" is with the wheels pointing to the ceiling.

For example, this code checks whether the left wing of the Finch is down. If this is true, then the beak turns on. Otherwise, the beak is off.

while not bird.getButton('A'): # While button A isn't pressed 
if bird.getOrientation() == "Tilt left": # If Finch tilted left 
bird.setBeak(0,100,0) 
else: # Otherwise 
bird.setBeak(0,0,100) 
bird.stopAll()

Exercise 1

Program the Finch to turn in a wide arc until you pick up the Finch and turn it upside down.

If-elif-else Statements

When using the accelerometer, you often want to make your robot respond to multiple orientations of the accelerometer. In this case, you need an if-elif-else statement. The if-elif-else statement checks multiple Boolean expressions. For example, in this code, the first if checks whether the Finch is tilted to the left. If it is, the beak turns green. If it isn’t, then the program checks whether the Finch is tilted to the right. If it is, the beak turns purple. If the Finch isn’t tilted to the right or the left, the beak turns blue. You can add as many elifs as you want to check different Boolean expressions!

while not bird.getButton('A'): # While button A isn't pressed 
if bird.getOrientation() == "Tilt left": # If Finch tilted left 
bird.setBeak(0,100,0) 
elif bird.getOrientation() == "Tilt right": # If Finch tilted right 
bird.setBeak(100,0,100) 
else: # Otherwise 
bird.setBeak(0,0,100) 
bird.stopAll()

Exercise 2

Try out the code above. Then modify it to add other colors when the Finch is in other positions. How many colors can you add?

micro:bit V2 Extra Challenge

If your Finch contains a micro:bit V2, you can also use a temperature sensor in the micro:bit to measure the temperature around the Finch in degrees Celsius. The getTemperature() method returns the value of this sensor. Room temperature is about 20°C. If you don’t have a micro:bit V2 in your Finch, you will see an error when you use this method.

Write a program that uses the Finch’s lights to tell you when the Finch is too hot, too cold, or just right.

Using the Accelerometer

The getOrientation() method calculates the position of the Finch based on measurements taken by the accelerometer in the micro:bit. The accelerometer measures any force that causes acceleration in the x-, y-, or z-direction. Because the Finch is usually moving pretty slowly, the accelerometer mostly measures the acceleration due to gravity. When the Finch is sitting on a level surface, the acceleration due to gravity points through the base of the Finch, along the negative z-axis. If you instead position the Finch so that its beak is pointing at the sky, the acceleration due to gravity points along the positive y -axis. The accelerometer enables you to measure these differences.

The getAcceleration() method returns a three float values that measure the acceleration in m/s2 in the x-, y-, and z-directions; these values are roughly between -10 and 10. The code below stores the three values in variables with informative names.

accelX, accelY, accelZ = bird.getAcceleration()

Exercise 3

Write a program that repeatedly prints the x-, y-, and z-values for acceleration to the screen. Remember to read new acceleration values within the loop. The program should stop when the Finch detects that it is dark. Test your program by tilting the Finch in different directions. When is the z-acceleration close to 10? How can you detect that the Finch’s left wing is pointing at the ground?

Exercise 4

Write a program that uses the x-, y-, and z-values for acceleration to set the amount of red, green, and blue in the Finch’s beak and tail. The program should run until you press button A.

Exercise 5

Turn your Finch into a timer. Allow the user to input a number of seconds. When that time is up, the Finch should do whatever is necessary to get the user’s attention. The Finch should continue to act until the user shakes the Finch with an acceleration that has a magnitude greater than 20 m/s2 squared.

Note: Remember that the acceleration is each direction is between -10 and 10 m/s2 squared. You will need to combine the acceleration measurements in each direction to find the total magnitude of the acceleration: a = math.sqrt(ax*ax+ ay*ay + az*az). You will need to import the math library.

Exercise 6

In this exercise, you will make the Finch climb a hill. You will need a board or table that you can tilt to make your “hill.” It should be tilted about 15°.

  • Start by making your robot turn VERY SLOWLY until the x-acceleration is close to 0. When this part of your program is finished, the Finch should be pointed either uphill or downhill.
  • Now your Finch is ready to drive, but you have to figure out whether it is pointed uphill or downhill. Which acceleration measurement can you use to determine that?
  • If your Finch is pointed uphill, drive forward up the hill. If your Finch is pointed downhill, drive backwards up the hill instead!
Back to Top