Skip to content

Accelerometer

How it works

The component used is NXP/Freescale MMA8652.

The accelerometer can measure accelerations of between +2g to -2g.

accelerometer

The micro:bit measures movement along three axes:

X - tilting from left to right. Y - tilting forwards and backwards. Z - moving up and down.

Reading the raw sensor data

from microbit import *

while True:
    x = accelerometer.get_x()
    y = accelerometer.get_y()
    z = accelerometer.get_z()
    #print("x, y, z:", x, y, z)
    print((x,y,z))
    sleep(500)

Use Mu-code Plot funciton for plotting this as a graph.

The example below shows a graph for the X axis only.

Microbit Flash

or in blocks

Blocks accel

Using the sensor data

The Fluid simulation is a good example of how you can use the accelerometer data.

See the Filters section for more details.

Detecting gestures

Gestures available:

up, down, left, right, face up, face down,freefall, 3g, 6g, 8g, shake
from microbit import *

while True:
    gesture = accelerometer.current_gesture()
    if gesture == "face up":
        display.show(Image.HAPPY)
    else:
        display.show(Image.ANGRY)

or in blocks

Blocks gestures