BBC micro:bit
The Accelerometer

Introduction

The micro:bit module for MicroPython is a little more flexible to work with than the statements built into the other code editors. This means that, if your Python skills are up to it, you have lots of things that you can do with the readings. You can capture gestures as well as take the raw accelerometer readings.

Raw Readings

Press the REPL button in Mu. When you do this, it should open a text box at the bottom of the screen. Ignore this for now and type the following program,

from microbit import *
while True:
    x = accelerometer.get_x()
    print(x)
    sleep(1000)

When you flash the program, you should see a reading of the accelerometer x axis being printed to the text box at the bottom of the Mu application. The one second delay is used so that you can see the readings, you will need to tilt to the left or right in one clear movement to be sure you are seeing the reading that corresponds to your movement.

You use the get_y and get_z() methods to find out the readings on the other axes.

Gestures

The accelerometer object gives you access to the following methods for detecting gestures,

accelerometer.is_gesture(name)returns True or False if the named gesture is currently being performed
accelerometer.was_gesture(name)returns True or False if the named gesture was carried out since last checked
accelerometer.get_gestures()returns a tuple containing the history of gestures performed
accelerometer.reset_gestures()resets the history of gestures

The following named gestures are defined for this object,

  • up
  • down
  • left
  • right
  • face up
  • face down
  • freefall
  • 3g
  • 6g
  • 8g
  • shake

Most of these names are self-explanatory. The 3g, 6g and 8g gestures refer to the amount of g force being applied to the device. Freefall is an interesting one - it should detect if the micro:bit is falling due to the force of gravity.

This example displays the direction of movement on the matrix,

from microbit import *
while True:
    if accelerometer.is_gesture("up"):
        display.show(Image.ARROW_S)
    elif accelerometer.is_gesture("right"):
        display.show(Image.ARROW_E)
    elif accelerometer.is_gesture("down"):
        display.show(Image.ARROW_N)
    elif accelerometer.is_gesture("left"):
        display.show(Image.ARROW_W)
    else:
        display.clear()
    sleep(20)

With this next program, you have to lean the micro:bit forwards and then backwards in order to make a smiling face appear. The first IF statement checks that there are at least 2 gestures in the history. The second one checks that the last one (-1) and the last but one (-2) are the ones we expect.

from microbit import *
while True:
   history = accelerometer.get_gestures()
   if len(history)>2:
       if history[-1]=="down" and history[-2]=="up":
            display.show(Image.HAPPY)
            sleep(5000)
   display.clear()

Challenges

  1. You can make an effective spirit level using the accelerometer. Think about the images you choose to display to report the amount of tilt.
  2. Use the shake gesture to trigger the roll of a die or the choice of a rock/paper/scissors image.
  3. Make the matrix randomly display either yes or no when it is shaken. Ask it any yes/no question and then shake to see what the micro:bit has to say to you.
  4. Use gestures to control a pixel moving around the matrix. You will have to look back through the matrix techniques on the other pages for help.
  5. A more complicated program would be a game of Simon Says using tilting to enter the pattern. You can use arrows to indicate the patterns. A buzzer brings this kind of game to life.
  6. Use the example to help you examine the gesture history in order to make a secret pass code for your micro:bit. Take care, the longer your pattern, the more likely it is that one of your movements accidentally conforms to a named gesture.