Accelerometer

An accelerometer measures the acceleration caused by gravity. During acceleration, the sensor measures the inertial force on a proof mass and uses Newton’s second law to obtain the acceleration value. The accelerometer on the mPython Board can measure acceleration in the range of -2g to +2g.

The mPython Board measures along 3 axes. The measurement value of each axis is positive or negative: the closer the positive axis direction is to the gravitational acceleration direction, the more the value increases in the positive direction; conversely it decreases in the negative direction. When the reading is 0, the board is placed “level” along that particular axis.

  • X - Tilting forward and backward.

  • Y - Tilting left and right.

  • Z - Flipping up and down.

Before use, import the mpython module:

from mpython import *

Get the acceleration along the X, Y and Z axes:

x1 = accelerometer.get_x()
y1 = accelerometer.get_y()
z1 = accelerometer.get_z()

Note

Use accelerometer.get_x() to get the 3-axis acceleration. The methods for getting each axis are get_x(), get_y() and get_z(). The measurement value of each axis is positive or negative depending on direction, expressed in units of g.

You can try placing the mPython Board in the following orientations and observe the 3-axis data:

  • Flat on the table –(0,0,-1)

  • Flipped flat on the table –(0,0,1)

  • Bottom edge standing upright on the table –(1,0,0)

  • Left edge standing upright on the table –(0,1,0)

Note

Did you notice the pattern? When the gravitational acceleration direction aligns with an accelerometer axis, it equals 1g of Earth’s gravitational acceleration. The positive direction gives +1g and the opposite direction gives -1g. If you shake the mPython Board vigorously, you will see the acceleration reach ±2g, because the maximum measurement value of this accelerometer is ±2g.

Level Ball

When tilt is detected on the X and Y axes of the mPython Board (range -1g to +1g), the X and Y offset values (i.e. acceleration values, range -1 to 1) are mapped to Y coordinates (range 32 to -32) and X coordinates (range -64 to 64) respectively, with a preset center point as the origin:

if y<=1 and y>=-1:
    offsetX=int(numberMap(y,1,-1,-64,64))
if x<=1 and x>=-1:
    offsetY=int(numberMap(x,1,-1,32,-32))

Note

numberMap(inputNum, bMin, bMax, cMin, cMax) is a mapping function where inputNum is the variable to map, bMin is the minimum value of the input range, bMax is the maximum value of the input range, cMin is the minimum value of the output range, and cMax is the maximum value of the output range.

Movement of the level ball on the X and Y coordinates: ball position = center point + acceleration offset:

move_x=Center_x+offsetX
move_y=Center_y+offsetY

If the level ball moves to the center position, the green LED lights up; otherwise it stays off:

if offsetX==0 and offsetY==0:
    rgb.fill((0,10,0))          # Green LED on when the ball is at center, brightness 10
    rgb.write()
else:
    rgb.fill((0,0,0))           # LED off when the ball is not at center
    rgb.write()

Calculating the Tilt Angle of the mPython Board

By measuring the acceleration caused by gravity, the tilt angle of the device relative to the horizontal plane can be calculated
 1# Calculate Y-axis tilt angle from horizontal using Y-axis acceleration
 2from mpython import*
 3from lv_gui import *
 4from math import acos,degrees
 5
 6
 7gui = GUI()
 8while True:
 9    y=accelerometer.get_y()
10    if y<=1 and y>=-1:
11        rad_y=acos(y)
12        deg_y=90-degrees(rad_y)
13        gui.fill(type=0)
14        gui.draw_label(text=str('%.2f°' %deg_y), row=1, color=0xffffff, wrap=False)
15        gui.update()
16
17        
18

Before use, import the mpython module and the acos and degrees functions from the math module:

from mpython import *
from math import acos,degrees

Get the acceleration along the X axis:

x = accelerometer.get_x()

Assume the reference horizontal plane of the mPython Board is the table surface. During tilting, the Y axis remains parallel to the table and its angle does not change (always 0 degrees). What changes is the angle between the X axis and the table, and the angle between the Z axis and the table, and the change in both angles is the same. For simplicity, if we look down from the Y axis direction, the problem reduces to a two-dimensional relationship between only the X and Z axes. Assume the mPython Board is in the following state at a certain moment:

../../../../../_images/xgraph1.png

In this diagram, the Y axis has been simplified and coincides with the origin O of the coordinate system. Let’s see how to calculate the tilt angle of the mPython Board, i.e. the angle a with the table surface. g is the gravitational acceleration, and gx and gz are the components of g along the X and Z axes respectively.

Since gravitational acceleration is perpendicular to the horizontal plane, we get:
angle a + angle b = 90 degrees
The X axis and Y axis are perpendicular, so we get:
angle c + angle b = 90 degrees
Therefore:
angle a = angle c

Using the inverse cosine theorem, calculate the radian value of angle b:

rad_x=acos(x)

Calculate the angle in degrees, i.e. angle a = angle c = 90 degrees - angle b:

deg_x=90-degrees(rad_x)

Note

  • The acos() function returns the inverse cosine in radians.

  • The degrees() function converts radians to degrees.