The accelerometer measures movement along three axes:
• X - tilting from left to right.
• Y - tilting forwards and backwards.
• Z - moving up and down.
There is a method for each axis that returns a positive or negative number indicating a measurement in milli-g’s.
'Reading' a 0 means the accelerometer is “level” along that particular axis.
Example: simple spirit-level that uses get_x to measure how level the device is along the X axis:
from microbit import *
while True:
reading = accelerometer.get_x()
if reading > 20:
display.show("R")
elif reading < -20:
display.show("L")
else:
display.show("-")
Holding the device flat should display ---
however, rotate it left or right and it’ll show L and R respectively.
To constantly react to change, an infinite while loop is used.
The first thing to happen within the body of the loop is a measurement along the X axis which is called reading.
Because the accelerometer is sensitive I’ve made level +/-20 in range. It’s why the if and elif conditionals check for > 20 and < -20.
The else statement means that if the reading is between -20 and 20 then we consider it level. For each of these conditions we use the display to show the appropriate character.
There is also a get_y method for the Y axis and a get_z method for the Z axis.
If you’ve ever wondered how a mobile phone knows which up to show the images on its screen, it’s because it uses an accelerometer in exactly the same way as the program above. Game controllers also contain accelerometers to help you steer and move around in games.
External Link to useful lesson