Home Code Using the Micro:bit onboard accelerometer a Micropython example

Using the Micro:bit onboard accelerometer a Micropython example

by shedboy71

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

microbit

For those that are interested it is actually a MMA8653FC . Here is some basic info from the NXP website

Features

Of course you do not really need to worry about the technical details unless like me you like looking at datasheets

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

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

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

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)

 

You may also like

Leave a Comment