Home Micro BitMicro Bit Code Micro:bit accelerometer Micropython example

Micro:bit accelerometer Micropython example

The micro:bit has an accelerometer on board, you can see this in the picture below

microbit

When you flash the program, you should see a reading of the accelerometer x axis being printed at the bottom of the Mu application. You can move the Micro:bit around and the values should change

 

Code

[codesyntax lang=”python”]

from microbit import *

while True:
    x = accelerometer.get_x()
    print(x)
    sleep(1000)

[/codesyntax]

 

You can also use the get_y and get_z() methods to find out the readings on the other axes. here is an example showing this

[codesyntax lang=”python”]

from microbit import *
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
print (“x: ” + str(x) + ” y: ” + str(y) + ” z: “+ str(z))
sleep(1000)

[/codesyntax]

You may also like