# 9. Accelerometer¶

The acceleration sensor can measure the acceleration due to gravity. During the acceleration process, the sensor uses the Newton’s second law to obtain the acceleration value by measuring the inertial force received by the mass. The accelerometer on the mPython Board can measure acceleration, with a measurement range of -2g to + 2g.。

It is 3-axis measurement of the mPython Board with positive or negative values on each axis. As the positive axis approaches the direction of gravitational acceleration, its value increases toward the positive direction, and conversely decreases toward the negative direction.

- X - Tilt forward and backward.
- Y - Tilt left and right。
- Z - Flip up and down。

Example：Observe the changes of the acceleration values of the three axes through the OLED display

```
from mpython import *
while True:
oled.fill(0)
x1 = accelerometer.get_x()
y1 = accelerometer.get_y()
z1 = accelerometer.get_z()
oled.DispChar("Acceleration x:", 0, 0)
oled.DispChar(str(x1), 48, 0)
oled.DispChar("Acceleration y:", 0, 16)
oled.DispChar(str(y1), 48, 16)
oled.DispChar("Acceleration z:", 0, 32)
oled.DispChar(str(z1), 48, 32)
oled.show()
```

First, import the mPython module:

```
from mpython import *
```

Obtain X、Y、Z 3-axis acceleration:

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

Note

Obtain the 3 axis acceleration by accelerometer.get_x() . The methods to get the 3-axis acceleration are `get_x()`

、`get_y()`

、`get_z()`

。
The measured value of each axis is positive or negative according to the direction, indicating the value in grams。

You can try to place the mPython Board panel as follows and observe the 3-axis data:

- Flat on the surface top –(0,0,-1)
- Flip the flat surface top –(0,0,1)
- The bottom edge of the control board is upright –(1,0,0)
- The left side of the control panel is upright –(0,1,0)

Note

Did you find any patterns? When the acceleration of gravity is consistent with the direction of the acceleration axis, it is equal to the acceleration of gravity of 1g. Positive direction is + 1g, negative direction is -1g. If you shake the control panel violently, you will see the acceleration reach ± 2g, which is because the maximum measurement value of this accelerometer is ± 2g.

## 9.1. Horizontal ball¶

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | ```
from mpython import * #导入mpython模块
Center_x=63 #设定中心点（原点）x的坐标
Center_y=31 #设定中心点（原点）y的坐标
while True:
x=accelerometer.get_x() #获取X轴的加速度
y=accelerometer.get_y() #获取Y轴的加速度
if y<=1 and y>=-1:
offsetX=int(numberMap(y,1,-1,-64,64)) #映射Y轴偏移值
if x<=1 and x>=-1:
offsetY=int(numberMap(x,1,-1,32,-32)) #映射X轴偏移值
move_x=Center_x+offsetX #水平球在X坐标上的移动
move_y=Center_y+offsetY #水平球在Y坐标上的移动
oled.circle(Center_x,Center_y,6,1) #画中心固定圆：空心
oled.fill_circle(move_x,move_y,4,1) #画移动的水平球：实心
oled.DispChar("%0.1f,%0.1f" %(x,y),85,0) #显示水平球在X、Y轴的加速度值
if offsetX==0 and offsetY==0:
rgb.fill((0,10,0)) #水平球在中心位置亮绿灯，亮度为10
rgb.write()
else:
rgb.fill((0,0,0)) #水平球不在中心位置灭灯
rgb.write()
oled.show()
oled.fill(0)
``` |

When it is detected that the control panel is tilted in the X-axis and Y-axis directions (range -1g to + 1g), the X-axis and Y-axis offset values, that is, acceleration values (range -1 to 1), are mapped to set The center point is the Y coordinate (range 32 to -32) and the X coordinate (range -64 to 64) on the X coordinate of 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 the mapping function, `inputNum`

is the variable to be mapped, `bMin`

is the minimum value that needs to be mapped, `bMax`

is the maximum value that needs to be mapped, `cMin`

as the minimum value of the mapped, `cMax`

as the maximum value of the mapped.

The movement of the horizontal ball on the X and Y coordinates: the movement of the horizontal ball on the coordinates = the position of the center point + the offset value of the acceleration:

```
move_x=Center_x+offsetX
move_y=Center_y+offsetY
```

If the horizontal ball moves to the center position, green light on, otherwise not lighted:

```
if offsetX==0 and offsetY==0:
rgb.fill((0,10,0)) #The horizontal ball lights green at the center, with a brightness of 10
rgb.write()
else:
rgb.fill((0,0,0)) #The horizontal ball is not in the center position, and the green light not lighted
rgb.write()
```

## 9.2. Calculate the tilt angle of the mPython Board¶

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ```
# 通过y轴加速度求y轴与水平面倾斜角度
from mpython import*
from math import acos,degrees
while True:
y=accelerometer.get_y()
if y<=1 and y>=-1:
rad_y=acos(y)
deg_y=90-degrees(rad_y)
oled.DispChar('%.2f°' %deg_y,50,25)
oled.show()
oled.fill(0)
``` |

First, import the mPython module and ACOS function and Degrees function in the math module:

```
from mpython import *
from math import acos,degrees
```

Obtain the X axis acceleration:

```
x = accelerometer.get_x()
```

Assuming that the reference plane of the mPython Board is the table top, during the tilting, the Y axis is parallel to the table top , and its angle is unchanged (always 0 degrees). What changes are the angle between the X axis and the desktop and the Z axis and the desktop. Angle, and the degree of change of the angle between the desktop and the X axis and Z axis is the same. In order to facilitate the analysis, we look down from the direction of the Y axis, then this problem will be simplified to only the two-dimensional relationship between the X axis and the Z axis. Suppose the control board is in the following state at a certain moment：

In this figure, the Y axis has been simplified to coincide with the origin O of the coordinate system. Let’s take a look at how to calculate the tilt angle of the control panel, which is the angle a with the desktop. g is the acceleration of gravity, gx and gz are the components of g in the X axis and Z axis respectively.

According to the arc cosine theorem, calculate the radian value of angle b:

```
rad_x=acos(x)
```

Calculate the angle of the included angle, ie angle a = angle c = 90 degrees-angle b:

```
deg_x=90-degrees(rad_x)
```

Note

- acos() the function returns the arc cosine radian value.
- degrees() the function is to convert radians to degrees.