Salted fish ZTMR example-acceleration sensor

Salted fish ZTMR example-acceleration sensor

MMA7660FC
  • MMA7660FC can be customized in six directions

  • Integrated many intelligent motion functions, such as direction, vibration and tap detection

  • Automatic wake-up sleep function Automatic wake-up sleep function

  • Including intelligent power management function Including intelligent power management function

A three-axis accelerometer chip (MMA7660FC) (U5) is integrated on ZTMS, which can be used directly through programming.

MMA7660FC is a digital output (I2C), ultra-low power, compact capacitive micromotor accelerometer. It has a low-pass filter, 0g offset and gain error compensation, 6-bit resolution, and user-configurable output rate. The device can provide interrupts such as sensor data changes, product direction and gesture recognition through the interrupt pin (INT). The MMA7660FC is packaged in a very small 3mm x 3mm x 0.9mm DFN package.

Main control board: ZTMR1.1python development board

The principle of using the MMA7660FC three-axis accelerometer is very simple, that is, it is
presented by data in the x, y, and z directions according to the force. The measurement results range from -32 to 31, approximately -1.5g to 1.5g (g
is the acceleration of gravity, 9.8m / s ^ 2). We only need to know the values ​​in the above three directions to calculate
the acceleration in each direction. The principle is shown in the figure below:
Insert picture description here

Basic use of acceleration sensor
acc=pyb.Accel()
acc.x()          #读取X轴参数

The above example returns signed angle values ​​between -30 and 30. Note that the measurement result is not accurate, which means that the measurement data will still appear even if it remains completely still. Therefore, the data obtained by the x () method cannot be used as an accurate value, but should be regarded as a range of accuracy.
The code for tilting the development board and lighting the LED via the acceleration sensor is as follows:

import pyb
accel = pyb.Accel
light = pyb.LED(3)
SENSITIVITY = 3
while True:
	x = accel.x。
	if abs(x) > SENSITIVITY:
	light.on。
	else:
	light.off。
	pyb.delay(lOO)

In the above code, we create two objects, Accel and LED, and then directly obtain the value of the acceleration sensor in the X direction. If the value of x value is greater than the fixed value SENSITIVITY, the LED will be lit, otherwise it will be extinguished. The pyb.delay () function is called in the loop. If the value of x is close to SENSITIVITY, the LED will flash very frequently.

Making a spirit level

The above routine only uses the angle value in the x direction, but we can use the value of the y () function and more LED lights to build the development board into a level.

There are 4 LED lights on the development board, and the names marked on the board are D2, D3, D4, D5.

Callout pyb instance name colour
D4 LED(1) red
D5 LED(2) green
D6 LED(3) yellow
D7 LED(4) blue

Case: oled to obtain acceleration sensor value

# main.py -- put your code here!

import pyb

xlights = (pyb.LED(2), pyb.LED(3))  #绿灯,黄灯
ylights = (pyb.LED(1), pyb.LED(4))  #红灯,蓝灯
  
accel = pyb.Accel()
SENSITIVITY = 1        
  
while True:
	x = accel.x()
	if x > SENSITIVITY:
		xlights[0].on()
		xlights[1].off()
	elif x < -SENSITIVITY:
		xlights[1].on()
		xlights[0].off()
	else:
		xlights[0].off()
		xlights[1].off()
        
	y = accel.y()
	if y > SENSITIVITY:
		ylights[0].on()
		ylights[1].off()
	elif y < -SENSITIVITY:
		ylights[1].on()
		ylights[0].off()
	else:
		ylights[0].off()
		ylights[1].off()
	  
	pyb.delay(100)

A tuple containing LED objects in the x and y directions is created. Tuples in Python are immutable objects, which means they cannot be changed once created. Then we start the same as the previous routine, but when the value of x is positive or negative, it lights different LED lights. The same principle applies in the y direction.

Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/105464985